The High and the Low
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
- KPMG
Problem
Which services in svc_health posted the highest and the lowest latency in each US region, labeled 'highest' or 'lowest' by the end they sit at? The on-call engineer reviewing them wants every service tied at an end, not just one of them.
The fastest and slowest in every region.
Tables
svc_health
Columns: check_id INTEGER, svc_name TEXT, status TEXT, latency REAL, uptime REAL, checked TEXT, region TEXT
| check_id | svc_name | status | latency | uptime | checked | region |
|---|---|---|---|---|---|---|
| 137 | payment-api | degraded | 3.6 | 97.03 | 2026-02-02 01:05:00 | us-west-2 |
| 174 | user-svc | unhealthy | 6.7 | 94.06 | 2026-03-03 02:10:00 | eu-west-1 |
| 211 | search-api | timeout | 9.8 | 91.09 | 2026-04-04 03:15:00 | ap-south-1 |
| 248 | gateway | Healthy | 12.9 | 98.12 | 2026-05-05 04:20:00 | eu-central-1 |
| 285 | notif-svc | DEGRADED | 16 | 95.15 | 2026-06-06 05:25:00 | us-east-1 |
| 322 | ml-serving | healthy | 19.1 | 92.18 | 2026-07-07 06:30:00 | us-west-2 |
| 359 | cache-svc | degraded | NULL | 99.21 | 2026-08-08 07:35:00 | eu-west-1 |
| 396 | queue-worker | unhealthy | 25.3 | 96.24 | 2026-09-09 08:40:00 | ap-south-1 |
| 433 | cron-svc | timeout | 28.4 | 93.27 | 2026-01-10 09:45:00 | eu-central-1 |
| 470 | auth-svc | Healthy | 31.5 | 90.3 | 2026-02-11 10:50:00 | us-east-1 |
Expected output
| svc_name | region | latency | latency_type |
|---|---|---|---|
| queue-worker | us-east-1 | 808.7 | highest |
| notif-svc | us-east-1 | 16 | lowest |
| gateway | us-west-2 | 792.3 | highest |
| payment-api | us-west-2 | 3.6 | lowest |
Worked solution and explanation
What this problem really is
This is a two-headed extremes query wearing an SRE dashboard costume. The real skill: can you pull both the slowest and the fastest service per region in a single pass, and keep every service that ties at either edge? Anyone can sort and grab the top row. The trap is ties: reach for ROW_NUMBER and a region with three services at the same latency loses two of them. The second trap is the filters. The non-US regions and the NULL latency rows have to go before you rank, or a NULL sorts to the front of the ascending order and quietly steals the 'lowest' slot.
Break down the requirements
Step 1: Scope to US regions and measured checks
Keep only rows of svc_health whose region starts with 'us-', and drop rows where latency is NULL. A NULL latency is not a measurement, and SQLite sorts it first under an ascending order, so leaving it in hands it the low end.
Step 2: DENSE_RANK twice in the same SELECT
In a CTE, compute two DENSE_RANK() windows per region: one ordering latency DESC (high_rank) and one ordering latency ASC (low_rank). Tied extremes all share rank 1, which is the whole point.
Step 3: Keep the ends and label them
Keep rows where high_rank = 1 or low_rank = 1, and let a CASE turn the ranks into the 'highest' or 'lowest' label. No final sort: the question asks which services, not in what order.
The solution
WITH ranked AS (
SELECT svc_name, region, latency,
DENSE_RANK() OVER (PARTITION BY region ORDER BY latency DESC) AS high_rank,
DENSE_RANK() OVER (PARTITION BY region ORDER BY latency ASC) AS low_rank
FROM svc_health
WHERE region LIKE 'us-%'
AND latency IS NOT NULL
)
SELECT svc_name, region, latency,
CASE WHEN high_rank = 1 THEN 'highest' WHEN low_rank = 1 THEN 'lowest' END AS latency_type
FROM ranked
WHERE high_rank = 1 OR low_rank = 1Common follow-up questions
- How would you label a region's only service, which is both highest and lowest? (Tests edge case thinking. When both ranks are 1 the `CASE` checks `high_rank` first, so a sole service always labels as 'highest'. A strong answer surfaces the ambiguity and proposes a 'both' label or two rows.)
- Why filter on `region` before ranking rather than on the ranked output? (Tests filter pushdown. `region` is a row level column, so `WHERE` shrinks the input before the windows run; filtering after ranking wastes the sorts on rows you discard.)
- What changes if `latency` could be negative due to clock skew? (Tests data quality awareness. A negative `latency` from clock skew would sort to the bottom and be crowned 'lowest'; a `latency >= 0` guard in `WHERE` keeps a buggy reading out.)