Sorting with ORDER BY
Prerequisites: Filtering rows with WHERE
Without ORDER BY, a database returns rows in no guaranteed order —
whatever is fastest. When order matters, ask for it.
SELECT name, price
FROM products
ORDER BY price;
+-----------------------------+--------+
| name | price |
+-----------------------------+--------+
| USB-C Hub | 35.5 |
| Laptop Stand | 42 |
| Webcam | 59.9 |
| Mechanical Keyboard | 89 |
| Noise-Cancelling Headphones | 199 |
| 27-inch Monitor | 249.99 |
+-----------------------------+--------+
ASC (ascending, smallest first) is the default. DESC sorts largest
first:
SELECT name, price
FROM products
ORDER BY price DESC;
+-----------------------------+--------+
| name | price |
+-----------------------------+--------+
| 27-inch Monitor | 249.99 |
| Noise-Cancelling Headphones | 199 |
| Mechanical Keyboard | 89 |
| Webcam | 59.9 |
| Laptop Stand | 42 |
| USB-C Hub | 35.5 |
+-----------------------------+--------+
Sorting by several columns
SELECT name, category, price
FROM products
ORDER BY category, price DESC;
+-----------------------------+-------------+--------+
| name | category | price |
+-----------------------------+-------------+--------+
| Mechanical Keyboard | Accessories | 89 |
| Laptop Stand | Accessories | 42 |
| USB-C Hub | Accessories | 35.5 |
| Noise-Cancelling Headphones | Audio | 199 |
| 27-inch Monitor | Displays | 249.99 |
| Webcam | Video | 59.9 |
+-----------------------------+-------------+--------+
Rows are sorted by category first; within each category, by price
from highest to lowest.
Sorting by an alias
You can sort by a column alias from the SELECT:
SELECT name, price * 1.2 AS with_tax
FROM products
ORDER BY with_tax DESC;
+-----------------------------+--------------------+
| name | with_tax |
+-----------------------------+--------------------+
| 27-inch Monitor | 299.988 |
| Noise-Cancelling Headphones | 238.79999999999998 |
| Mechanical Keyboard | 106.8 |
| Webcam | 71.88 |
| Laptop Stand | 50.4 |
| USB-C Hub | 42.6 |
+-----------------------------+--------------------+
Where do NULLs go?
SQLite and MySQL put NULLs first when sorting ascending; PostgreSQL
and Oracle put them last. Standard SQL lets you choose with
NULLS FIRST or NULLS LAST (supported by SQLite and PostgreSQL):
SELECT name, city FROM customers ORDER BY city NULLS LAST;
+-------------------+----------+
| name | city |
+-------------------+----------+
| Margaret Hamilton | Boston |
| Linus Torvalds | Helsinki |
| Ada Lovelace | London |
| Alan Turing | London |
| Grace Hopper | New York |
| Tim Berners-Lee | NULL |
+-------------------+----------+
Practice
List all orders from newest to oldest.
Show solution
SELECT id, ordered_on, status
FROM orders
ORDER BY ordered_on DESC;
+----+------------+-----------+
| id | ordered_on | status |
+----+------------+-----------+
| 8 | 2025-04-22 | pending |
| 7 | 2025-04-10 | shipped |
| 6 | 2025-04-02 | pending |
| 5 | 2025-03-18 | cancelled |
| 4 | 2025-03-01 | delivered |
| 3 | 2025-02-20 | shipped |
| 2 | 2025-02-14 | delivered |
| 1 | 2025-01-05 | delivered |
+----+------------+-----------+