Metric Trend by 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
BearingPoint

Problem

Our department scorecards show a single all-time average of the tracked metrics per team, which hides whether a department rose or fell from one fiscal year to the next. Rebuild the scorecard from employee_metrics with that year-by-year view, departments alphabetically and each one's earliest year first.

How each team's numbers moved, year over year.

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 30 rows
departmentfiscal_yearavg_metric_value
Data202444.2
Data202557.70000000000001
Data202630.7
Design202439.93333333333333
Design202579.6

Worked solution and explanation

This is a grain question dressed up as a trend report. The skill being probed: can you collapse employee_metrics to exactly one row per department per fiscal year? Everyone reaches for AVG(metric_value). What separates candidates is naming the grain before typing. Group by department alone and you hand back 10 blended all-time numbers instead of the 30-row year-by-year series, and the movement the scorecard exists to show disappears.

Getting to the answer

Step 1: Name the grain out loud

The ask is a year-by-year view per department, so one result row is one (department, fiscal_year) pair. On the seed that is 10 departments times 3 fiscal years (2024 to 2026), which is 30 rows. Say that number before writing SQL, because it is your check when the query returns.

Step 2: Blend every metric row in the cell

metric_name holds ten different measures (headcount, attrition_rate and more), and the scorecard wants one blended number per cell, so metric_name stays out of the grouping. AVG skips the 12 rows whose metric_value is NULL, which is what you want: a missing reading is not a zero.

Step 3: Sort the way the reader scans

Departments alphabetically, earliest year first: ORDER BY department, fiscal_year. Both sort keys are the grouping keys, so every row has its own position and no tiebreaker is needed.

Average metric value per department per fiscal year
SELECT department, fiscal_year, AVG(metric_value) AS avg_metric_value FROM employee_metrics GROUP BY department, fiscal_year ORDER BY department, fiscal_year
Grouped by department only
GROUP BY department returns 10 rows, each averaging all three years together. The 2024 and 2026 readings cancel into one number, and the year-over-year movement is gone.
Grouped by department and year
GROUP BY department, fiscal_year returns 30 rows, one per cell. A reader can scan Data from 44.2 in 2024 up to 57.7 in 2025 and back down to 30.7 in 2026.

Common follow-up questions