# Max Value Per Location

> Every location has a peak.

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

Domain: SQL · Difficulty: easy · Seniority: L5

## Problem

HR wants to see the peak performance metric within each department. Show each department alongside its highest recorded metric value.

## Worked solution and explanation

### Why this problem exists in real interviews

The interviewer wants to see you apply grouping to employee_metrics.department while accounting for the distribution of metric_value. This surfaces as a fundamentals check because small logic errors produce results that look correct at a glance.

---

### Break down the requirements

#### Step 1: Aggregate with MAX

Group by the output grain and apply `MAX()` to compute the metric. The `GROUP BY` must match exactly what the output needs: one row per group key.

#### Step 2: Order the final output

Apply `ORDER BY` as specified to produce the expected row sequence. When tied values exist, add a secondary sort column for determinism.

---

### The solution

**MAX aggregate per department**

```sql
SELECT department, MAX(metric_value) AS max_value
FROM employee_metrics
GROUP BY department
ORDER BY max_value DESC
```

> **Cost Analysis**
>
> The query scans 8K rows from `employee_metrics`. CTEs in most engines are optimization fences. For production workloads, consider inlining or materializing the intermediate results.

> **Interviewers Watch For**
>
> Breaking complex logic into named CTEs shows the interviewer you prioritize readability and debuggability.

> **Common Pitfall**
>
> Returning more columns than the prompt asks for can trigger a "wrong schema" failure in automated grading. Match the output specification exactly.

---

## Common follow-up questions

- If employee_metrics.metric_id could contain unexpected NULL values, how would your query behave? _(Tests NULL awareness even when the schema does not currently allow NULLs in metric_id.)_
- How would you verify that your aggregation on employee_metrics.metric_id is not double-counting due to duplicate rows? _(Tests data quality awareness and deduplication strategies.)_
- What index would you add to employee_metrics to avoid a full table scan when filtering or sorting by metric_id? _(Tests practical indexing decisions for numeric filter columns.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/max_value_per_location)
- [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). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.