Extremely Late Resolutions

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
L4
Asked in
General Assembly

Problem

What were the first three alerts each service fired, according to alert_events, and where does each one fall in its service's history? The on-call review team wants those alerts exactly as they were logged, every column in its original spelling, with the lower alert_id counting as earlier when two fired at the same moment.

Twenty minutes past the SLA. Still unresolved.

Tables

alert_events

Columns: alert_id INTEGER, svc_name TEXT, severity TEXT, status TEXT, fired_at TEXT, ack_by TEXT, resolved TEXT

alert_events: 10 of 200 rows
alert_idsvc_nameseveritystatusfired_atack_byresolved
129payment-apihighacknowledged2026-02-02 01:07:00bob2026-02-02 03:11:00
158user-svcmediumresolved2026-03-03 02:14:00charlie2026-03-03 04:22:00
187search-apilowsilenced2026-04-04 03:21:00danaNULL
216gatewayCriticalfiring2026-05-05 04:28:00NULL2026-05-05 06:44:00
245notif-svcHIGHacknowledged2026-06-06 05:35:00alice2026-06-06 07:55:00
274db-primarycriticalresolved2026-07-07 06:42:00bobNULL
303cache-01highsilenced2026-08-08 07:49:00charlie2026-08-08 09:17:00
332auth-svcmediumfiring2026-09-09 08:56:00NULL2026-09-09 10:28:00
361payment-apilowacknowledged2026-01-10 09:03:00eveNULL
390user-svcCriticalresolved2026-02-11 10:10:00alice2026-02-11 12:50:00

Expected output

First 5 of 24 rows
alert_idsvc_nameseveritystatusfired_atack_byresolvedrnk
10003016auth-svcCriticalfiring2022-04-17 16:52:00NULL2022-04-17 18:56:001
10003056auth-svcmediumfiring2022-08-01 08:32:00NULL2022-08-01 10:16:002
10003031cache-01highsilenced2022-07-04 07:37:00bob2022-07-04 09:41:001
10003086db-primarymediumresolved2022-02-03 14:02:00bob2022-02-03 16:46:001
10003061notif-svchighacknowledged2022-01-06 13:07:00bob2022-01-06 15:11:001

Worked solution and explanation

What this really is

This is top-N-per-group dressed up as an on-call history review. Everyone sees 'first three' and types ORDER BY fired_at LIMIT 3, and that is the trap: LIMIT caps the whole result, so you get three alerts from whichever service fired first and nothing for the other services. The candidates who dodge that often swing to MIN(fired_at), which only ever finds the first alert. The skill being probed is whether you can number rows inside each service, keep the whole row, and hand back its position.

Limit the whole table
ORDER BY fired_at LIMIT 3 sorts every alert together and stops after three rows total. A MIN(fired_at) join-back or a single-row dedup does better on coverage but still returns one alert per service and no position.
Number inside each service
ROW_NUMBER() OVER (PARTITION BY svc_name ORDER BY fired_at, alert_id) restarts at 1 for every service. Keep rnk <= 3 and each service contributes its own three full rows, labeled 1, 2, 3.

Building it

Step 1: Partition by svc_name

PARTITION BY svc_name makes the count restart for every service. That is the per-service boundary LIMIT has no way to express: it only knows the total result.

Step 2: Order by fired_at, then alert_id

Order each partition by fired_at, then alert_id. Without the second key, two alerts fired in the same minute trade places run to run, and which one is 'second' becomes a coin flip the grader will not forgive.

Step 3: Keep rnk <= 3 in the outer query

A window alias cannot be filtered in the SELECT that computes it, so the numbering lives in the derived table ranked_alerts and the outer query keeps rnk <= 3. There is no filter on resolved and no cleanup of severity: every alert comes back as it was logged and holds its place in line.

First three alerts per service
SELECT alert_id, svc_name, severity, status, fired_at, ack_by, resolved, rnk
FROM (
    SELECT alert_id,
           svc_name,
           severity,
           status,
           fired_at,
           ack_by,
           resolved,
           ROW_NUMBER() OVER (PARTITION BY svc_name ORDER BY fired_at, alert_id) AS rnk
    FROM alert_events
) AS ranked_alerts
WHERE rnk <= 3

Common follow-up questions