The Upper Rungs

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
L5

Problem

A compensation team is setting reference points for a new salary-band ladder, where a value that appears more than once still counts only once. Return the five highest values from the employee metrics table, highest first.

The top rungs set the ceiling for everyone below.

Tables

employee_metrics

Columns: metric_id INTEGER, metric_value REAL, department TEXT, metric_name TEXT, fiscal_quarter TEXT, fiscal_year INTEGER

employee_metrics: 10 of 200 rows
metric_iddepartmentmetric_namemetric_valuefiscal_quarterfiscal_year
141Productattrition_rate7.3Q22025
182Designavg_tenure_months14.6Q32026
223Marketingsatisfaction_score21.9Q42024
264Salestraining_hours29.2Q12025
305HRpromotions36.5Q22026
346Financeopen_positions43.8Q32024
387Legaltime_to_hire_days51.1Q42025
428Operationsdiversity_ratio58.4Q12026
469Dataremote_percentage65.7Q22024
510Engineeringheadcount73Q32025

Expected output

First 5 of 5 rows
benchmark_value
99.3
98.6
97.1
96.4
94.9

Worked solution and explanation

What this really tests

This is a top-N over values wearing a compensation costume. Anyone can sort high to low and take five off the top. What separates candidates is noticing the clause that says a repeated value counts only once: you want the five highest VALUES, not the five highest ROWS. On this table every value is stored in two rows, so skip the dedupe and the ladder comes back with three real rungs and two echoes, without a single error being raised.


Building it up

Step 1: Collapse repeats first

SELECT DISTINCT metric_value collapses repeated values so an identical peak posted twice is treated as one level, not two. This is the line the prompt is really probing.

Step 2: Sort highest first

ORDER BY metric_value DESC puts the largest value on top. Descending is the whole point: the compensation team wants the ceiling, not the floor.

Step 3: Take the top five

LIMIT 5 keeps only the top five. If the table held fewer than five distinct values, LIMIT would quietly return whatever exists, which is exactly the desired behavior.


The solution

Five highest distinct values, highest first
SELECT DISTINCT metric_value AS benchmark_value
FROM employee_metrics
ORDER BY metric_value DESC
LIMIT 5
Without DISTINCT
ORDER BY metric_value DESC LIMIT 5 over raw rows. Each value sits in two rows, so the result is 99.3, 99.3, 98.6, 98.6, 97.1: only three real levels.
With DISTINCT
SELECT DISTINCT first, then order and limit. The result is 99.3, 98.6, 97.1, 96.4, 94.9: five genuinely different benchmark levels.

Common follow-up questions