Skip to main content
Albero Academy

Sorting & Limiting

Order results and return just the top rows.

Foundations7 min

ORDER BY sorts the result, and LIMIT returns just the first N rows — together they answer "top 10" style questions and make output readable.

ORDER BY

sort the result
SELECT name, package FROM students
ORDER BY package DESC; -- highest first (ASC is the default)
SELECT * FROM students
ORDER BY programme ASC, package DESC; -- by two keys

LIMIT & pagination

top N and paging
SELECT name, package FROM students
ORDER BY package DESC
LIMIT 10; -- top 10
SELECT * FROM students
ORDER BY id LIMIT 10 OFFSET 20; -- page 3 of 10-row pages

Key takeaways

  1. 01ORDER BY sorts the result; ASC (default) or DESC, and by multiple keys.
  2. 02LIMIT returns the first N rows — pair it with ORDER BY for meaningful "top N".
  3. 03OFFSET skips rows for pagination.
  4. 04LIMIT syntax varies by database (TOP / FETCH FIRST elsewhere).

Finished this chapter?

Mark it complete to track your progress through SQL.