Recurring Error Types
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
- Meta
Problem
To the on-call SRE triaging the payment-api outage, an error message with three or fewer entries in the log is background noise. List the messages in the err_tracks log that rose above that noise for payment-api.
The same errors, recurring.
Tables
err_tracks
Columns: err_id INTEGER, err_type TEXT, message TEXT, svc_name TEXT, severity TEXT, count INTEGER, first_at TEXT
| err_id | err_type | message | svc_name | severity | count | first_at |
|---|---|---|---|---|---|---|
| 137 | TypeError | Cannot read property of undefined | payment-api | error | 8 | 2026-02-02 01:11:00 |
| 174 | ConnectionTimeout | Connection timed out after 30s | user-svc | warning | 15 | 2026-03-03 02:22:00 |
| 211 | OutOfMemory | Heap space exhausted | search-api | info | 22 | 2026-04-04 03:33:00 |
| 248 | FileNotFound | File /tmp/data.csv not found | gateway | Fatal | 29 | 2026-05-05 04:44:00 |
| 285 | PermissionDenied | Access denied for user root | worker | ERROR | 36 | 2026-06-06 05:55:00 |
| 322 | RateLimitExceeded | Too many requests from 10.0.0.1 | auth-svc | fatal | 43 | 2026-07-07 06:06:00 |
| 359 | ParseError | Malformed JSON in request body | payment-api | error | 50 | 2026-08-08 07:17:00 |
| 396 | nullpointerexception | Stack overflow in recursive call | user-svc | warning | 57 | 2026-09-09 08:28:00 |
| 433 | TYPEERROR | Division by zero | search-api | info | 64 | 2026-01-10 09:39:00 |
| 470 | NullPointerException | Unexpected null value at line 42 | gateway | Fatal | NULL | 2026-02-11 10:50:00 |
Expected output
| message |
|---|
| Cannot read property of undefined |
| File /tmp/data.csv not found |
| Malformed JSON in request body |
| Unexpected null value at line 42 |
Worked solution and explanation
What this really asks
This is a scoped frequency count dressed up as incident triage. Scoping to 'payment-api' is a plain row filter, and everyone writes it. What separates candidates is where the bar of three sits and what it counts: it is a threshold on log entries per message, which only exists after grouping, so it belongs in HAVING. Put it in WHERE and the query errors. Reach for SUM(count) because the table has a column called count and all ten payment-api messages clear the bar, burying the four that genuinely repeat. Skip the threshold and you get all ten too, because every payment-api message has at least three entries.
Building the query
Step 1: Scope to the one service
WHERE svc_name = 'payment-api' discards every other service's rows before any counting happens. This is a row-level filter, exactly what WHERE is for.
Step 2: Count entries per message
GROUP BY message collapses the survivors into one group per message, and COUNT(*) gives how many log entries payment-api wrote for each. An entry is a row, so you count rows, not the stored count values.
Step 3: Keep only the recurring ones
HAVING COUNT(*) > 3 runs after the grouping and drops any message with three or fewer entries. WHERE cannot do this: the per-message count does not exist yet when WHERE is evaluated.
The solution
SELECT message
FROM err_tracks
WHERE svc_name = 'payment-api'
GROUP BY message
HAVING COUNT(*) > 3- Summing the stored tally
HAVING SUM(count) > 3adds up thecountcolumn, which holds values like 8 and 50 per row, so all ten payment-api messages sail past three.- Counting log entries
HAVING COUNT(*) > 3counts rows: four messages have four entries and survive, six have three and drop.
Common follow-up questions
- How would you return each message's number of entries next to `message`? (Tests moving from a filtered key list to reporting the aggregate itself.)
- What changes if the SRE wants this for every service at once, one threshold applied per service? (Probes grouping over `svc_name, message` rather than hard-coding a single service.)
- What would you have to do to report `err_type` instead, given 'TypeError' and 'TYPEERROR' both appear? (Tests normalizing a dirty key before grouping.)