# Peak Metric Per Department

> Peak metrics for the quarterly deck.

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

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

HR is spotting which departments have top-performing individuals by looking at each department's peak metric value, sorted from highest peak to lowest.

## Worked solution and explanation

### Why this problem exists in real interviews

Querying employee_metrics for department data using grouping tests whether you can translate a business requirement into the right column references and filter sequence. It shows up as a fundamentals check to verify practical fluency.

---

### Break down the requirements

#### Step 1: Group by department

`GROUP BY department` collapses all metrics into one row per department.

#### Step 2: Select MAX metric value

`MAX(metric_value)` returns the peak value for each department.

#### Step 3: Order descending

`ORDER BY MAX(metric_value) DESC` surfaces the highest-performing departments first.

---

### The solution

**GROUP BY with MAX, ordered descending**

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

> **Cost Analysis**
>
> With `employee_metrics` (10,000 rows), this query scans a small dataset. No indexing is needed for this volume. At production scale, an index on the primary filter column would improve performance.

> **Interviewers Watch For**
>
> Interviewers evaluate whether you translate the English requirements into the correct SQL clauses on the first attempt. They watch for clean syntax, correct column references, and whether you verify edge cases before declaring the query complete.

> **Common Pitfall**
>
> The most common mistake is misreading the prompt's filtering or grouping requirements. Double-check which columns to group by, which to aggregate, and whether the output should be filtered with `WHERE` (before grouping) or `HAVING` (after grouping).

---

## 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/peak_metric_per_department)
- [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.