Rank Scores

medium sorting database ranking

Problem

Write a SQL query to rank the scores. If two scores tie, they have the same rank, and the next rank is exactly one larger (DENSE_RANK).

SQL: SELECT score, DENSE_RANK() OVER (ORDER BY score DESC) AS 'rank' FROM Scores;

Inputscores = [3.5, 3.65, 4.0, 3.85, 4.0, 3.65]
Output[(4.0,1),(4.0,1),(3.85,2),(3.65,3),(3.65,3),(3.5,4)]

SELECT score, DENSE_RANK() OVER (ORDER BY score DESC) AS 'rank'
FROM Scores;
Time: O(n log n) Space: O(n)