Split Metric Sums
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
- L3
- Asked in
- Tesla
Problem
Give us the average of each of our people metrics in employee_metrics for the first half of the fiscal year (Q1 and Q2) and for the second half (Q3 and Q4), as whole numbers rounded down. Leave out attrition_rate and diversity_ratio: they are rates we read to the decimal, and rounding them down would erase that.
One column, two totals.
Tables
employee_metrics
Columns: metric_id INTEGER, department TEXT, metric_name TEXT, metric_value REAL, fiscal_quarter TEXT, fiscal_year INTEGER
| metric_id | department | metric_name | metric_value | fiscal_quarter | fiscal_year |
|---|---|---|---|---|---|
| 141 | Product | attrition_rate | 7.3 | Q2 | 2025 |
| 182 | Design | avg_tenure_months | 14.6 | Q3 | 2026 |
| 223 | Marketing | satisfaction_score | 21.9 | Q4 | 2024 |
| 264 | Sales | training_hours | 29.2 | Q1 | 2025 |
| 305 | HR | promotions | 36.5 | Q2 | 2026 |
| 346 | Finance | open_positions | 43.8 | Q3 | 2024 |
| 387 | Legal | time_to_hire_days | 51.1 | Q4 | 2025 |
| 428 | Operations | diversity_ratio | 58.4 | Q1 | 2026 |
| 469 | Data | remote_percentage | 65.7 | Q2 | 2024 |
| 510 | Engineering | headcount | 73 | Q3 | 2025 |
Expected output
| metric_name | fiscal_half | avg_value |
|---|---|---|
| avg_tenure_months | H1 | 66 |
| avg_tenure_months | H2 | 46 |
| headcount | H1 | 51 |
| headcount | H2 | 45 |
| open_positions | H1 | 42 |
Worked solution and explanation
What this really is
This is a derived-key average with a truncation, dressed up as a half-year people report. employee_metrics is tall: one metric_value column holds every measurement, told apart only by metric_name. Anyone can average by metric. What separates candidates is 3 quiet decisions: the halves are a key you build, 2 metrics have to go before the math, and the fraction comes off the finished mean, not off each row and not by rounding. Miss any one and rows in the report are off.
Step 1: Remove the 2 rates in the WHERE
attrition_rate and diversity_ratio are rates read to the decimal, so truncating them throws away exactly the precision they are reported at. Filtering them with metric_name <> 'attrition_rate' AND metric_name <> 'diversity_ratio' drops their rows before any group is formed.
Step 2: Fold 4 quarters into 2 halves
A CASE maps 'Q1' and 'Q2' to 'H1' and everything else to 'H2'. Group by metric_name and that derived fiscal_half; grouping by fiscal_quarter instead returns 4 rows per metric where the report wants 2.
Step 3: Average, floor, then cast
AVG(metric_value) skips the missing values, so a gap in the log does not drag the mean down. FLOOR then drops the fraction of the finished mean; every measurement is non-negative, so flooring is truncation. The outer CAST(... AS INTEGER) only turns that whole number into an integer type.
SELECT
metric_name,
CASE
WHEN fiscal_quarter = 'Q1' OR fiscal_quarter = 'Q2' THEN 'H1'
ELSE 'H2'
END AS fiscal_half,
CAST(FLOOR(AVG(metric_value)) AS INTEGER) AS avg_value
FROM employee_metrics
WHERE metric_name <> 'attrition_rate'
AND metric_name <> 'diversity_ratio'
GROUP BY metric_name, fiscal_half- Floor each row, then average
AVG(FLOOR(metric_value))chops the fraction off every measurement first, then averages the stumps. The result still carries a fraction, and it sits below the true mean by up to a full unit.- Average, then floor
FLOOR(AVG(metric_value))computes the honest mean from the real values and only then drops the partial unit. One truncation, at the end, exactly where the report asks for it.
Common follow-up questions
- How would you show 'H1' and 'H2' side by side as 2 columns, one row per metric? (Moves the `CASE` inside the aggregate instead of into the grouping key.)
- Finance wants the halves broken out per `fiscal_year`. What changes? (Tests whether the candidate sees it is one more grouping key and nothing else.)
- If a metric could go negative, would `FLOOR` still drop the fraction the way the report means? (Checks whether the candidate knows `FLOOR` and truncation part ways below zero.)