SQL ORDER BY, LIMIT and OFFSET
ORDER BY sorts the result, ascending by default. LIMIT keeps only the first rows, and OFFSET skips rows first. Without ORDER BY, the order of rows is not guaranteed, so "top 10" needs both.
How do you get the top 5?
SELECT name, revenueFROM customersORDER BY revenue DESCLIMIT 5;
How do you sort by two columns?
ORDER BY country ASC, revenue DESC
sorts by country and, within each country, by revenue from high to low.
How do you page through results?
LIMIT 10 OFFSET 20
returns rows 21 to 30.
Does every database use LIMIT?
| Database | Top 5 |
|---|---|
| SQLite, MySQL, PostgreSQL | LIMIT 5 |
| SQL Server | SELECT TOP 5 … |
| Standard SQL, Oracle, PostgreSQL | FETCH FIRST 5 ROWS ONLY |
Common interview follow-up
"What about ties?" LIMIT cuts at exactly 5 rows even if the sixth has the same revenue. To include ties, use a window function such as DENSE_RANK.
Try it yourself. Practise this in a real database in your browser, with instant feedback.
Practise ORDER BY and LIMIT Practise LIMIT and OFFSET
Practise ORDER BY and LIMIT Practise LIMIT and OFFSET
Updated November 2026.