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_tracks: 10 of 200 rows
err_iderr_typemessagesvc_nameseveritycountfirst_at
137TypeErrorCannot read property of undefinedpayment-apierror82026-02-02 01:11:00
174ConnectionTimeoutConnection timed out after 30suser-svcwarning152026-03-03 02:22:00
211OutOfMemoryHeap space exhaustedsearch-apiinfo222026-04-04 03:33:00
248FileNotFoundFile /tmp/data.csv not foundgatewayFatal292026-05-05 04:44:00
285PermissionDeniedAccess denied for user rootworkerERROR362026-06-06 05:55:00
322RateLimitExceededToo many requests from 10.0.0.1auth-svcfatal432026-07-07 06:06:00
359ParseErrorMalformed JSON in request bodypayment-apierror502026-08-08 07:17:00
396nullpointerexceptionStack overflow in recursive calluser-svcwarning572026-09-09 08:28:00
433TYPEERRORDivision by zerosearch-apiinfo642026-01-10 09:39:00
470NullPointerExceptionUnexpected null value at line 42gatewayFatalNULL2026-02-11 10:50:00

Expected output

First 4 of 4 rows
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

Filter one service, then filter the count
SELECT message
FROM err_tracks
WHERE svc_name = 'payment-api'
GROUP BY message
HAVING COUNT(*) > 3
Summing the stored tally
HAVING SUM(count) > 3 adds up the count column, which holds values like 8 and 50 per row, so all ten payment-api messages sail past three.
Counting log entries
HAVING COUNT(*) > 3 counts rows: four messages have four entries and survive, six have three and drop.

Common follow-up questions