Services With Most Error Occurrences

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
ESPN

Problem

The reliability team is auditing the first half of the year's error logs, where each row records how many times a given error fired. For each service, surface its single highest occurrence count, biggest spike first.

The noisiest services.

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 5 of 6 rows
svc_namemax_error_count
search-api2201
user-svc2190
payment-api2179
auth-svc2102
worker2091

Worked solution and explanation

What this is really asking

Beneath the incident-review framing this is a per-service MAX, not a sum. The count column already stores how many times each error fired, so a service's 'peak' is the single largest count it logged, MAX(count). The trap is reading 'error occurrences' as a total and writing SUM(count) or COUNT(*): both answer a different question and re-sort the board. One row also logs a null count; MAX steps over it for free, while a hand-rolled peak or a SUM would have to reckon with it.


Scope, collapse, order

Step 1: Keep the first half of the year

CAST(strftime('%m', first_at) AS INTEGER) <= 6 pulls the month out of the first_at timestamp and keeps January through June. Do it in WHERE so those rows are gone before the grouping runs, not filtered after the fact. This window matters: several services peak later in the year, so scoping it changes their answer.

Step 2: Collapse to one row per service

GROUP BY svc_name gives one row per service, and MAX(count) reads that service's single busiest spike. Group on svc_name exactly as stored: the messy casing in severity is a decoy, and severity never enters the answer.

Step 3: Order the board

ORDER BY max_error_count DESC puts the loudest service on top. Over this window every service's peak is a distinct number, so the single sort key already yields a total order: two correct answers cannot disagree on row placement, and no tiebreak is needed.


The solution

Per-service peak over the first half of the year
SELECT svc_name, MAX(count) AS max_error_count
FROM err_tracks
WHERE CAST(strftime('%m', first_at) AS INTEGER) <= 6
GROUP BY svc_name
ORDER BY max_error_count DESC

Common follow-up questions