Sort and limit in SQL
ORDER BY sorts the result; LIMIT trims it to the top rows.
SELECT
name,
score
FROM players
ORDER BY score DESC, name ASC
LIMIT 10;
How it works
DESCsorts high to low,ASC(the default) low to high.- Sorting by a second column breaks ties in the first.
LIMITruns last, after sorting, so you get a real top ten.
Keywords and builtins used here
ASCBYDESCFROMLIMITORDERSELECT
The run, in numbers
- Lines
- 6
- Characters to type
- 71
- Tokens
- 16
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 13 seconds.
Step 1 of 3 in Sorting & paging, step 6 of 23 in Language basics.