Raw scan order is random soup. ORDER BY cooks the rows into a ranking; LIMIT serves only the top slices.
1 ORDER BY sorts
Names a column to sort by. ASC (small to big) is the default; DESC flips it.
2 LIMIT trims
Keeps only the first N rows of the finished result.
3 Order matters
Sorting happens BEFORE the limit: LIMIT 3 returns the top 3 of the sorted list.
4 Top-N pattern
ORDER BY ... LIMIT is the classic leaderboard query behind games, shops and feeds.
Put the rows in order, then cut
ORDER BY sorts1Names a column to sort by. ASC (small to big) is the default; DESC flips it. LIMIT trims2Keeps only the first N rows of the finished result. Order matters3Sorting happens BEFORE the limit: LIMIT 3 returns the top 3 of the sorted list. Top-N pattern4ORDER BY ... LIMIT is the classic leaderboard query behind games, shops and feeds. 4 building blocks make up Put the rows in order, then cut
Press play: all six rows are read, sorted by score from high to low, then trimmed to the top 3.
SELECT name, score
FROM students
ORDER BY score DESC
LIMIT 3;
students
| id | name | score | city |
|---|
| 1 | Ava | 92 | Oslo |
| 2 | Ben | 82 | Oslo |
| 3 | Chloe | 85 | Lima |
| 4 | Dev | 68 | Lima |
| 5 | Emma | 95 | Oslo |
| 6 | Finn | 88 | Paris |
| name | score |
|---|
| press play — rows will appear here |
Step 1/5FROM hands the engine all six rows of students — in storage order, not ranked yet.
The engine always finishes the sort before applying any limit.
process decision input / output start / end Flip the direction and cut. Which two names come back?
Your predicted output — one line per console line
Hint: ASC sorts smallest first — and LIMIT 2 acts after the sort, on the sorted list.
Click a keyword, then click its job to lock the pair.
5 conceptsTap a card to expand ?ORDER BY scoredrops into place
?DESCdrops into place
?ASCdrops into place
?LIMIT 5drops into place
?Top-N querydrops into place
01ORDER BY column sorts the result; DESC flips to biggest-first.
02ASC is the default — you rarely need to write it.
03LIMIT cuts the result AFTER sorting, keeping the first N rows.
04ORDER BY ... LIMIT builds leaderboards: highest scores, newest posts, cheapest items.
05No ORDER BY? Then row order is whatever the engine felt like.