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

Worked solution and explanation

This is a recurring time-of-day filter dressed up as an incident review. 'Before noon' is not a moment on the calendar. It is a window that opens every single day the table covers. Anyone can match log_level = 'WARN'. What separates candidates is filtering on the hour component of log_timestamp, not on the timestamp itself. The weaker answer compares log_timestamp to one noon datetime and gets one day's mornings, or everything before that day. The other trap is pulling the hour out as text and comparing it to the number 12, which is not a numeric comparison at all.

Pull the hour out, then compare it

Step 1: Match the level exactly

log_level holds five 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: Treat log_timestamp as text you have to decompose

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

Step 3: Cast, then compare strictly below 12

CAST(... AS INTEGER) < 12 turns '07' into 7 before comparing. The strict inequality is the boundary decision: 12:00:00 is noon, not before it. The seed has WARN rows logged in the 12 o'clock hour, so a <= 12 quietly adds them.

Step 4: Return the rows as stored

No grouping, no ordering, all six columns. Six 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 level match
SELECT log_id, server_name, log_level, message, response_time_ms, log_timestamp
FROM server_logs
WHERE log_level = 'WARN'
    AND CAST(STRFTIME('%H', log_timestamp) AS INTEGER) < 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
CAST(STRFTIME('%H', log_timestamp) AS INTEGER) < 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