TopicsCheat sheetLessons
Lurndo › Topics › Ranking with RANK and DENSE_RANK

Learn SQL: Ranking with RANK and DENSE_RANK

Same time

In the hilly stage to Galmaarden the riders crossed the line in small groups. Whoever finishes together gets the same time. "Then they share their place in the time ranking too," says Griet. `ROW_NUMBER` knows no shared places. Time for something better.

Shared places

RANK() works like ROW_NUMBER(), but rows with the same value get the same number. After that it skips places: after three riders in 2nd place comes 5th place.

Here is the time behind in stage 3, in seconds. The WHERE runs first, so only the riders of that stage get ranked.

SELECT r.name, res.time_behind, RANK() OVER (ORDER BY res.time_behind) AS rnk FROM results res JOIN riders r ON r.id = res.rider_id WHERE res.stage_id = 3
Result
nametime_behindrnk
Wout De Clercq01
Tibo Lauwers422
Jesper Kuipers422
Louis Garnier422
Arne Van Hecke945
Kasper Holm945
Ruben de Graaf945
Jens Moreels1628

Three ways of counting

Side by side, for stage 4. ROW_NUMBER always gives different numbers, even for equal times: who goes first is then SQLite's own choice. RANK shares places and skips afterwards. DENSE_RANK shares too, but skips nothing: after three 2s comes 3.

SELECT rider_id, time_behind,
ROW_NUMBER() OVER (ORDER BY time_behind) AS row_num,
RANK() OVER (ORDER BY time_behind) AS rnk,
DENSE_RANK() OVER (ORDER BY time_behind) AS dense
FROM results WHERE stage_id = 4
Result
rider_idtime_behindrow_numrnkdense
110111
218222
1038333
1338433
965554
373665
873765
1273865

Ranking after GROUP BY

Window functions run after GROUP BY. That is why you can rank on a total: RANK() OVER (ORDER BY SUM(...) DESC).

Here is the team classification on points: first add up per team, then rank.

SELECT t.name AS team, SUM(res.points) AS points,
RANK() OVER (ORDER BY SUM(res.points) DESC) AS place
FROM results res
JOIN riders r ON r.id = res.rider_id
JOIN teams t ON t.id = r.team_id
GROUP BY t.id, t.name
Result
teampointsplace
Polderwind941
Équipe Nord722
Flandrien Pro573
Pajot Cycling414

Try it yourself

Build the riders' points classification. Show the name, the total points and the place. Riders with equal points share the place, and the next place is skipped.

Ready for more?

The full lesson has 5 exercises, from easy to challenging, with hints, points and a streak you can build.

Start the lesson
← PreviousA number for every rowNext →Per team, per stage
SQL topicsCheat sheetExcelLeaderboardAccountPrivacyNederlands