LQ
SQL Reference
Practice →
SQL ReferenceFoundationsORDER BY & LIMIT

ORDER BY & LIMIT

Sort results and cap how many rows you get back.

Questions this answers
  • How do I sort results alphabetically or by number?
  • How do I get the top 10 results?
  • How do I get the most recent rows?
  • How do I paginate results?

How it works

ORDER BY sorts the result set. By default it sorts ascending (smallest first). Add DESC for descending. LIMIT caps the number of rows returned — useful for top-N queries and pagination.

Sort ascending and descending

SELECT name, monthly_listeners
FROM artists
ORDER BY monthly_listeners DESC;

Top N rows

SELECT name, monthly_listeners
FROM artists
ORDER BY monthly_listeners DESC
LIMIT 10;
💡 Always pair LIMIT with ORDER BY. Without ORDER BY, the "top 10" is arbitrary.

Pagination with OFFSET

SELECT name FROM artists
ORDER BY name
LIMIT 20 OFFSET 40;
💡 OFFSET skips rows. LIMIT 20 OFFSET 40 returns rows 41-60 — useful for page 3 of 20-per-page results.

Ready to practise ORDER BY & LIMIT?

Solve real exercises with AI feedback — free to start.

Try it free →