SQL ORDER BY
A table has no built-in order, and a query without ORDER BY may return rows in whatever order is convenient for the database. ORDER BY sorts the result.
SELECT columns
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;ASC (ascending, the default) sorts from small to large and A to Z; DESC sorts the other way.
Sorting by one column
SELECT name, price
FROM products
WHERE category_id = 6
ORDER BY price;name | price
----------------------------+------
USB-C Cable | 9.99
Wireless Mouse | 24.99
Power Bank | 29.5
Mechanical Keyboard | 89
Noise-Cancelling Headphones | 149
(5 rows)
Descending order
The most recently hired employees first:
SELECT first_name, last_name, hire_date
FROM employees
ORDER BY hire_date DESC;first_name | last_name | hire_date
-----------+-----------+-----------
Owen | Price | 2024-05-13
Elena | Popescu | 2023-08-21
Hana | Sato | 2023-01-09
Aisha | Bello | 2022-04-04
Marco | Ricci | 2021-11-11
Tom | Becker | 2021-09-20
Rosa | Delgado | 2021-02-10
Lin | Zhou | 2020-06-01
Victor | Ames | 2020-01-15
Grace | Holt | 2019-03-01
(10 rows)
Sorting by several columns
When the first column has ties, the second column decides, and so on. Each column has its own direction:
SELECT country, city, name
FROM customers
WHERE country IN ('USA', 'Canada', 'France', 'UK')
ORDER BY country ASC, name DESC;country | city | name
--------+----------+----------------
Canada | Toronto | Green Fork
Canada | Toronto | Elif Yilmaz
France | Paris | Kai Moreau
France | Paris | Bright Bytes
UK | London | Nonna's Kitchen
UK | London | Jonas Garcia
USA | Austin | Tech Nest
USA | New York | Night Owl Books
(8 rows)
Within each country the names run Z to A.
Sorting by an expression or alias
You can sort by a calculated value, and — unlike WHERE — ORDER BY may use aliases from the SELECT list, because it runs last:
SELECT name, price, stock, price * stock AS stock_value
FROM products
WHERE category_id IN (7, 8)
ORDER BY stock_value DESC;name | price | stock | stock_value
-----------------------+-------+-------+------------
The SQL Handbook | 29 | 50 | 1450
Python Recipes | 34.5 | 30 | 1035
Learn Rust in Practice | 39 | 22 | 858
Chef Knife | 45 | 15 | 675
Coffee Grinder | 59.9 | 9 | 539.1
Cast Iron Pan | 38 | 10 | 380
(6 rows)
You may also write a column position (ORDER BY 4 DESC), but names are clearer and survive changes to the select list.
Where do NULLs go?
Different databases place NULL differently when sorting:
| Database | ASC |
DESC |
Override |
|---|---|---|---|
| SQLite, MySQL, SQL Server | NULLs first | NULLs last | SQLite: NULLS FIRST / NULLS LAST |
| PostgreSQL, Oracle | NULLs last | NULLs first | NULLS FIRST / NULLS LAST |
SELECT order_id, shipped_date
FROM orders
WHERE order_id BETWEEN 1 AND 6
ORDER BY shipped_date NULLS LAST, order_id;order_id | shipped_date
---------+-------------
2 | 2025-01-03
4 | 2025-01-12
3 | 2025-01-22
6 | 2025-02-11
1 | NULL
5 | NULL
(6 rows)
Tip: Whenever the order of rows matters — reports, pagination, "top 5" lists — always say so with
ORDER BY, and add a unique column such as the id as the last sort key so ties are broken the same way every time.
Text sorting and case
Text sorts by character code by default in SQLite, so upper-case letters come before lower-case ones. Add COLLATE NOCASE to sort case-insensitively: ORDER BY name COLLATE NOCASE.
Test yourself
What is the default sort direction?
- DESC
- ASC
- Random
- Insertion order
In ORDER BY country, name DESC, how is country sorted?
- Descending
- Ascending
- Not at all
- Same as name
Can ORDER BY use a column alias defined in the SELECT list?
- Yes
- No
- Only in MySQL
- Only with numbers