How to Sort Rows in SQLite with ORDER BY
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
SELECT <columns> FROM <table> ORDER BY <column>;
SELECT <columns> FROM <table> ORDER BY <column> DESC;
| Part | What it means |
|---|---|
ORDER BY <column> | Sorts the rows by that column, smallest first. That's ASC, the default |
DESC | Sorts 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 --versionto 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:
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;

ORDER BY pagessorts the books from fewest pages to most.ORDER BY pages DESCsorts them from most to fewest.
Step 2: Run It
In the folder that holds the script, run:
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.
+------------+-------+
| title | pages |
+------------+-------+
| Persuasion | 249 |
| Dune | 412 |
| Emma | 474 |
+------------+-------+
+------------+-------+
| title | pages |
+------------+-------+
| Emma | 474 |
| Dune | 412 |
| Persuasion | 249 |
+------------+-------+

The first result runs from Persuasion's 249 pages up to Emma's 474. DESC turned the same three rows around.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
ORDER BY before WHERE, as in ORDER BY pages WHERE pages > 300 | near "WHERE": syntax error | WHERE first, ORDER BY last |
| Expecting text to sort regardless of capitals | A book titled anthem came after Persuasion: capital letters sort before small ones | ORDER BY title COLLATE NOCASE put anthem first |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Rows come back in a different order each time you change something | The query has no ORDER BY | Add one: without it, the order isn't defined |
Parse error near line 1 of order_by.sql: table books already exists | The script already ran on library.db | Delete 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
- The ORDER BY clause — sorting, collations and
NULLS FIRST/NULLS LAST. - SELECT — where
ORDER BYfits in the statement.
