Where the Year Leans

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
ESPN

Problem

A people-analytics team logs one reading per department metric each quarter, and leadership wants to see whether a department's reporting leans toward the first half of the fiscal year (quarters Q1 and Q2) or the second half (quarters Q3 and Q4). Walking through the departments in alphabetical order, show the first-half reading count, the second-half reading count, and the ratio of the two.

Some teams file early, some file late. See which way each one tips.

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
departmentfirst_half_countsecond_half_counthalf_year_ratio
Data10101
Design10101
Engineering10101
Finance10101
HR10101

Worked solution and explanation

What this really is

Strip the reporting-cadence costume and this is a single-pass conditional pivot: for each department you pull two counts out of the same scan, split by which half of the year a reading lands in, then divide them. Anyone can produce the two counts. Two moves separate the people who finish clean. They put the CASE INSIDE the SUM so one pass over the table yields both buckets, and they cast before dividing so SQLite does floating-point division instead of handing back a truncated 0 or 1. Miss the cast and every ratio collapses to a whole number that looks plausible and is quietly wrong.


Building it

Step 1: Split each row with CASE inside the aggregate

SUM(CASE WHEN fiscal_quarter IN ('Q1','Q2') THEN 1 ELSE 0 END) counts only first-half readings; a second SUM keyed on Q3 and Q4 counts the rest. Both are computed in the same pass because the CASE lives inside the SUM, not wrapped around it.

Step 2: Collapse to one row per department

GROUP BY department collapses the scan to one row per team, and the two conditional sums are evaluated within each group.

Step 3: Divide, but cast first

SQLite divides two integers as integers, so 5 over 8 becomes 0. Cast the numerator to REAL to force floating-point division. Wrap the denominator in MAX(second_half_count, 1) so a department with no second-half readings returns the numerator instead of a divide-by-zero NULL.


The solution

Single-pass conditional pivot
SELECT department,
       SUM(CASE WHEN fiscal_quarter IN ('Q1','Q2') THEN 1 ELSE 0 END) AS first_half_count,
       SUM(CASE WHEN fiscal_quarter IN ('Q3','Q4') THEN 1 ELSE 0 END) AS second_half_count,
       CAST(SUM(CASE WHEN fiscal_quarter IN ('Q1','Q2') THEN 1 ELSE 0 END) AS REAL) / MAX(SUM(CASE WHEN fiscal_quarter IN ('Q3','Q4') THEN 1 ELSE 0 END), 1) AS half_year_ratio
FROM employee_metrics
GROUP BY department
ORDER BY department
Two-pass stitch
Run one grouped count filtered to Q1 and Q2, run another filtered to Q3 and Q4, then join the two result sets on department to compute the ratio. Two scans, one join, more code, and a silent gap if a department shows up in only one half.
Single-pass pivot
One scan, two SUM(CASE...) columns, then divide. Every department appears exactly once, a missing half counts as zero for free, and there is nothing to join.

Common follow-up questions