Problems / Top Scores With Ties / Editorial
DENSE_RANK
DENSE_RANK() OVER (ORDER BY score DESC)
score_rank <= 3
The three ranking functions encode three different *contracts* for "top N": ROW_NUMBER = exactly N rows (ties broken arbitrarily — unfair), RANK = competition style (ties share a rank, next value skips — 'top 3 ranks' can miss the 3rd-best score), DENSE_RANK = top N distinct values, ties included. Interviews love this distinction because the query shapes are identical and only the semantics differ — with a 2-way tie at the top and a 3-way tie at rank 3, this dataset returns 6, and only DENSE_RANK gets it right.
ROW_NUMBER
RANK
Solve Top Scores With Ties yourself →