# The Upper Rungs

> The top rungs set the ceiling for everyone below.

Canonical URL: <https://datadriven.io/problems/the_upper_rungs>

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.

## Worked solution and explanation

### What this really tests

This is a top-N ranking wearing a compensation costume. Anyone can sort the values from high to low and take five off the top. The move that separates candidates is noticing the word that says a repeated value counts only once: you want the five highest VALUES, not the five highest ROWS. Skip the dedupe and a metric that two departments happen to share will eat a slot, and your salary ladder comes back one rung short 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 happens to hold fewer than five distinct values, LIMIT quietly returns whatever exists, which is exactly the desired behavior.

---

### The solution

**Five highest distinct values, highest first**

```sql
SELECT DISTINCT metric_value AS benchmark_value
FROM employee_metrics
ORDER BY metric_value DESC
LIMIT 5
```

> **Common pitfall**
>
> The failure that shows up in real submissions is dropping DISTINCT. Without it, if the highest value appears in three rows, all three come back and your five rungs collapse into three real levels plus two repeats. The result looks plausible and runs clean, which is exactly why it slips through.

**Without DISTINCT**

ORDER BY metric_value DESC LIMIT 5 over raw rows. If 73.0 appears three times, the result is 73.0, 73.0, 73.0, 65.7, 58.4: only three real levels.

**With DISTINCT**

SELECT DISTINCT first, then order and limit. The result is 73.0, 65.7, 58.4, 51.1, 43.8: five genuinely different benchmark levels.

> **Interviewers watch for**
>
> The tell interviewers look for is whether you react to the phrase about a value counting once. A candidate who reaches for DISTINCT unprompted, then sanity-checks the result against a duplicated peak, signals they read requirements for edge behavior rather than just pattern-matching to ORDER BY plus LIMIT.

> **Cost analysis**
>
> On 8,000 rows this scans a small table and sorts in memory, so no index is required at this size. At production scale, an index on metric_value lets the engine walk the top of the sorted order and stop early instead of sorting the full table, which is the classic top-N optimization.

---

## Common follow-up questions

- How would you return the five highest distinct values within each department instead of across the whole table? _(Tests generalizing top-N into a per-group ranking, which needs a window function.)_
- If metric_value could contain NULLs, where would they land in your ordering and how would you exclude them? _(Tests understanding of how DISTINCT interacts with NULL values in the ranked column.)_
- What index would let this query avoid sorting the entire table to find the top five? _(Tests awareness of the top-N index optimization versus a full sort.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_upper_rungs)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.