Skip to content

DENSE_RANK

DENSE_RANK() OVER ([PARTITION BY partition_expression] ORDER BY sort_expression [ASC | DESC])

Section titled “DENSE_RANK() OVER ([PARTITION BY partition_expression] ORDER BY sort_expression [ASC | DESC])”
SELECT name, salary, DENSE_RANK() OVER (ORDER BY salary DESC) salary_rank
FROM employees;
| name | salary | salary_rank |
|-------|--------|-------------|
| John | 70000 | 1 |
| Alice | 60000 | 2 |
| Bob | 60000 | 2 |
| David | 50000 | 3 |
| Carol | 40000 | 4 |

In the example, the DENSE_RANK() function is applied to the salary column in the employees table, which ranks employees based on their salary in descending order. Employees with same salary get the same rank. Unlike RANK(), DENSE_RANK() does not skip rank in case of ties, hence the rank follows orderly as 1, 2, 2, 3, 4, not 1, 2, 2, 4, 5.