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

svc_health: 10 of 200 rows
check_idsvc_namestatuslatencyuptimecheckedregion
137payment-apidegraded3.697.032026-02-02 01:05:00us-west-2
174user-svcunhealthy6.794.062026-03-03 02:10:00eu-west-1
211search-apitimeout9.891.092026-04-04 03:15:00ap-south-1
248gatewayHealthy12.998.122026-05-05 04:20:00eu-central-1
285notif-svcDEGRADED1695.152026-06-06 05:25:00us-east-1
322ml-servinghealthy19.192.182026-07-07 06:30:00us-west-2
359cache-svcdegradedNULL99.212026-08-08 07:35:00eu-west-1
396queue-workerunhealthy25.396.242026-09-09 08:40:00ap-south-1
433cron-svctimeout28.493.272026-01-10 09:45:00eu-central-1
470auth-svcHealthy31.590.32026-02-11 10:50:00us-east-1

Expected output

First 5 of 13 rows
svc_namemarch_share_pct
auth-svc40
cache-svc50
cron-svc50
event-bus0
feature-flag-svcNULL

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.

Two-month pivot into a whole-number March share
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_name

One 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