typestar

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

  1. DESC sorts high to low, ASC (the default) low to high.
  2. Sorting by a second column breaks ties in the first.
  3. LIMIT runs last, after sorting, so you get a real top ten.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Sorting & paging, step 6 of 23 in Language basics.

← Previous Next →