Low Uptime Services

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

Problem

The SRE team set a 95% uptime SLA and needs to surface every health check that fell short, worst uptime first so the shakiest services surface at the top. Show the service name, check date, and uptime for each violation, and when two share the same uptime, put the one checked earliest ahead.

Underperforming services.

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 88 rows
svc_namecheckeduptime
payment-api2022-01-05 00:00:0088
payment-api2022-01-21 12:00:0088
notif-svc2026-01-17 00:48:0088.2
cron-svc2025-01-01 00:36:0088.4
search-api2024-01-13 00:24:0088.6

Worked solution and explanation

This is a units question dressed up as an SLA report. The SLA reads '95%', and the reflex is to type uptime < 0.95. But uptime is stored on a 0-100 scale: the very first sample rows read 97.03, 94.06, 91.09. Write the fraction and the filter matches nothing, so the report comes back empty and every breach goes unreported. Anyone can write a filter and a sort; what separates candidates is glancing at the data before typing the threshold.

Read the units, then write the bar

Step 1: Check the scale of uptime before trusting the prose

A percentage can be stored as 0.9703 or as 97.03, and the prompt will not tell you which. Three sample values settle it: everything in svc_health sits between about 88 and 99.99, so 95% means the literal 95.

Step 2: Keep strict misses only

A check at exactly 95 meets the SLA, so the bar is uptime < 95, not <=. The 15 checks with a NULL uptime fall out on their own because a comparison with NULL is never true. That is the right outcome: a check that recorded no uptime is not a proven breach.

Step 3: Sort worst first, earliest first within a tie

ORDER BY uptime ASC puts the deepest breach on top, and checked ASC orders checks that share an uptime. The second key does real work here: two 'payment-api' checks both sit at 88.0. checked is text in 'YYYY-MM-DD HH:MM:SS' form, so sorting it as text is already chronological.

SLA violations, worst first
SELECT svc_name, checked, uptime
FROM svc_health
WHERE uptime < 95
ORDER BY uptime ASC, checked ASC
Fraction reading
WHERE uptime < 0.95 treats '95%' as 0.95. No stored value is that low, so the query returns zero rows and looks like a clean month.
Percentage reading
WHERE uptime < 95 matches the column's real scale and returns all 88 breaches, from 88.0 upward.

Common follow-up questions