SQL 101· Level 3
ORDER BY, LIMIT and DISTINCT
These three shape the result once the rows have been chosen.
ORDER BYsorts the rows. AddDESCfor 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_namesorts by last name, then by first name within each last name. LIMIT 10keeps only the first 10 rows. Pair it with ORDER BY to get a "top 10".SELECT DISTINCTremoves 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.