Server With Most Errors

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
L3
Asked in
Spotify

Problem

During an incident, the on-call SRE needs the one server carrying the most logged errors, and if two are running neck and neck, the tie goes to whichever server name comes first alphabetically. Show that server name and its error count.

One server stands out. Not in a good way.

Tables

server_logs

Columns: log_id INTEGER, server_name TEXT, log_level TEXT, message TEXT, response_time_ms INTEGER, log_timestamp TEXT

server_logs: 10 of 200 rows
log_idserver_namelog_levelmessageresponse_time_mslog_timestamp
137web-prod-02WARNConnection timeout after 30s232026-02-02 01:07:13
174api-prod-01ERRORDatabase query slow: 2.5sNULL2026-03-03 02:14:26
211api-prod-02DEBUGCache miss for key user_123492026-04-04 03:21:39
248db-primaryCRITICALMemory usage at 85%02026-05-05 04:28:52
285db-replica-01INFOSSL certificate expires in 7 daysNULL2026-06-06 05:35:05
322cache-01WARNRate limit exceeded for IP 192.168.1.x882026-07-07 06:42:18
359worker-01ERRORDisk space low: 10% remaining1012026-08-08 07:49:31
396cdn-edge-usDEBUGNew deployment v2.3.1 startedNULL2026-09-09 08:56:44
433cdn-edge-euCRITICALHealth check passed1272026-01-10 09:03:57
470monitor-01INFOFailed to connect to upstream1402026-02-11 10:10:10

Expected output

First 1 of 1 rows
server_nameerror_count
api-prod-014

Worked solution and explanation

This is an argmax with a tie hiding in it, dressed up as incident triage. Anyone can count ERROR lines per server. What separates candidates is noticing that the top count is shared: on this data nine servers sit at 4 errors each, and the only thing that decides the answer is the alphabetical rule. Write LIMIT 1 without it and the database hands back whichever tied server it happened to read first. Skip the log_level = 'ERROR' filter and you send the on-call to web-prod-02, the noisiest server overall with 20 lines, which is not the one throwing errors.

Walk it out loud

Step 1: Keep only ERROR lines

log_level holds five severities, and CRITICAL and WARN look like trouble too. The question counts errors, so filter to log_level = 'ERROR' in WHERE before anything is counted. Counting every line answers 'busiest server' instead, which on this data is web-prod-02.

Step 2: Count per server_name

GROUP BY server_name with COUNT(*) gives each server its own tally. Every row is one log line and log_id never repeats, so counting rows is counting errors.

Step 3: Sort by the count, then by the name

ORDER BY error_count DESC puts the biggest tallies first, and server_name as the second key decides among the tied ones. It ascends by default, which is exactly 'first alphabetically'.

Step 4: Cut to one row

LIMIT 1 keeps the winner. Because the name key settles every tie among the nine servers at 4, the cut is deterministic: api-prod-01, every run.

The solution

The server with the most `ERROR` lines, ties to the first name
SELECT server_name, COUNT(*) AS error_count
FROM server_logs
WHERE log_level = 'ERROR'
GROUP BY server_name
ORDER BY error_count DESC, server_name
LIMIT 1
Max of the counts
MAX(error_count) over the per-server tallies returns 4, the right number with no server attached. Putting server_name beside it without grouping is either rejected or pairs the max with an arbitrary name.
Sort and cut
Sorting the grouped rows and keeping the first carries server_name along with its own count, and the name key makes the pick repeatable.

Common follow-up questions