Learn SQL
-- subqueries.nth_salary

Nth Highest Salary (Classic) in SQL

DENSE_RANK for ties; correlated COUNT fallback

--concept

Dense-rank window (DENSE_RANK() OVER (ORDER BY salary DESC), filter = N) handles ties correctly and reads clearly. Correlated COUNT (salaries greater than me = N-1) is the no-window fallback for old engines. LIMIT 1 OFFSET N-1 breaks on ties — two people sharing second place both deserve rank 2.

--how it works
1

Rank distinct salaries

DENSE_RANK() OVER (ORDER BY salary DESC) — ties share, no gaps. The Nth-distinct engine.

2

Filter outside

Windows evaluate after WHERE — wrap in subquery/CTE, filter r = N outside (or QUALIFY where supported).

3

Know the siblings

ROW_NUMBER breaks ties arbitrarily; RANK gaps (1,1,3). LIMIT/OFFSET breaks on ties entirely.

4

Fallback without windows

Correlated COUNT (salaries greater than me = N-1) works on old engines — O(n²)-ish but portable.

--visualization
r=1Cynthia 91,000r=2Alice 85,000r=3Farid 73,000r=4Elena 58,000
Stage 1 / 1
DENSE_RANK = 2: Alice (85,000). LIMIT/OFFSET would break on ties; DENSE_RANK shares ranks without gaps.
--example
query.sql
1SELECT * FROM (SELECT *, DENSE_RANK() OVER (ORDER BY salary DESC) AS r FROM employees) t WHERE r = 2;
Result
namesalaryr
Alice850002

Salaries ranked distinct: 91k→1, 85k→2, 73k→3… Alice alone holds rank 2. With a tie at 91k both would share rank 1 and Alice would still be rank 2 — LIMIT/OFFSET would have missed that.

--variants

Second-highest distinct

Ties handled — both 91k holders share rank 1.

query.sql
1SELECT * FROM (SELECT *, DENSE_RANK() OVER (ORDER BY salary DESC) AS r FROM employees) t WHERE r = 2;
Result
namesalaryr
Alice850002
1Window orders all salaries descending and densifies.
2Outer filter keeps rank-2 rows only.
3Duplicate top pay would keep both at rank 1, Alice still rank 2.

No-window fallback

Correlated COUNT for MySQL 5 / old SQLite.

query.sql
1SELECT name, salary FROM employees e1 WHERE (SELECT COUNT(*) FROM employees e2 WHERE e2.salary > e1.salary) = 1;
Result
namesalary
Alice85000
1Per row, count strictly-greater salaries.
2Exactly one greater means second-highest.
3Correct but per-row — windows are the single-pass answer.
--key facts
DENSE_RANKties share, no gaps — Nth distinct salary
LIMIT trapOFFSET breaks on ties
Filter outsidewindows evaluate after WHERE
--use cases
Leaderboards, comp bands, latest-per-user (ROW_NUMBER variant)
Legacy engines without window support
--under the hood
DENSE_RANK needs one sort — O(n log n); correlated COUNT is O(n²) probes without decorrelation.
ROW_NUMBER-partition-filter generalizes to dedup (rn=1 keeps latest per key).

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.