Master programming syntax, solve interactive challenges & run live code in Syntaxis Explore Arena →
Reference / SQL

DENSE_RANK()

Returns the rank of each row within a partition of a result set, without gaps in the ranking values for tied records.

Syntax
DENSE_RANK() OVER ([PARTITION BY partition_col] ORDER BY sort_col [ASC|DESC])

Parameters

NameTypeDescription
PARTITION BYclauseDivides rows into groups.
ORDER BYclauseSpecifies the ranking order.

Examples

Top 3 salaries per department
WITH ranked AS (
  SELECT name, department, salary,
         DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) as rank
  FROM employees
)
SELECT * FROM ranked WHERE rank <= 3;
Unlike RANK(), DENSE_RANK() does not skip rank numbers after duplicate values.