The Ones Worth Paging

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
Google

Problem

The SRE team is building a triage chart covering the three severity levels that call for action: CRITICAL, ERROR, and WARN. List each of these levels in alphabetical order alongside the total number of log entries recorded for it.

Three levels wake someone up at night. Tally each one.

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 3 of 3 rows
log_leveltotal_count
CRITICAL40
ERROR40
WARN40

Worked solution and explanation

What this is really testing

This is a filtered bucket count dressed up as an SRE triage chart. The skill being probed is restricting rows to the three levels that call for action before you count them. Anyone can write the group-and-count; the tell is whether you read 'the three severity levels' as a hard filter or as scene-setting. Skip the filter and server_logs hands back five rows, with 'INFO' and 'DEBUG' sitting on a chart meant for paging, and the answer fails on row count before a single value is compared. The second thing the interviewer watches: do you ask about a time window before you agree to scan every partition of a 200M-row table.


Break down the requirements

Step 1: Restrict to the three actionable levels

Use WHERE log_level IN ('CRITICAL', 'ERROR', 'WARN'). It runs before the grouping, so 'INFO' and 'DEBUG' never become groups at all. IN reads better than a chain of ORs, and the planner treats them the same.

Step 2: Group on log_level

One row per level. GROUP BY log_level is the only correct grain. No DISTINCT, no window.

Step 3: Count rows per group

COUNT(*) aliased to total_count. COUNT(log_level) gives the same answer here because the column is never null inside the filter, but COUNT(*) says what you mean: every entry.

Step 4: Order deterministically

ORDER BY log_level returns the levels in alphabetical order: CRITICAL, ERROR, WARN. Without it, partition-parallel scans return groups in arbitrary order.


The solution

SEVERITY COUNTS
SELECT log_level, COUNT(*) AS total_count
FROM server_logs
WHERE log_level IN ('CRITICAL', 'ERROR', 'WARN')
GROUP BY log_level
ORDER BY log_level

COMMON FOLLOW-UP QUESTIONS

Common follow-up questions