SQL 101· Level 3

ORDER BY, LIMIT and DISTINCT

These three shape the result once the rows have been chosen.

  • ORDER BY sorts the rows. Add DESC for largest first (or Z to A). Without ORDER BY, rows can come back in any order.
  • Sort by several columns with commas: ORDER BY last_name, first_name sorts by last name, then by first name within each last name.
  • LIMIT 10 keeps only the first 10 rows. Pair it with ORDER BY to get a "top 10".
  • SELECT DISTINCT removes duplicate rows, so each different value appears once.

Clauses always go in this order: SELECT, FROM, WHERE, ORDER BY, LIMIT.

Worked example

What were the ten highest marks awarded in 2025?

SELECT student_id, unit_code, mark
FROM enrolments
WHERE year = 2025
ORDER BY mark DESC
LIMIT 10
Getting the practice database ready…

This takes a few seconds the first time. Nothing leaves your computer.

Exercises in this level