Peak Satisfaction

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

Problem

The employee_metrics table keeps ten different HR metrics in one shared value column, so an analyst building the engagement report can only trust numbers taken from the satisfaction_score readings. She needs the highest and lowest satisfaction score in each fiscal year, along with how many measurements were recorded that year.

Which departments are winning on satisfaction?

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 3 of 3 rows
fiscal_yearmax_scoremin_scoremeasurement_count
202478.921.98
202594.913.96
202686.95.96

Worked solution and explanation

What this problem is really testing

Strip off the engagement-report costume and this is a filtered group-wise aggregation: keep one metric's rows, collapse them to one row per fiscal_year, and carry three aggregates at once. Anyone can write MAX(metric_value). What separates candidates is noticing that metric_value is a shared column holding ten unrelated metrics, so WHERE metric_name = 'satisfaction_score' has to run before any grouping. Skip it and every year's peak and floor come from whatever metric runs hottest, and the count balloons tenfold.


Walking to the answer

Step 1: Restrict to satisfaction_score first

employee_metrics is a long, narrow table: the metric's identity lives in metric_name, not in the column name. The filter has to sit in WHERE so the other nine metrics never reach the aggregates.

Step 2: Group by fiscal_year

The report wants a per-year view, so fiscal_year is the grain. One GROUP BY gives one row per year with no extra passes over the table.

Step 3: Emit peak, floor and count together

MAX(metric_value), MIN(metric_value) and COUNT(*) all ride on the same grouping. Writing three subqueries for three numbers is the shape interviewers mark down.

Filter, then summarize per fiscal year
SELECT
    fiscal_year,
    MAX(metric_value) AS max_score,
    MIN(metric_value) AS min_score,
    COUNT(*) AS measurement_count
FROM employee_metrics
WHERE metric_name = 'satisfaction_score'
GROUP BY fiscal_year

Common follow-up questions