SQL Quest › SQL Interview Questions › Window Functions
Top 3 Salary Tiers (DENSE_RANK)
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_id | name | department | position | salary | hire_date | manager_id | performance_rating |
|---|---|---|---|---|---|---|---|
| 1 | Alice Johnson | Engineering | Senior Developer | 95000 | 2019-03-15 | 5 | 4.5 |
| 2 | Bob Smith | Engineering | Developer | 75000 | 2020-06-01 | 1 | 3.8 |
| 3 | Carol Williams | Marketing | Marketing Manager | 85000 | 2018-09-20 | NULL | 4.2 |
Expected output: Tier 1: 95,95 — Tier 2: 90 — Tier 3: 85
Hint
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
A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.
Related questions
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