Where The Lights Stay On

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
L3
Asked in
City and County of San Francisco

Problem

Our SLA targets keep getting set off one fleet-wide uptime number, which hides how differently each region behaves. From the health checks in svc_health, give each region's average service uptime, most reliable first.

Every region promises reliability. See which ones keep it.

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 5 rows
regionavg_uptime
eu-west-195.63459459459459
eu-central-195.36783783783784
us-west-295.02810810810811
ap-south-194.01166666666667
us-east-193.4828947368421

Worked solution and explanation

What this is really asking

This is a per-region average wearing an SLA costume. The entire problem is one grouped mean, but two habits sink candidates. The first is collapsing every row into a single fleet-wide number instead of one average per region. The second is reading 'most reliable' as the smallest value and sorting the wrong way. Miss the grouping and you report one meaningless number; miss the direction and you crown your least reliable region as the gold standard.


Build it in two moves

Step 1: Average uptime within each region

Collapse the rows to one per region and take the mean uptime inside each. AVG(uptime) skips NULLs, and uptime does have missing readings across the full table, so those checks simply drop out of their region's mean instead of dragging it toward zero. The NULL you can see in the sample rows sits in latency, which this metric never reads.

Step 2: Order most reliable first

The business phrase 'most reliable first' is a descending sort on the average you just computed. Because you now have one row per region, ordering by avg_uptime puts the steadiest region at the top. If two regions tied, either could come first; on this data the five averages are all different.


The solution

Grouped average with ordering
SELECT region, AVG(uptime) AS avg_uptime
FROM svc_health
GROUP BY region
ORDER BY avg_uptime DESC

Common follow-up questions