ORDER BY
Sort results by any column — ascending, descending, or both
Sorting Results
Without ORDER BY, SQL makes no guarantee about row order. You might get the same sequence each time, or you might not — it depends on the database engine. If order matters, you must always specify it explicitly. Here's how sorting works:
DESC to reverse the direction: largest first, Z→A, and most recent dates at the top of your results.ORDER BY category ASC, price DESC groups rows by category alphabetically, then within each category shows the most expensive products first. Later columns only matter when earlier columns have ties.ORDER BY 2 DESC sorts by the second column in your SELECT list. Handy for quick queries, though less readable than naming the column explicitly in production code.ORDER BY col ASC NULLS LAST (or NULLS FIRST) for explicit control. SQLite added the same syntax in 3.30 (Oct 2019). MySQL has no NULLS FIRST/LAST; emulate with ORDER BY col IS NULL, col.How ORDER BY Works
Sort your results before display
- ORDER BY runs LAST — after SELECT
- ASC = ascending (default), DESC = descending
- Multi-column sort: first column breaks ties with second
- Can reference column aliases or positions (1, 2, 3)
NULL Sorting Behavior
Where do NULLs end up?
- Behavior varies by database — never assume
- PostgreSQL / Oracle: NULLs sort LAST in ASC
- MySQL / SQLite / SQL Server: NULLs sort FIRST in ASC
- PostgreSQL: NULLS FIRST / NULLS LAST modifier available
Performance Impact
Sorting has a cost
- Sorting large results is expensive — add LIMIT when possible
- Index on ORDER BY column avoids a separate sort step
- ORDER BY + LIMIT = efficient top-N query
- Avoid ORDER BY in subqueries — it's usually ignored
Power Patterns
Advanced sorting techniques
- Multi-column:
ORDER BY country, name - Mixed direction:
ORDER BY price DESC, name ASC - Expression sort:
ORDER BY price * quantity DESC - Conditional:
ORDER BY CASE WHEN ... END
ASCAscending (default) — smallest/earliest/A firstDESCDescending — largest/latest/Z firstMultiple columnsSecond column breaks ties in the first| name | category | price |
|---|---|---|
| Wireless Mouse | Electronics | 24.99 |
| Office Desk | Furniture | 189 |
| LED Desk Lamp | Furniture | 45.5 |
| USB-C Cable | Electronics | 12.99 |
| Bluetooth Speaker | Electronics | 79 |
| Office Chair | Furniture | 95 |
| Coffee Maker | Appliances | 89.99 |
| Notebook Set | Stationery | 8.5 |
| Wireless Headphones | Electronics | 159 |
| Standing Desk | Furniture | 425 |
| id | order_date | total |
|---|---|---|
| 1 | 2024-03-15 | 24.99 |
| 2 | 2024-03-16 | 189 |
| 3 | 2024-03-18 | 45.5 |
| 4 | 2024-03-20 | 329 |
| 5 | 2024-03-22 | 12.99 |
| 6 | 2024-03-25 | 259 |
| 7 | 2024-03-28 | 95 |
| 8 | 2024-04-01 | 89.99 |
| 9 | 2024-04-03 | 8.5 |
| 10 | 2024-04-05 | 425 |
Worked Example
Show all products sorted by category (A-Z), then by price (highest first) within each category.
| name | category | price |
|---|---|---|
| Coffee Maker | Appliances | 89.99 |
| Wireless Headphones | Electronics | 159 |
| Bluetooth Speaker | Electronics | 79 |
| Wireless Mouse | Electronics | 24.99 |
| USB-C Cable | Electronics | 12.99 |
| Standing Desk | Furniture | 425 |
| Office Desk | Furniture | 189 |
| Office Chair | Furniture | 95 |
| LED Desk Lamp | Furniture | 45.5 |
| Notebook Set | Stationery | 8.5 |
Key Concepts
Pro Tip
Unsorted data is hard to read and impossible to paginate. ORDER BY gives results a predictable structure — essential for reports, top-N queries, and leaderboards.
When to Use
Top-selling products, most recent orders, alphabetical customer lists, leaderboards, any ranked listing.
Show all orders from the orders table sorted by total descending. Display id, order_date, and total. Show only the top 5.