SQL QuestSQL Interview Questions › Window Functions

Top 3 Salary Tiers (DENSE_RANK)

MediumFreeQuerying BasicsWindow FunctionsSubqueries & CTEs

Comp planning wants every employee in the top 3 distinct salary tiers — multiple people can share a tier, and tier 4+ is filtered out.

Use DENSE_RANK() OVER (ORDER BY salary DESC) to assign a tier per employee, then keep only tier <= 3. Show name, department, salary, tier. Order by tier, then name.

Why DENSE_RANK here: if three people earn $95K and you ask for "the top 3 *tiers*", you want all three of them plus everyone at the next two distinct salaries. RANK would skip from 1 → 4 after a 3-way tie and you'd lose the next tier. ROW_NUMBER would break ties arbitrarily and only show 3 rows. DENSE_RANK is exactly the right tool for tier semantics.

Solve it in the browser editor →

Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".

Schema

employees

emp_idnamedepartmentpositionsalaryhire_datemanager_idperformance_rating
1Alice JohnsonEngineeringSenior Developer950002019-03-1554.5
2Bob SmithEngineeringDeveloper750002020-06-0113.8
3Carol WilliamsMarketingMarketing Manager850002018-09-20NULL4.2

Expected output: Tier 1: 95,95 — Tier 2: 90 — Tier 3: 85

Hint

The hint for this one spells out most of the query, so it stays in the editor. Reach for SELECT, Window Functions, DENSE_RANK, and open the hint there if you stall.

Concepts

SELECT Window Functions DENSE_RANK CTE

Practise the topic: SQL practice questions · Window function practice · CTE practice · Ranking function practice

In these company practice sets

NVIDIA · LinkedIn

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Window Functions: ROW_NUMBERMedium · FreeMonth-over-Month Customer GrowthMedium · FreeRANK vs DENSE_RANK Side-by-SideMedium · FreeMost Recent Order Per Customer (ROW_NUMBER)Medium · FreeSecond-Highest Earner Per Department (ROW_NUMBER)Medium · FreeSalary vs Department Average (PARTITION BY)Medium · Free

Where would this cost you points in an interview?

Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.

Take the readiness test