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

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 16 rows
metric_namefiscal_halfavg_value
avg_tenure_monthsH166
avg_tenure_monthsH246
headcountH151
headcountH245
open_positionsH142

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.

Per-metric averages by fiscal half
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