Rank Scores (Dense Rank)

Medium SQLWindowDENSE_RANK

Problem

Schema: Scores(id INT, score DECIMAL). Return each score and its rank. Scores tie → same rank; no gaps after a tie (dense rank). Output ordered by score desc.

Example 1 Input: scores [3.50, 3.65, 3.65, 3.85, 4.00] Output: (4.00,1) (3.85,2) (3.65,3) (3.65,3) (3.50,4)

Constraints

Approach — Core Patterns

This is a Core Patterns problem. The idea: pick the data structure best matched to the constraints and solve it in one clean pass. Work through the reference code below line by line, then re-derive it yourself in the editor — that's how the pattern sticks.

Complexity: see the walkthrough below.

Solution code

SQL

SELECT score, DENSE_RANK() OVER (ORDER BY score DESC) AS rnk
FROM Scores
ORDER BY score DESC;

Practice it

Reading a solution isn't the same as being able to write it under pressure. Open this problem in the in-browser editor, hide the solution, and solve it from scratch — your code runs against real test cases instantly.

Solve Rank Scores (Dense Rank) interactively → ← All solutions