DENSE_RANK()

SQL · Window Functions

Theory

DENSE_RANK() works like RANK(): tied values share the same number. The difference is that it never leaves gaps. After two people tie at rank 2, the next person is rank 3, not 4. In other words, DENSE_RANK counts distinct values: rank n means 'the n-th highest distinct value'.

That makes it the natural answer to the classic interview question 'find the second (or Nth) highest salary'. Compute DENSE_RANK() OVER (ORDER BY salary DESC) and keep rows where it equals 2; ties at the top do not push the second salary out of reach. The three ranking functions only differ on ties. On 75000, 60000, 60000, 52000, 45000 they give: ROW_NUMBER 1,2,3,4,5; RANK 1,2,2,4,5; DENSE_RANK 1,2,2,3,4. Remembering that one line answers most interview questions about them.

Why we use it

Some questions are about distinct levels, not positions: the Nth highest salary, the top 3 price points, grade bands. DENSE_RANK gives consecutive numbers per distinct value so 'rank = N' always means the Nth distinct value.

When to use it

Use DENSE_RANK for 'Nth highest value' questions and when gaps would confuse users. Use RANK when the gap is meaningful (how many are ahead), and ROW_NUMBER when you need exactly one row per position.

How it works

  1. Partition is sorted by the window ORDER BY
  2. First row gets rank 1
  3. Same value as previous row? Yes: Same rank. No: Previous rank + 1 (no gap).
  4. Repeat for every row in the partition
  5. Consecutive ranks: 1, 2, 2, 3

Syntax

DENSE_RANK() OVER (ORDER BY col)

Example

SELECT name, salary,
  DENSE_RANK() OVER (ORDER BY salary DESC) AS drnk
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic