Services With Multi-Quarter Uptime

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
Meta

Problem

A service earns a stable rating in the SRE team's 2026 review only when its healthy checks in svc_health are spread across at least two separate calendar quarters of that year. List the services that earn it.

Multi-quarter uptime streaks.

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 6 rows
svc_name
auth-svc
gateway
ml-serving
notif-svc
queue-worker

Worked solution and explanation

What this really is

This is a distinct-bucket count dressed up as an SRE stability review. The skill being probed: can you turn a timestamp into a quarter and count how many separate quarters each service covers, instead of how many checks it logged? Anyone can filter svc_health to healthy rows. The trick is collapsing to one row per service and quarter before counting. Skip that and a service with two healthy checks in the same week gets certified as stable for the year.


Build it in order

Step 1: Scope to healthy checks in 2026

Keep rows whose status is 'healthy' and whose EXTRACT(YEAR FROM checked) equals 2026. The year scope has to come before the bucketing, or Q1 of last year looks identical to Q1 of this year.

Step 2: Bucket each check into its quarter

EXTRACT(QUARTER FROM checked) returns 1 to 4 straight from the timestamp. No month arithmetic, no lookup: the quarter becomes a real column you can deduplicate on.

Step 3: Collapse to one row per service and quarter

SELECT DISTINCT svc_name, quarter in the healthy_quarters CTE means ten checks in March become one row. After that, COUNT(*) per service IS a count of quarters, and HAVING COUNT(*) >= 2 is the stability bar.


The solution

Quarter buckets, then count quarters per service
WITH healthy_quarters AS (
  SELECT DISTINCT
         svc_name,
         EXTRACT(QUARTER FROM checked) AS quarter
  FROM svc_health
  WHERE status = 'healthy'
    AND EXTRACT(YEAR FROM checked) = 2026
)
SELECT svc_name
FROM healthy_quarters
GROUP BY svc_name
HAVING COUNT(*) >= 2;
Counts checks
Filter healthy 2026 rows, GROUP BY svc_name, HAVING COUNT(*) >= 2. Two healthy checks on the same afternoon pass.
Counts quarters
Deduplicate to svc_name, quarter first, then HAVING COUNT(*) >= 2. Only two different quarters pass. COUNT(DISTINCT quarter) without the CTE is the same skill.

Common follow-up questions