1.ROW_NUMBER vs RANK vs DENSE_RANK
AdvancedFor every student, return student, score, and three columns ranking them by score from high to low across all classes: row_num (ROW_NUMBER), rnk (RANK) and dense_rnk (DENSE_RANK). Break ties in row_num by student name.
Show hint
Each function needs its own OVER (ORDER BY ...). Watch what happens to the numbers after a tie.
Show solution
SELECT student, score,
ROW_NUMBER() OVER (ORDER BY score DESC, student) AS row_num,
RANK() OVER (ORDER BY score DESC) AS rnk,
DENSE_RANK() OVER (ORDER BY score DESC) AS dense_rnk
FROM exam_scores
ORDER BY score DESC, student;student | score | row_num | rnk | dense_rnk
--------+-------+---------+-----+----------
Kiran | 95 | 1 | 1 | 1
Asha | 92 | 2 | 2 | 2
Meera | 92 | 3 | 2 | 2
Priya | 88 | 4 | 4 | 3
Sara | 88 | 5 | 4 | 3
Ravi | 85 | 6 | 6 | 4
John | 78 | 7 | 7 | 5
Dev | 70 | 8 | 8 | 6Asha and Meera both scored 92. ROW_NUMBER still gives them different numbers (2 and 3). RANK gives both 2 and then skips to 4. DENSE_RANK gives both 2 and continues with 3, with no gap.