DENSE_RANK() behaves like RANK() except it never leaves a gap in
the ranking sequence after a tie.
Ordered values: [10, 20, 20, 40]
RANK(): [1, 2, 2, 4]
DENSE_RANK(): [1, 2, 2, 3]
RANK() and DENSE_RANK() based on business
requirements.01_ROW_NUMBER02_RANKSELECT
column_a,
DENSE_RANK() OVER (ORDER BY sort_column) AS dense_rank_value
FROM table_name;
employes
See 03_dense_rank.sql.
| Domain | Scenario |
|---|---|
| Finance | Assign consecutive risk tiers to loan applicants |
| Retail | Assign consecutive pricing tiers to products |
| HR | Assign consecutive seniority bands per manager group |
DENSE_RANK() when the business actually wants gap-aware
standings (use RANK() instead).DENSE_RANK() and ROW_NUMBER() are interchangeable when
there are no ties in the sample data – they diverge as soon as a tie
appears.DENSE_RANK() maintains an internal counter that increments only when
the ORDER BY value changes from the previous row – this is the single
mechanical difference from RANK(), which instead increments by the
number of rows seen so far.
manager_id.emp_id).RANK() and DENSE_RANK() side by side.Beginner
20 minutes
DENSE_RANK() output on tied data without running the query.DENSE_RANK() over RANK().← Previous: 02_RANK
↑ Module README
→ Next: 04_PARTITION_BY