Behind the Fleet

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
mid
Asked in
Slalom

Problem

The reliability team is combing through server logs to find the machines dragging response times up. Give each server its average response time and keep only the machines slower than the typical server, meaning those sitting above the mean of the per-server averages. List the worst offender at the top and work down to the least sluggish, so the team fixes the slowest machine first.

Every machine keeps its own pace. Find the ones falling short of the average.

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 5 rows
server_nameavg_response_ms
web-prod-01796.5
cdn-edge-us757.5
db-replica-01718.5
cdn-edge-eu699
api-prod-01679.5

Worked solution and explanation

Why this problem exists in real interviews

This looks like a monitoring question, but it is really a grain trap wearing an SRE costume. The baseline is 'the average machine', which means the average OF the per-server averages, not the average over all log rows. Those two numbers differ the moment servers log different volumes: a chatty cache node with 10x the rows would drag a row-level average toward itself and quietly redefine 'typical'. The prompt tells you which one it wants; the skill is expressing that grain in SQL without accidentally collapsing back to the row-level number.


Break down the requirements

Step 1: Roll up to server grain

GROUP BY server_name with AVG(response_time_ms). This is the unit the question cares about: one row per machine, not per log line. Park it in a CTE so the baseline can reuse the same rollup instead of recomputing it.

Step 2: Define the baseline at the server grain

The cutoff is (SELECT AVG(avg_response_ms) FROM server_latency): the average of the per-server averages, so every machine gets one vote. Averaging response_time_ms over the raw rows instead would weight each server by its log volume, which is a different and wrong number.

Step 3: Filter and sort

Keep servers strictly above that scalar, then ORDER BY avg_response_ms DESC so the worst offender surfaces first.


The solution

SERVERS ABOVE THE TYPICAL LATENCY
WITH server_latency AS (
  SELECT server_name,
         AVG(response_time_ms) AS avg_response_ms
  FROM server_logs
  GROUP BY server_name
)
SELECT server_name,
       avg_response_ms
FROM server_latency
WHERE avg_response_ms > (SELECT AVG(avg_response_ms) FROM server_latency)
ORDER BY avg_response_ms DESC

Common follow-up questions