SQL-Engineering-Handbook

DENSE_RANK()

DENSE_RANK diagram showing no gaps after ties

Overview

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]

Learning Objectives

Prerequisites

Syntax

SELECT
    column_a,
    DENSE_RANK() OVER (ORDER BY sort_column) AS dense_rank_value
FROM table_name;

Dataset Used

employes

Examples

See 03_dense_rank.sql.

Real World Applications

Business Use Cases

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

Common Mistakes

Best Practices

Engineering Notes

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.

Practice Questions

  1. Assign dense rank to employees ordered by manager_id.
  2. Show employee name and dense rank (ordered by emp_id).
  3. Show employees with dense rank ≤ 3.
  4. Compare RANK() and DENSE_RANK() side by side.
  5. Show the employee(s) with dense rank = 1.

Difficulty

Beginner

Estimated Time

20 minutes

Learning Outcomes

Next Topic

04_PARTITION_BY


Lesson Navigation

← Previous: 02_RANKModule README → Next: 04_PARTITION_BY