SQL · Level 3 Window functions

SQL RANK() and DENSE_RANK()

Give tied players the same place on a leaderboard, and know the difference between ROW_NUMBER(), RANK() and DENSE_RANK().

  • ROW_NUMBER() 1, 2, 3
  • RANK() 1, 1, 3
  • DENSE_RANK() 1, 1, 2

For tied scores, ROW_NUMBER() gives 1, 2, 3, RANK() gives 1, 1, 3 and DENSE_RANK() gives 1, 1, 2. Trap: ROW_NUMBER() silently picks a winner among equal scores, which is unfair on a leaderboard.

SELECT name, score,
       DENSE_RANK() OVER (ORDER BY score DESC) AS level
FROM players;   -- 100 → 1, 100 → 1, 90 → 2

Return each player's name, score and place, where tied players share a place and the next place is skipped (1, 1, 3), best first and ties by name. In the database below, Bo and Cy share first place, so Ana is third.

Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.

players 4 rows
idINTEGERnameTEXTscoreINTEGER
1Ana90
2Bo100
3Cy100
4Di70
solution.sql
Test Suites Pending
Output