SQL ORDER BY: sorting query results, explained and practiced
ORDER BY decides the order of your rows: a price list from cheapest up, orders by date, staff by department and salary. Add LIMIT and you keep only the first few rows, such as the three latest orders.
In short: ORDER BY price sorts ascending (smallest first), ORDER BY price DESC sorts descending. Separate several sort columns with commas. LIMIT 3 at the end keeps just the first three rows.
What you should know first: picking columns with SELECT and filtering rows with WHERE.
Try it now
Without ORDER BY there is no guaranteed order. Rows often come back the way they're stored, but you can't rely on it. In these exercises the order of the rows is part of the check.
Task For the new price list: show the name and price of every product, sorted by price with the cheapest first.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
How ORDER BY works
ORDER BY comes after FROM and WHERE, near the end of the query. After it you name the column to sort by and, if you like, the direction:
| Keyword | Direction | Numbers | Text | Dates (YYYY-MM-DD) |
|---|---|---|---|---|
ASC or nothing | ascending | low to high | A to Z | oldest first |
DESC | descending | high to low | Z to A | newest first |
The warehouse wants to know which products they hold the most of:
SELECT name, stock
FROM products
ORDER BY stock DESC;
The first five of twelve rows:
| name | stock |
|---|---|
| Copy paper, 500 sheets | 420 |
| Blue ballpoint pens, 10-pack | 310 |
| Lever arch file, gray | 185 |
| Notebook A5, ruled | 140 |
| Black ink cartridge | 64 |
At the bottom sit the sticky notes and the shredder, both with a stock of 0. Which of the two comes first isn't fixed: the SQLite documentation says the order of rows with equal sort values is undefined. If you need it fixed, add a second sort column, for example ORDER BY stock DESC, name.
Sorting by more than one column
With several sort columns, the first one wins. Only where it's tied does the second one count, then the third. ASC and DESC apply only to the column right before them: ORDER BY department, salary DESC lists departments A to Z and, inside each department, salaries from high to low.
Task Management needs a list for the pay review: department, last_name and salary for every employee. Sort by department from A to Z, and within each department put the highest salary first.
employees 10 rows, 7 columns
| employee_id | first_name | last_name | department | salary | hire_date | manager_id |
|---|---|---|---|---|---|---|
| 1 | Karen | Mitchell | Management | 93600 | 2018-01-02 | NULL |
| 2 | Tom | Bradley | Sales | 62400 | 2019-04-01 | 1 |
| 3 | Julia | Reyes | Sales | 46800 | 2021-06-15 | 2 |
| 4 | Marcus | Allen | Sales | 49200 | 2020-09-01 | 2 |
| 5 | Sarah | Long | Warehouse | 39600 | 2022-02-01 | 1 |
| 6 | Felix | Turner | Warehouse | 35400 | 2024-03-18 | 5 |
| 7 | Nina | Cooper | Accounting | 52800 | 2019-10-01 | 1 |
| 8 | David | Ross | Customer service | 38400 | 2023-05-02 | 2 |
| 9 | Laura | Hughes | Customer service | NULL | 2025-09-01 | 2 |
| 10 | Paul | Ward | Accounting | 43200 | 2022-11-14 | 7 |
Just the first rows: LIMIT and OFFSET
LIMIT n goes at the very end and keeps the first n rows of the sorted result. OFFSET m skips m rows first, which is how you page through results. Places 4 to 6 by stock level:
SELECT name, stock
FROM products
ORDER BY stock DESC
LIMIT 3 OFFSET 3;
| name | stock |
|---|---|
| Notebook A5, ruled | 140 |
| Black ink cartridge | 64 |
| Black toner XL | 22 |
LIMIT without ORDER BY gives you any old rows. For "the three most expensive" or "the three latest", you need both.
Task The dashboard should show the three most recent orders. Show order_id, order_date and status for those three orders, newest first.
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
Sorting by column number
Instead of a name you can give the column's position in the SELECT list: SELECT name, price FROM products ORDER BY 2 sorts by price. It's short, but fragile. Add a column at the front later and the query quietly sorts by something else. MySQL calls this syntax deprecated, and Microsoft advises against it in SQL Server. Use the name.
Where do NULLs go?
Laura Hughes has no salary on file yet. With ORDER BY salary she comes first in these exercises, with ORDER BY salary DESC last, because SQLite treats NULL as the smallest value when sorting. Not every database agrees (see the table below). In SQLite and PostgreSQL you can decide for yourself by adding NULLS FIRST or NULLS LAST, as in ORDER BY salary DESC NULLS FIRST. The NULL lesson explains what NULL actually is.
Common mistakes
Clauses in the wrong order
... LIMIT 3 ORDER BY order_date DESC fails because LIMIT always comes last. The exercise tells you "LIMIT goes at the very end, after ORDER BY". Putting ORDER BY before WHERE fails too, with a syntax error and a reminder of the right order of the clauses.
DESC only covers one column
ORDER BY department DESC, salary reverses the departments and leaves salary ascending. The exercise tells you "The rows are right, but not their order". Put DESC straight after every column that should run high to low.
Forgetting the direction
ORDER BY order_date LIMIT 3 returns the three oldest orders instead of the newest. The exercise says these are the wrong rows and that with LIMIT the sort order decides which rows come first.
TOP or FETCH FIRST in the exercises
SELECT TOP 3 ... is SQL Server syntax and FETCH FIRST 3 ROWS ONLY is the SQL standard's. SQLite knows neither. For TOP the exercise explains that it only exists in SQL Server and shows the LIMIT version. Use LIMIT here.
ORDER BY in MySQL, PostgreSQL, SQL Server and SQLite
ORDER BY with ASC, DESC and several columns works the same everywhere. The differences are in limiting rows and in NULL handling:
| Database | First 3 rows | NULLs in ascending order | NULLS FIRST / LAST |
|---|---|---|---|
| MySQL | LIMIT 3, also LIMIT 3 OFFSET 6 or LIMIT 6, 3 | first | not in the syntax |
| PostgreSQL | LIMIT 3 or FETCH FIRST 3 ROWS ONLY | last | yes |
| SQL Server | SELECT TOP (3) ... or OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY after ORDER BY | first | not in the syntax |
| SQLite (these exercises) | LIMIT 3, also LIMIT 3 OFFSET 6 or LIMIT 6, 3 | first | yes, since version 3.30.0 |
Watch out for the short comma form: LIMIT 6, 3 means "skip 6, then take 3", so the numbers are the other way round from LIMIT and OFFSET. The SQLite documentation explicitly recommends the OFFSET form for that reason. OFFSET … FETCH is the SQL standard's syntax since SQL:2008, and PostgreSQL supports it alongside LIMIT.
Sources: MySQL Reference Manual (SELECT Statement, Problems with NULL Values, Sorting Rows), PostgreSQL documentation (SELECT, Sorting Rows), Microsoft Learn (ORDER BY Clause, TOP), sqlite.org (SELECT, Release History 3.30.0).
FAQ
Does the sort column have to be in the SELECT list?
No. SELECT name FROM products ORDER BY price works: prices are used for sorting but not shown. Still, whoever reads the result usually finds it easier when the sort column is visible.
Why do rows sometimes repeat or go missing when paging with OFFSET?
When several rows share the same sort value, the database is free to order them differently on each run. One row can then show up on two pages while another never appears. Add a unique column, such as the ID, as the last sort key.
Why does "apple" come after "Zebra"?
That depends on the database's collation. In these exercises SQLite compares text byte by byte, so capital letters come before small ones and accented letters such as é land after z. According to the MySQL manual, MySQL normally sorts text without regard to case; PostgreSQL and SQL Server follow whatever collation is set up.
Can I sort by a calculation?
Yes, ORDER BY accepts an expression as well, for example ORDER BY price * stock DESC for stock value. How to show such a value as its own column is covered in the calculations lesson.