Clean Exit

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
Meta

Problem

Return the job IDs of every priority-1 job in batch_jobs that finished successfully. Different schedulers log the same status with different capitalization, and a completed job counts as cleared however its status was written.

Priority one. Only some of them made it to the end.

Tables

batch_jobs

Columns: job_id INTEGER, job_name TEXT, status TEXT, rows_done INTEGER, started TEXT, ended TEXT, priority INTEGER, retries INTEGER

batch_jobs: 10 of 200 rows
job_idjob_namestatusrows_donestartedendedpriorityretries
1047user_exportrunning972026-02-02 01:00:002026-02-02 03:11:0030
1094data_cleanupfailed1942026-03-03 02:00:002026-03-03 04:22:0060
1141email_blastqueued2912026-04-04 03:00:002026-04-04 05:33:0090
1188index_rebuildcanceled3882026-05-05 04:00:00NULL20
1235cache_warmCompleted4852026-06-06 05:00:002026-06-06 07:55:0050
1282log_archiveFAILEDNULL2026-07-07 06:00:002026-07-07 08:06:0080
1329metric_aggcompleted6792026-08-08 07:00:002026-08-08 09:17:0010
1376backup_dbrunning7762026-09-09 08:00:00NULL40
1423sync_elasticfailed8732026-01-10 09:00:002026-01-10 11:39:0075
1470daily_reportqueued9702026-02-11 10:00:002026-02-11 12:50:0000

Expected output

First 5 of 6 rows
job_id
1329
3209
4619
10005707
10005747

Worked solution and explanation

What this really tests

This is a dirty-data filter wearing a status-lookup costume. The status column records the same job state in more than one casing: some rows say 'completed', others say 'Completed'. Anyone can type WHERE status = 'completed'. What the interviewer is watching for is whether you noticed the casing and normalized it before comparing. Miss it and you silently drop every priority-1 job that happened to be logged with a capital C: on this data that is two of the six, and the on-call check under-counts the very jobs it exists to verify.


The trap: inconsistent casing

Look at the sample rows before writing anything. 'completed' and 'Completed' both appear, and 'failed' shows up as both 'failed' and 'FAILED'. That is production reality: different services write the same state with different casing. A case-sensitive equality test treats them as different values, so the result depends on which service happened to log the row. That is a correctness bug, not a style choice.

Case-sensitive (wrong here)
WHERE status = 'completed' matches only the lowercase rows and drops every job written as 'Completed'. The count silently changes with the data's casing.
Normalized (correct)
WHERE LOWER(status) = 'completed' folds every casing to one form, so a completed job counts as completed no matter which service logged it.

Building the filter

Step 1: Normalize the status, then match

Fold status to a single casing with LOWER() and compare to 'completed'. This is the one decision that separates a correct answer from a plausible-looking wrong one on this data.

Step 2: Restrict to priority 1

Add the priority = 1 predicate. priority is an integer, so there are no casing concerns; a plain equality is exact.

Step 3: Return only the IDs

SELECT job_id returns just the identifiers the on-call check needs, nothing more.


The solution

Case-normalized filter
SELECT job_id
FROM batch_jobs
WHERE LOWER(status) = 'completed'
  AND priority = 1

Common follow-up questions