Alert Response Breakdown
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
- City and County of San Francisco
Problem
Every row of alert_events is one fired alert, with severity recorded in inconsistent letter case and ack_by left empty when nobody acknowledged it. Across the whole table, each service needs its total alerts, how many were critical, how many were high, and how many were never acknowledged. Alongside those comes the average number of alerts per status category the service saw, with the busiest services first.
An on-call postmortem asks which services are bleeding alerts nobody acknowledges.
Tables
alert_events
Columns: alert_id INTEGER, svc_name TEXT, severity TEXT, status TEXT, fired_at TIMESTAMP, ack_by TEXT, resolved TIMESTAMP
| alert_id | svc_name | severity | status | fired_at | ack_by | resolved |
|---|---|---|---|---|---|---|
| 129 | payment-api | high | acknowledged | 2026-02-02 01:07:00 | bob | 2026-02-02 03:11:00 |
| 158 | user-svc | medium | resolved | 2026-03-03 02:14:00 | charlie | 2026-03-03 04:22:00 |
| 187 | search-api | low | silenced | 2026-04-04 03:21:00 | dana | NULL |
| 216 | gateway | Critical | firing | 2026-05-05 04:28:00 | NULL | 2026-05-05 06:44:00 |
| 245 | notif-svc | HIGH | acknowledged | 2026-06-06 05:35:00 | alice | 2026-06-06 07:55:00 |
| 274 | db-primary | critical | resolved | 2026-07-07 06:42:00 | bob | NULL |
| 303 | cache-01 | high | silenced | 2026-08-08 07:49:00 | charlie | 2026-08-08 09:17:00 |
| 332 | auth-svc | medium | firing | 2026-09-09 08:56:00 | NULL | 2026-09-09 10:28:00 |
| 361 | payment-api | low | acknowledged | 2026-01-10 09:03:00 | eve | NULL |
| 390 | user-svc | Critical | resolved | 2026-02-11 10:10:00 | alice | 2026-02-11 12:50:00 |
Expected output
| svc_name | total_alerts | critical_count | high_count | unacked_count | avg_per_status |
|---|---|---|---|---|---|
| user-svc | 26 | 16 | 0 | 0 | 26 |
| search-api | 26 | 0 | 16 | 0 | 26 |
| payment-api | 26 | 0 | 18 | 0 | 26 |
| notif-svc | 24 | 0 | 16 | 0 | 24 |
| db-primary | 24 | 16 | 0 | 0 | 24 |
Worked solution and explanation
Why this problem exists in real interviews
This is a 4-bucket pivot dressed up as an on-call postmortem, and it tests whether you reach for conditional aggregation on instinct. The wide output is the tell: critical_count, high_count and unacked_count sit side by side. A junior writes 3 subqueries and stitches them back together; you collapse it into one pass over alert_events with a SUM(CASE WHEN ...) per bucket. The trap is a query-level WHERE: the moment severity = 'critical' sits there, total_alerts stops meaning total. The quieter slips are case ('Critical' and 'HIGH' live beside their lowercase twins) and the per-status denominator.
Break down the requirements
Step 1: Group by service, once
GROUP BY svc_name and nothing else. The total, the severity buckets, the unacked count and the per-status average all come out of the same scan over alert_events, so there is nothing to stitch back together.
Step 2: Normalize severity in the CASE
Put LOWER(severity) = 'critical' inside each SUM(CASE WHEN ...). The comparison runs row by row during the aggregate, so it decides which bucket a row lands in while every row still reaches the other columns.
Step 3: Count unacked rows through ack_by IS NULL
SUM(CASE WHEN ack_by IS NULL THEN 1 ELSE 0 END) says what it counts. COUNT(*) - COUNT(ack_by) returns the same number but hides the intent in a wide pivot, and COUNT(ack_by) alone is the acknowledged count, the opposite figure.
Step 4: Divide by COUNT(DISTINCT status)
1.0 * COUNT(*) / COUNT(DISTINCT status) is total alerts over the number of status values the service saw. The 1.0 * forces decimal division, so an engine that floors integer division (Postgres, SQL Server) keeps the fraction once a service spreads across several statuses.
Step 5: Order by volume
ORDER BY total_alerts DESC puts the busiest services on top. Several services tie at 26 and at 24 alerts, and the question names no rule for who comes first among them, so the query adds no tiebreaker.
The solution
SELECT
svc_name,
COUNT(*) AS total_alerts,
SUM(CASE WHEN LOWER(severity) = 'critical' THEN 1 ELSE 0 END) AS critical_count,
SUM(CASE WHEN LOWER(severity) = 'high' THEN 1 ELSE 0 END) AS high_count,
SUM(CASE WHEN ack_by IS NULL THEN 1 ELSE 0 END) AS unacked_count,
1.0 * COUNT(*) / COUNT(DISTINCT status) AS avg_per_status
FROM alert_events
GROUP BY svc_name
ORDER BY total_alerts DESC;- Pivot via conditional aggregation
- One pass, output already wide. A
SUM(CASE WHEN LOWER(severity) = 'critical' ...)per bucket, grouped only bysvc_name, hands the BI tool a ready table. - Group by service and severity, then reshape
- Long format: one row per service and severity. Correct, but you still need a
PIVOT(Snowflake, SQL Server) or a wrapping conditional aggregate to widen it. 2 steps for no gain.
Common follow-up questions
Common follow-up questions
- How would you extend this to a per-week breakdown without rewriting the aggregate? (Probes whether you add `DATE_TRUNC('week', fired_at)` to the `GROUP BY` and see that this shape grows by adding grouping keys.)
- What changes if severity values were normalized at ingest? (Tests whether you push data hygiene upstream instead of paying for `LOWER()` on every read of a 50M-row table.)
- How would you compute the median time-to-acknowledge per service in the same pass? (There is no acknowledgement timestamp in `alert_events`, so it starts with the data you would need, then leads to `PERCENTILE_CONT(0.5)` and why a median cannot fold into a plain `SUM(CASE ...)`.)
- Why might `COUNT(DISTINCT status)` mislead the average? (A service with one observed status divides by 1, inflating its average against a service that sees all 4 statuses.)
- Rewrite this for a columnar warehouse where `severity` is dictionary-encoded. (Surfaces awareness that `LOWER()` defeats dictionary pushdown; the fix is to compare against the encoded set or normalize at load.)