Beginner

615. JOIN with DENSE_RANK

Learn how DENSE_RANK can rank JOINed records without gaps after ties.

📘 Lesson 615 of 728 ⏱ 14 min 🎯 Advanced

DENSE_RANK assigns the same rank to tied values but does not leave gaps in the ranking sequence.

It is useful when ranking business results and you want consecutive ranking positions despite tied values.

Rank employees by salary within each department without ranking gaps.

SQL
SELECT
    e.EmployeeName,
    d.DepartmentName,
    e.Salary,
    DENSE_RANK() OVER (
        PARTITION BY e.DepartmentID
        ORDER BY e.Salary DESC
    ) AS SalaryRank
FROM Employees e
INNER JOIN Departments d
    ON e.DepartmentID = d.DepartmentID;

Used in employee rankings, sales leaderboards, product rankings, and performance analysis.

  • DENSE_RANK assigns rankings within groups.
  • Tied rows receive the same rank.
  • Ranking continues without gaps.
  • PARTITION BY separates groups.
  • Useful for business ranking scenarios.
What is DENSE_RANK?

DENSE_RANK assigns the same rank to tied values and continues with the next consecutive rank.

How does DENSE_RANK differ from RANK?

RANK can leave gaps after ties, while DENSE_RANK does not.

Have you finished this lesson?

Introduction to SQL – All Lessons