The Loudest Machines

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

An SRE sizing next quarter's log storage wants to see how 2026's load spread across the servers writing to server_logs. Report how many log entries each server wrote that year, busiest first.

Every server keeps a diary. Find the ones that never stop writing.

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 5 of 11 rows
server_namelog_count
web-prod-0212
worker-0111
monitor-0111
web-prod-0110
cache-0110

Worked solution and explanation

What this really is

Under the capacity-planning story, this is a filtered count per group: carve one calendar year out of a text timestamp, then count every row each server wrote. Everyone writes the GROUP BY. Candidates split on two questions: which rows are in scope, and what gets counted. server_logs spans five years and only part of it falls in 2026, so a count with no year filter inflates every server. About a third of the rows have no response_time_ms, so COUNT(response_time_ms) quietly drops untimed lines that still take up storage.

Step 1: Scope to the year

log_timestamp is text in 'YYYY-MM-DD HH:MM:SS' form. STRFTIME('%Y', log_timestamp) pulls out the four-digit year as text, which you compare against the string '2026'. Comparing the raw column to '2026' matches nothing, because no full timestamp equals a bare year.

Step 2: Count every surviving row

Use GROUP BY server_name with COUNT(*). Every surviving row is one stored line, whatever its log_level and whether or not it was timed, so the row count is the volume. Do not add DISTINCT or a level filter.

Step 3: Busiest first

Sort by log_count descending. Servers tied on volume (two sit at 11, two at 10) may come back in either order. The grader checks that counts never rise down the list. It does not check which tied server is printed first, so no tiebreaker is owed.

Year-scoped volume per server
SELECT server_name, COUNT(*) AS log_count
FROM server_logs
WHERE STRFTIME('%Y', log_timestamp) = '2026'
GROUP BY server_name
ORDER BY log_count DESC

Common follow-up questions