Max Value Per Location

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

HR is reviewing peak performance across the company, one department at a time. Show each department alongside its highest recorded metric value.

Every location has a peak.

Tables

employee_metrics

Columns: metric_id INTEGER, department TEXT, metric_name TEXT, metric_value REAL, 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 10 rows
departmentmax_value
Data84.7
Design98.6
Engineering92
Finance89.8
HR93.5

Worked solution and explanation

What this really is

This is a one-row-per-department maximum wearing an HR leaderboard costume. Anyone can find the biggest number in employee_metrics; the skill is making MAX(metric_value) answer once per department instead of once for the whole table. 2 shortcuts sink people. Reach for AVG because metric_value reads like a score you would average, and every department reports its mean, so all 10 values come back wrong. Drop the GROUP BY and the query hands back a single 99.3 for all 8,000 rows: one row where HR asked for ten.

Building it

Step 1: Fix the grain before the metric

Say it out loud first: one row per department. That sentence is the GROUP BY department, and it also tells you what may appear in the SELECT: the key itself plus aggregates. Anything else, like metric_name or metric_id, would either split the groups or be rejected outright.

Step 2: Take the peak, not the typical value

HR asked for the highest recorded value, so the aggregate is MAX(metric_value). Alias it max_value so the output column says what it holds. No filter on metric_name: the ask is the single highest value the department ever recorded, whatever metric produced it.

Step 3: Present it biggest peak first

A leaderboard reads top-down, so finish with ORDER BY max_value DESC. Sorting by the alias works because ORDER BY runs after the SELECT list is computed, so you never repeat the MAX(...) expression.

Peak `metric_value` per `department`
SELECT department, MAX(metric_value) AS max_value
FROM employee_metrics
GROUP BY department

10 groups in, 10 rows out, strongest peak at the top.

No grouping
SELECT MAX(metric_value) FROM employee_metrics collapses all 8,000 rows into one bucket and returns a lone 99.3. It is the right number for Product and says nothing about the other 9 departments.
Grouped by department
GROUP BY department splits the table into 10 buckets first, so MAX(metric_value) runs inside each one and every department gets its own peak.

Common follow-up questions