Infant Mortality

A hard SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
hard
Seniority
L4
Asked in
Google

Problem

Our svc_health checks record status with inconsistent capitalization, and the casing carries no meaning. For every service whose first check landed on or after January 1, 2026, report its failure rate as a decimal fraction: the share of all its checks that came back as anything other than healthy.

The youngest ones break first.

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 7 rows
svc_namenegative_ratio
auth-svc0.6111111111111112
edge-auth-svc0.5
event-bus0.5
feature-flag-svc0.5
notif-svc0.6666666666666666

Worked solution and explanation

What this problem really is

This is a per-group cohort test wearing a reliability costume. 'First check on or after January 1, 2026' is a statement about MIN(checked) across a service's whole history, not about any one row. Everyone writes the conditional ratio; what separates candidates is where the date goes. Put checked >= '2026-01-01' in WHERE and every veteran service that also ran this year sails in as new, with its rate computed over this year's checks alone.


Break down the requirements

Step 1: Count failures without trusting casing

SUM(CASE WHEN LOWER(status) <> 'healthy' THEN 1 ELSE 0 END) over COUNT(*). Lowercasing matters on this data: 'Healthy' sits next to 'healthy', and a raw comparison books every capitalized healthy check as a failure.

Step 2: Force fractional division

Cast the sum to REAL before dividing. Integer division turns every rate between 0 and 1 into 0, and the statement asks for a decimal fraction.

Step 3: Scope the cohort on the group

HAVING MIN(checked) >= '2026-01-01' keeps a service only when its whole history starts on or after that date. It has to be HAVING: the minimum does not exist until the group does. No upper bound is needed, because the question is 'new since', and any later first check qualifies.


The solution

Cohort by earliest check
SELECT
  svc_name,
  CAST(SUM(CASE WHEN LOWER(status) <> 'healthy' THEN 1 ELSE 0 END) AS REAL)
    / COUNT(*) AS negative_ratio
FROM svc_health
GROUP BY svc_name
HAVING MIN(checked) >= '2026-01-01'
Row filter
WHERE checked >= '2026-01-01' asks whether one check is recent. Every service with a check this year passes, and its rate silently drops all its earlier history.
Group filter
HAVING MIN(checked) >= '2026-01-01' asks whether the service's earliest check is recent. Only genuinely new services pass, and their rate covers every check they have.

Common follow-up questions

Common follow-up questions