Last Server Activity

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
DoorDash

Problem

A server shows up in server_logs once for every line it writes, but we only care about the last time each one spoke. List every server with its most recent log timestamp, most recently active first, so the ones that have gone quiet sink to the bottom.

Each server's last heartbeat.

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_namelast_activity
cdn-edge-us2026-09-09 08:56:44
api-prod-012026-09-08 11:05:35
worker-012026-09-07 14:14:26
web-prod-022026-09-06 21:15:45
cache-012026-09-06 17:59:41

Worked solution and explanation

This is a last-seen heartbeat check dressed up as a log query. server_logs holds one row per line a server wrote, so every server repeats many times, and the answer wants exactly one row per server carrying its latest log_timestamp. Anyone can sort the log newest first. The skill is collapsing each server to a single last-seen time and then sorting the servers by that time. Sort the raw lines instead and the first row looks right, but the list repeats busy servers and buries the quiet ones you were asked to find.

Walking to the answer

Step 1: Settle the grain first

One row per server is the output grain, so server_name is the only key you collapse on. Everything else on a log line (its log_level, its message, its response_time_ms) is noise for this question. Put any of it in the key and a server splits into several rows.

Step 2: Take the latest time per server

MAX(log_timestamp) per server_name is the last time each server spoke. You need only the time, not the rest of that line, so there is no row to pick and nothing to tie-break.

Step 3: Sort by the collapsed value

Name the aggregate once as last_activity and sort by it descending. The freshest server leads and the silent ones fall to the bottom, which is exactly where an on-call engineer reads.

The query

Last activity per server, freshest first
SELECT server_name, MAX(log_timestamp) AS last_activity
FROM server_logs
GROUP BY server_name
ORDER BY last_activity DESC
Sorting the raw lines
SELECT server_name, log_timestamp FROM server_logs ORDER BY log_timestamp DESC returns all 200 lines. cdn-edge-us sits on top and then reappears further down, and a quiet server only shows up after you scroll past every busy one.
Collapsing first
Grouping on server_name with MAX(log_timestamp) returns 11 rows, one per server. The last row is the server that has been silent the longest.

Common follow-up questions