Peak Metric Per Department

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
L3
Asked in
Forbes

Problem

Across every metric HR tracks in employee_metrics, what is the single highest value each department has ever recorded? HR's review reads the departments from the highest peak down.

Peak metrics for the quarterly deck.

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
departmentpeak_value
Product99.3
Design98.6
Legal97.1
Operations96.4
Marketing94.9

Worked solution and explanation

This is a one-number-per-group summary dressed up as an HR review. Nearly everyone finds the largest metric_value. What separates answers is what the sort runs on. employee_metrics has 200 rows over ten departments, and every row has a clone. If you sort rows instead of peaks, HR's list opens with Product's 99.3 twice before any other department appears. If you collapse the rows but sort by department, Product ends up behind Data and Design.

Getting there

Step 1: Fix the grain at one row per department

The question asks for one figure per department, so ten rows come back, not 200. Group on department alone. Adding metric_name would return one peak per metric, which is the reading 'across every metric' rules out.

Step 2: Take MAX(metric_value) as the peak

The peak is the largest value from any quarter or year, so there is no filter on fiscal_year or fiscal_quarter. Twelve rows have a NULL metric_value. MAX skips them, and every department still has real values, so no department drops out.

Step 3: Sort on the peak, not on a stored column

Name the aggregate peak_value and sort on that alias, highest first. The sort runs after the grouping, so it orders departments by their peaks rather than ordering rows by their values.

One peak per department, highest first
SELECT department, MAX(metric_value) AS peak_value
FROM employee_metrics
GROUP BY department
ORDER BY peak_value DESC
Sorting rows
SELECT department, metric_value FROM employee_metrics ORDER BY metric_value DESC returns 200 rows. Product shows up twice at 99.3, then Design twice at 98.6, and every department repeats down the list.
Sorting peaks
Grouping first leaves ten rows. ORDER BY peak_value DESC then puts Product, Design and Legal at the top, one line each.

Common follow-up questions