Morning Warning Logs

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
Lyft

Problem

After noticing pre-business-hours warning spikes, the SRE team wants to review every WARN-level server log entry that fired before noon, along with the hour of day it fired in. Return all fields.

Warnings before noon.

Tables

server_logs

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

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 20 rows
log_idserver_namelog_levelmessageresponse_time_mslog_timestamplog_hour
137web-prod-02WARNConnection timeout after 30s232026-02-02 01:07:131
322cache-01WARNRate limit exceeded for IP 192.168.1.x882026-07-07 06:42:186
507web-prod-01WARNRetrying request (attempt 3/5)NULL2026-03-12 11:17:2311
1062db-primaryWARNDisk space low: 10% remainingNULL2026-09-03 02:02:382
1247cdn-edge-euWARNUser authentication failed4132026-05-04 07:37:437

Worked solution and explanation

This is a recurring time-of-day filter dressed up as an incident review. 'Before noon' is a window that opens on every date the table covers, so it lives in the hour of log_timestamp and nowhere else. Anyone can match log_level = 'WARN'. What separates candidates is decomposing the timestamp: the weaker answer compares log_timestamp to one noon datetime and gets everything before a single cut-off. The second trap sits in the output. The hour STRFTIME hands back is the text '07', and the review wants the number 7.

Pull the hour out, then use it twice

Step 1: Match the level exactly

log_level holds 5 values: 'WARN', 'ERROR', 'DEBUG', 'CRITICAL' and 'INFO'. The review is about warnings, so it is an equality on 'WARN'. Sweeping in 'ERROR' or 'CRITICAL' because they are 'at least as bad' answers a different question.

Step 2: Decompose log_timestamp

Every value is shaped like '2026-02-02 01:07:13'. The hour you need is a sub-field of each row, and it repeats every day. STRFTIME('%H', log_timestamp) hands it back as the zero-padded string '01'.

Step 3: Keep hours strictly below '12'

Zero-padded hours sort correctly as text, so STRFTIME('%H', log_timestamp) < '12' keeps '00' through '11'. 12:00:00 is noon itself and falls outside the window. The seed has WARN rows logged in the 12 o'clock hour, so a <= '12' quietly adds them.

Step 4: Return the hour as a number

CAST(STRFTIME('%H', log_timestamp) AS INTEGER) AS log_hour turns '07' into 7, which is what the preview shows and what an hour axis can plot. Left as text, every hour before 10 keeps its leading zero and the column stops matching.

Step 5: Return the rows as stored

No grouping and no ordering: the 6 stored columns plus log_hour. 6 of the 20 matching rows have a NULL response_time_ms. They are still warnings that fired before noon, so nothing should touch that column.

Time-of-day filter with the hour returned
SELECT
    log_id,
    server_name,
    log_level,
    message,
    response_time_ms,
    log_timestamp,
    CAST(STRFTIME('%H', log_timestamp) AS INTEGER) AS log_hour
FROM server_logs
WHERE log_level = 'WARN'
    AND STRFTIME('%H', log_timestamp) < '12'
One noon boundary
log_timestamp < '2026-09-12 12:00:00' keeps every warning up to one particular noon, mornings and afternoons alike. Drop the date and compare to a bare '12:00:00', and you are comparing strings that start with a year, which means nothing at all.
Hour of day
STRFTIME('%H', log_timestamp) < '12' asks the same question of every row regardless of its date. That recurring window is what 'before noon' means here.

Common follow-up questions