Top Metric Values
An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- easy
- Seniority
- L3
- Asked in
- Siemens
Problem
We're putting together a leaderboard of top performance scores from employee metrics, where a score that shows up more than once still counts only once. List the 5 highest such scores, ordered from the top score down to the lowest.
The 5 highest numbers. No duplicates.
Tables
employee_metrics
Columns: metric_id INTEGER, department TEXT, metric_name TEXT, metric_value REAL, fiscal_quarter TEXT, fiscal_year INTEGER
| metric_id | department | metric_name | metric_value | fiscal_quarter | fiscal_year |
|---|---|---|---|---|---|
| 141 | Product | attrition_rate | 7.3 | Q2 | 2025 |
| 182 | Design | avg_tenure_months | 14.6 | Q3 | 2026 |
| 223 | Marketing | satisfaction_score | 21.9 | Q4 | 2024 |
| 264 | Sales | training_hours | 29.2 | Q1 | 2025 |
| 305 | HR | promotions | 36.5 | Q2 | 2026 |
| 346 | Finance | open_positions | 43.8 | Q3 | 2024 |
| 387 | Legal | time_to_hire_days | 51.1 | Q4 | 2025 |
| 428 | Operations | diversity_ratio | 58.4 | Q1 | 2026 |
| 469 | Data | remote_percentage | 65.7 | Q2 | 2024 |
| 510 | Engineering | headcount | 73 | Q3 | 2025 |
Expected output
| metric_value |
|---|
| 99.3 |
| 98.6 |
| 97.1 |
| 96.4 |
| 94.9 |
Worked solution and explanation
What this really is
This is a top-N over values dressed up as a leaderboard. Every score in employee_metrics sits on exactly 2 rows, so the first 5 rows by metric_value hold only 3 different numbers: 99.3, 99.3, 98.6, 98.6, 97.1. Anyone can write ORDER BY metric_value DESC LIMIT 5. What separates candidates is collapsing repeats before the cut, because LIMIT counts rows, not scores. Miss it and 96.4 and 94.9 never make the board.
Getting there
Step 1: Decide what one entry on the board is
The statement says a repeated score counts once, so one entry on the board is one value. Project only metric_value. Bring metric_id along and every row is unique again, because each row carries its own id.
Step 2: Collapse repeats with DISTINCT before counting
SELECT DISTINCT metric_value folds the 200 rows into 95 values: 94 scores plus one NULL. The dedup happens on the projected set before LIMIT does its counting, so the 5 rows that survive are 5 different numbers.
Step 3: Sort high to low, then take 5
ORDER BY metric_value DESC puts 99.3 first and LIMIT 5 stops at 94.9. The cut falls between 2 different values, so no tiebreaker decides who gets the last slot.
The solution
SELECT DISTINCT metric_value
FROM employee_metrics
ORDER BY metric_value DESC
LIMIT 5Dedup on metric_value, sort it high to low, keep 5.
- Rows, then cut
ORDER BY metric_value DESC LIMIT 5returns 99.3, 99.3, 98.6, 98.6, 97.1: 5 rows, 3 scores.- Values, then cut
SELECT DISTINCT metric_value ... LIMIT 5returns 99.3, 98.6, 97.1, 96.4, 94.9: the board the statement asks for.
SELECT metric_value
FROM employee_metrics
ORDER BY metric_value DESC
LIMIT 5Looks right and returns 99.3 twice and 98.6 twice: wrong on this data.
Common follow-up questions
- The board now needs the `department` next to each score, and one score appears in 2 departments. What changes? (Tests whether adding a column changes what one entry is.)
- How would you return every row that holds one of the top 5 scores? (Tests turning the value list into a filter, for example a dense ordering over `metric_value`.)
- On an engine that sorts `NULL` first under `DESC`, what does this query return, and how do you fix it? (Tests NULL ordering portability.)