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_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 8 rows
svc_nametotal_alertscritical_counthigh_countunacked_countavg_per_status
user-svc26160026
search-api26016026
payment-api26018026
notif-svc24016024
db-primary24160024

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

Conditional-agg pivot in one pass
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 by svc_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