SQL · Module 06: Window Functions (SQL Interview Superpower)Tutorial 17 of 24
All Tutorials·~1.5 hrs

6.2 Ranking: ROW_NUMBER() vs RANK() vs DENSE_RANK() & Top-N Per Group

Master the #1 most-asked SQL interview pattern: exact differences between ROW_NUMBER(), RANK(), and DENSE_RANK() on ties, Top-3 salaries per department, and deduplicating dirty tables.

01 Intuition: Plain-English mental model
02 Visuals: Step-by-step diagrams
03 Practice: Clean examples & tradeoffs

1. ROW_NUMBER() vs RANK() vs DENSE_RANK() — Memorize This Table!

Suppose 4 students have scores: 96, 92, 92, 88 (notice the tie at 92!). Look at how the 3 ranking functions behave:

StudentScoreROW_NUMBER()RANK()DENSE_RANK()
Meera96111
Aarav92222
Riya92322
Kabir8844 (skips 3!)3 (no gap!)
  • ROW_NUMBER(): Always assigns unique sequential integers 1, 2, 3, 4 — breaks ties arbitrarily unless you add a tie-breaker in ORDER BY! Best for deduplication & strict pagination.
  • RANK(): Olympic medal ranking — tied rows share the same rank (2, 2), and the next rank skips (4).
  • DENSE_RANK(): Tied rows share the same rank (2, 2), and the next rank has no gap (3). Best for "2nd / 3rd highest distinct salary"!

2. The #1 FAANG SQL Interview Pattern: Top-N Per Group!

"Find the Top 2 highest-paid employees in each department (including ties)."
Pattern: CTE + DENSE_RANK() OVER (PARTITION BY dept ORDER BY salary DESC) + outer WHERE rnk <= 2:

SQL
WITH ranked_employees AS (
    SELECT
        dept,
        name,
        salary,
        DENSE_RANK() OVER (
            PARTITION BY dept
            ORDER BY salary DESC
        ) AS salary_rank
    FROM employees
)
SELECT dept, name, salary, salary_rank
FROM ranked_employees
WHERE salary_rank <= 2
ORDER BY dept ASC, salary_rank ASC;

Watch ROW_NUMBER() vs DENSE_RANK() Execute Live

Tracing Top-2 Per Department with DENSE_RANK()
Interactive SQL Execution Tracer · Step 1 of 3 (1. Load employees)
1WITH ranked AS (
2 SELECT dept, name, salary,
3 DENSE_RANK() OVER (PARTITION BY dept ORDER BY salary DESC) AS rnk
4→ FROM employees
5) SELECT dept, name, salary, rnk FROM ranked WHERE rnk <= 2;
Live Table State · 1. Load employees5 rows
deptnamesalary
EngKabir160k
EngAarav140k
EngDev140k
EngZoya110k
SalesRiya100k
What the DB engine just did: Notice Aarav and Dev are tied at 140k in Eng.

3. Classic Production Pattern: Keep Only the Latest Record Per User (Deduplication)

Suppose an event log has multiple rows per user_id and you only want each user's most recent login:

SQL
WITH latest_logins AS (
    SELECT
        user_id,
        ip_address,
        logged_in_at,
        ROW_NUMBER() OVER (
            PARTITION BY user_id
            ORDER BY logged_in_at DESC
        ) AS rn
    FROM login_events
)
SELECT user_id, ip_address, logged_in_at
FROM latest_logins
WHERE rn = 1;

4. Quick Check Your Understanding

Module 6.2 Quick Quiz

Q1.Given salaries [100, 100, 80, 60], which function assigns the rank 2 to salary 80?
Q2.To pick exactly ONE latest order per customer (even if two orders have the exact same timestamp), which function should you use?

Keep Exploring: Related Lessons & Tools

All Courses →