Skip to main content

How to Sort Rows in SQLite with ORDER BY

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Without ORDER BY, SQLite promises no order for the rows it returns. ORDER BY sorts them by a column, smallest first, and DESC turns that around. This guide sorts books by their page count both ways, with real output from SQLite 3.54.1.

Syntax​

Syntax
SELECT <columns> FROM <table> ORDER BY <column>;
SELECT <columns> FROM <table> ORDER BY <column> DESC;
PartWhat it means
ORDER BY <column>Sorts the rows by that column, smallest first. That's ASC, the default
DESCSorts largest first

ORDER BY comes after WHERE, at the end of the SELECT. SQLite's documentation on ORDER BY: "If a SELECT statement that returns more than one row does not have an ORDER BY clause, the order in which the rows are returned is undefined."

Prerequisites​

  • The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run sqlite3 --version to see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.

Step 1: Write the Script​

Save this as order_by.sql:

order_by.sql
CREATE TABLE books (
title TEXT NOT NULL,
author TEXT NOT NULL,
pages INTEGER NOT NULL
);

INSERT INTO books (title, author, pages) VALUES ('Emma', 'Jane Austen', 474);
INSERT INTO books (title, author, pages) VALUES ('Persuasion', 'Jane Austen', 249);
INSERT INTO books (title, author, pages) VALUES ('Dune', 'Frank Herbert', 412);

SELECT title, pages FROM books ORDER BY pages;
SELECT title, pages FROM books ORDER BY pages DESC;

The order_by.sql script in a macOS-style code window: a books table with three books, a SELECT of title and pages ORDER BY pages, and the same SELECT ORDER BY pages DESC.

  • ORDER BY pages sorts the books from fewest pages to most.
  • ORDER BY pages DESC sorts them from most to fewest.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table library.db ".read order_by.sql"

sqlite3 opens the database file library.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.

Output
+------------+-------+
| title | pages |
+------------+-------+
| Persuasion | 249 |
| Dune | 412 |
| Emma | 474 |
+------------+-------+
+------------+-------+
| title | pages |
+------------+-------+
| Emma | 474 |
| Dune | 412 |
| Persuasion | 249 |
+------------+-------+

The output in a terminal: the books sorted by pages from Persuasion at 249 through Dune at 412 to Emma at 474, then the same books in the opposite order.

The first result runs from Persuasion's 249 pages up to Emma's 474. DESC turned the same three rows around.

Common Mistakes​

MistakeWhat happensDo this instead
ORDER BY before WHERE, as in ORDER BY pages WHERE pages > 300near "WHERE": syntax errorWHERE first, ORDER BY last
Expecting text to sort regardless of capitalsA book titled anthem came after Persuasion: capital letters sort before small onesORDER BY title COLLATE NOCASE put anthem first

Troubleshooting​

SymptomLikely causeFix
Rows come back in a different order each time you change somethingThe query has no ORDER BYAdd one: without it, the order isn't defined
Parse error near line 1 of order_by.sql: table books already existsThe script already ran on library.dbDelete library.db and run the script again

FAQ​

Can I sort by two columns?​

Yes, separated by commas: the second sorts rows that tie on the first. ORDER BY author, title DESC listed Frank Herbert's Dune, then Jane Austen's Persuasion and Emma, her titles in reverse order.

Where do missing values go?​

First, when sorting smallest first. With a book that had no page count, ORDER BY pages put it at the top, and ORDER BY pages NULLS LAST moved it to the end. The documentation: "SQLite considers NULL values to be smaller than any other values for sorting purposes."

Conclusion​

ORDER BY pages sorted the books from 249 to 474 pages, and ORDER BY pages DESC sorted them back from 474 to 249.

Additional Resources​