Learn SQL
-- windows.rank_denserank

RANK vs DENSE_RANK vs ROW_NUMBER in SQL

Tie semantics decide — state them, don’t just name them

--concept

ROW_NUMBER assigns unique 1..N (ties broken arbitrarily — pagination). RANK gives ties the same number with gaps after (1,1,3 — leaderboards with skipped places). DENSE_RANK gives ties the same number with no gaps (1,1,2 — Nth-salary). Nth-highest wants DENSE_RANK; deterministic pagination wants ROW_NUMBER + id tiebreak.

--how it works
1

ROW_NUMBER uniques

1..N with arbitrary tie-break (+ id for determinism). Pagination.

2

RANK gaps

Ties share, places skip (1,1,3). Leaderboard places.

3

DENSE_RANK packs

Ties share, no gaps (1,1,2). Nth-distinct salary.

4

PARTITION restarts

Per-group numbering; top-N per group via CTE + rn <= N outside.

--visualization
namesalaryrow_numberrankdense_rank
Cynthia91000111
Alice85000222
Farid73000333
Stage 1 / 1
Without ties all three agree — duplicate a pay to see them diverge.
--example
query.sql
1SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rnk, DENSE_RANK() OVER (ORDER BY salary DESC) AS drnk FROM employees ORDER BY salary DESC LIMIT 3;
Result
namesalaryrnkdrnk
Cynthia9100011
Alice8500022
Farid7300033

No ties in the top three, so all three functions agree here. Duplicate a salary and RANK would skip (1,1,3) while DENSE_RANK would not (1,1,2) — that divergence is the interview answer.

--variants

Three functions side by side

No ties here — all agree. Duplicate a pay to split them.

query.sql
1SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn, RANK() OVER (ORDER BY salary DESC) AS rnk, DENSE_RANK() OVER (ORDER BY salary DESC) AS drnk FROM employees ORDER BY salary DESC LIMIT 3;
Result
namesalaryrnrnkdrnk
Cynthia91000111
Alice85000222
Farid73000333
1Window orders by salary descending once.
2Each function stamps its tie policy.
3Add a second 91k earner: rn splits 1/2, rnk gives 1,1,3, drnk 1,1,2.

Top-1 per department

Partition-rank-filter mantra.

query.sql
1SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn FROM employees) t WHERE rn = 1;
Result
namedepartmentsalary
CynthiaEngineering91000
ElenaSales58000
DavidMarketing47000
1PARTITION slices per team; ORDER ranks inside.
2Outer filter keeps the head of each slice.
3rn <= 3 generalizes to top-3 leaderboards.
--key facts
ROW_NUMBERunique — pagination
RANKties + gaps — leaderboard places
DENSE_RANKties, no gaps — Nth distinct
--use cases
Leaderboards with stated tie policy
Dedup: latest row per user (ORDER BY created_at DESC, rn=1)
--under the hood
Ranking needs a sort per partition key — composite (partition, order) indexes feed it without sorting.
Pagination must add id tiebreak or rows drift between pages.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.