Almost every business question is a top-N question in disguise: top 10 products, latest 5 orders, highest-paid employees. ORDER BY and LIMIT are how SQL answers them.
Key Points
- Sort ascending (default): ORDER BY salary; descending: ORDER BY salary DESC
- Multi-column sort: ORDER BY department ASC, salary DESC
- Top-N: SELECT * FROM products ORDER BY revenue DESC LIMIT 10;
- Pagination: LIMIT 10 OFFSET 20 returns rows 21–30
- ORDER BY works on expressions and aliases too
- Sorting large tables is expensive — indexes on sort columns help
.png)