Still Climbing
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
- L4
- Asked in
- Instacart
Problem
Health-check volume in svc_health jumped from February into March 2026, and the SRE team wants each service's March share of its checks across those two months, as a whole percentage with any fraction dropped. A service with no February checks was only switched on in March, so its share comes back blank rather than a full 100.
Which services kept gaining month over month?
Tables
svc_health
Columns: check_id INTEGER, svc_name TEXT, status TEXT, latency REAL, uptime REAL, checked TEXT, region TEXT
| check_id | svc_name | status | latency | uptime | checked | region |
|---|---|---|---|---|---|---|
| 137 | payment-api | degraded | 3.6 | 97.03 | 2026-02-02 01:05:00 | us-west-2 |
| 174 | user-svc | unhealthy | 6.7 | 94.06 | 2026-03-03 02:10:00 | eu-west-1 |
| 211 | search-api | timeout | 9.8 | 91.09 | 2026-04-04 03:15:00 | ap-south-1 |
| 248 | gateway | Healthy | 12.9 | 98.12 | 2026-05-05 04:20:00 | eu-central-1 |
| 285 | notif-svc | DEGRADED | 16 | 95.15 | 2026-06-06 05:25:00 | us-east-1 |
| 322 | ml-serving | healthy | 19.1 | 92.18 | 2026-07-07 06:30:00 | us-west-2 |
| 359 | cache-svc | degraded | NULL | 99.21 | 2026-08-08 07:35:00 | eu-west-1 |
| 396 | queue-worker | unhealthy | 25.3 | 96.24 | 2026-09-09 08:40:00 | ap-south-1 |
| 433 | cron-svc | timeout | 28.4 | 93.27 | 2026-01-10 09:45:00 | eu-central-1 |
| 470 | auth-svc | Healthy | 31.5 | 90.3 | 2026-02-11 10:50:00 | us-east-1 |
Expected output
| svc_name | march_share_pct |
|---|---|
| auth-svc | 40 |
| cache-svc | 50 |
| cron-svc | 50 |
| event-bus | 0 |
| feature-flag-svc | NULL |
Worked solution and explanation
What this problem really is
This is a two-bucket pivot dressed up as an SRE growth question, with two type decisions hiding in the last line. Folding February and March into one row per service is the part everyone gets. What separates candidates is the share itself: a service whose probe first fired in March has zero February checks, so its March share is a perfect 100 and it tops the board as the biggest climber when it never climbed at all. Blank it with NULLIF, then cut the share to a whole number with CAST, not a round. Skip the first and new probes look like breakouts; skip the second and every share with a fraction above one half lands a point high.
How to get there
Step 1: Scope to the two months
Only two year-months matter, so the WHERE keeps rows whose strftime('%Y-%m', checked) is in that two-value IN list. That also fixes the grain: a service shows up exactly when it was checked in at least one of the two months, and every check counts, whatever its status says.
Step 2: Count each month with CASE inside SUM
SUM(CASE WHEN ... = '2026-02' THEN 1 ELSE 0 END) counts February rows and its twin counts March, both in the same scan, one row per service. No second pass, no self-join.
Step 3: Let a missing baseline poison the denominator
The denominator is March plus February. Wrap the February count in NULLIF(..., 0) and a service with no February checks gets a NULL addend, and NULL plus anything is NULL, so its share comes back blank instead of 100. Services with a real baseline divide exactly as before.
Step 4: Multiply first, then drop the fraction
100.0 * march / total keeps the arithmetic in floating point and puts the scaling before the division, so exact shares stay exact. CAST(... AS INTEGER) then drops the fraction: 66.67 becomes 66.
SELECT svc_name,
CAST(
100.0 * SUM(CASE WHEN strftime('%Y-%m', checked) = '2026-03' THEN 1 ELSE 0 END)
/ (SUM(CASE WHEN strftime('%Y-%m', checked) = '2026-03' THEN 1 ELSE 0 END)
+ NULLIF(SUM(CASE WHEN strftime('%Y-%m', checked) = '2026-02' THEN 1 ELSE 0 END), 0))
AS INTEGER) AS march_share_pct
FROM svc_health
WHERE strftime('%Y-%m', checked) IN ('2026-02', '2026-03')
GROUP BY svc_nameOne scan, two conditional counts, a NULL for a missing baseline, a truncated whole-number share.
- Rounded
ROUND(100.0 * march / total): 2 of 3 checks in March comes back as 67, a point above what the team asked for.- Truncated
CAST(100.0 * march / total AS INTEGER): 2 of 3 comes back as 66, the whole percentage with the fraction dropped.
Common follow-up questions
- The team now wants the March share for every region separately. What changes, and which services newly come back blank? (Tests whether the candidate sees that a finer grain creates more empty February buckets.)
- How would you produce this share for every consecutive pair of months this year without one `CASE` per month? (Tests moving from a fixed pivot to a per-month count plus a lag.)
- If February checks can land days after the month closes, how do you keep this figure correct without rescanning both months on every run? (Tests incremental aggregation against late-arriving data.)