RANK, DENSE_RANK and ROW_NUMBER
Ranking Functions
These three functions all assign a number to each row within a partition, but differ in how they handle ties.
SELECT name, score,
ROW_NUMBER() OVER (ORDER BY score DESC) AS row_num,
RANK() OVER (ORDER BY score DESC) AS rank,
DENSE_RANK() OVER (ORDER BY score DESC) AS dense_rank
FROM results;
| name | score | row_num | rank | dense_rank |
|---|---|---|---|---|
| Alice | 95 | 1 | 1 | 1 |
| Bob | 95 | 2 | 1 | 1 |
| Carol | 80 | 3 | 3 | 2 |