Sorting with ORDER BY and LIMIT

Here is something that surprises people: without instructions, a database does not promise any order. Rows often come back in the order they were added, but that is luck, not a rule. If order matters, say so with ORDER BY:

SELECT gamertag, score FROM leaderboard ORDER BY score;

That sorts from smallest to biggest, which is called ascending (ASC, the default). Leaderboards want the biggest first, so add DESC for descending:

SELECT gamertag, score FROM leaderboard ORDER BY score DESC;

Text sorts alphabetically, and dates stored as '2026-03-14' sort correctly too, which is exactly why that format is popular.

What about ties? You can list more than one column. The second one only matters when the first one is equal:

ORDER BY level DESC, gamertag ASC

means "highest level first, and players on the same level in alphabetical order".

To keep only the first few rows, add LIMIT at the very end:

SELECT gamertag, score FROM leaderboard ORDER BY score DESC LIMIT 3;

That is your podium. LIMIT without ORDER BY is legal but kind of pointless: you get "some 3 rows", and which ones is up to the database's mood.

The full order of the parts so far is:

SELECT ... FROM ... WHERE ... ORDER BY ... LIMIT ...

The database filters first (WHERE), then sorts what is left, then chops it with LIMIT. So WHERE region = 'EU' ORDER BY score DESC LIMIT 1 gives the best EU player, not the best player overall who happens to be from the EU.

Tables in this lesson
leaderboard 8 rows
idgamertagregionlevelscore
1NoScopeNanEU4218200
2xX_Toast_XxNA3715900
3LagIsMyFaultOCE4221050
4QuietKeyboardEU299400
5BrbSnacksNA3716800
6MumSaysBedtimeEU122300
7ZeroDeathsOCE5025500
8AFKAlexNA298800
Show the SQL that built them
CREATE TABLE leaderboard (id INTEGER PRIMARY KEY, gamertag TEXT, region TEXT, level INTEGER, score INTEGER);
INSERT INTO leaderboard VALUES
 (1, 'NoScopeNan', 'EU', 42, 18200),
 (2, 'xX_Toast_Xx', 'NA', 37, 15900),
 (3, 'LagIsMyFault', 'OCE', 42, 21050),
 (4, 'QuietKeyboard', 'EU', 29, 9400),
 (5, 'BrbSnacks', 'NA', 37, 16800),
 (6, 'MumSaysBedtime', 'EU', 12, 2300),
 (7, 'ZeroDeaths', 'OCE', 50, 25500),
 (8, 'AFKAlex', 'NA', 29, 8800);

Try it yourself

Edit it. Break it. Run it again.
-- The podium: top 3 scores
SELECT gamertag, score
FROM leaderboard
ORDER BY score DESC
LIMIT 3;
Ctrl/Cmd + Enter runs. Esc, then Tab, leaves the editor.

Your turn

Type it yourself. That is the whole trick.

1Level ranking

Return gamertag and level for every player, sorted by level from highest to lowest. Players on the same level should be in alphabetical order of gamertag.

SELECT gamertag, level
FROM leaderboard
ORDER BY level;
Ctrl/Cmd + Enter runs. Esc, then Tab, leaves the editor.

2Best in NA

Return the gamertag and score of the two highest scoring players in the NA region, highest first.

SELECT gamertag, score
FROM leaderboard
ORDER BY score DESC
LIMIT 2;
Ctrl/Cmd + Enter runs. Esc, then Tab, leaves the editor.

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.