Best in Show

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4
Asked in
Asana

Problem

We log a range of performance metrics for each department, and the same reading sometimes gets recorded more than once. For each department, surface its highest metric value as a single entry, showing the department, the metric name, the value, and its standing within the department. Walk through the results alphabetically by department, and by metric name wherever a department contributes more than one entry.

Every department has a peak. Name it, once each.

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
departmentmetric_namemetric_valuernk
Dataremote_percentage84.71
Designavg_tenure_months98.61
Engineeringheadcount921
Financeopen_positions89.81
HRpromotions93.51

Worked solution and explanation

What this really is

Strip the business costume and this is a per-partition maximum with two quiet traps sitting on top of it. Anyone can find the highest metric_value per department; what separates candidates is keeping the tied winners AND folding away the duplicate rows the raw table carries. Miss the first and a co-leader silently vanishes. Miss the second and every winner shows up twice, which is exactly the 20-rows-instead-of-10 result that fails the check.


The two traps


Building it

Step 1: Rank within each department

RANK() OVER (PARTITION BY department ORDER BY metric_value DESC) numbers rows inside each department, biggest value first. RANK (not ROW_NUMBER) is deliberate: equal values receive the same rnk, so a department's co-leaders both land on 1.

Step 2: Keep the top standing

Wrap the windowed query in a subquery and keep WHERE rnk = 1. Because ties share rank 1, this keeps every top entry per department rather than an arbitrary single winner.

Step 3: Collapse duplicates and order

Apply DISTINCT to the selected columns so identical logged rows collapse into one entry, then ORDER BY department, metric_name for a stable, readable result.


The solution

Rank per department, keep rank 1, dedup
SELECT DISTINCT department, metric_name, metric_value, rnk FROM (SELECT department, metric_name, metric_value, RANK() OVER (PARTITION BY department ORDER BY metric_value DESC) AS rnk FROM employee_metrics) WHERE rnk = 1 ORDER BY department, metric_name

Common follow-up questions