AnalystPracticeGuides
DashboardSign up

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.

Watch it solved. Q2: top 3 salaries (ORDER BY DESC), from 1:12, in our video “SQL Interview Questions and Answers for Data Analysts (Easy to Hard)”. Pressing play loads the video from YouTube (privacy). Open on YouTube

How do you get the top 5?

SELECT name, revenue
FROM customers
ORDER BY revenue DESC
LIMIT 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?

DatabaseTop 5
SQLite, MySQL, PostgreSQLLIMIT 5
SQL ServerSELECT TOP 5 …
Standard SQL, Oracle, PostgreSQLFETCH 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

Updated November 2026.

← All free guides