Last Five Batch Jobs

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
Visa

Problem

Recency here is judged by job ID, not by start time: the highest IDs are the newest jobs. Pull the five newest records from batch_jobs in full for the data platform team's tail check, newest to oldest.

The last five. A quick tail check.

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 5 rows
job_idjob_namestatusrows_donestartedendedpriorityretries
10005800daily_reportfailed97002026-01-17 04:00:00NULL00
10005799sync_elasticrunning96032025-03-16 03:00:002025-03-16 05:09:0075
10005798backup_dbcompleted95062024-02-03 02:00:002024-02-03 04:58:0040
10005797metric_aggFAILED94092023-01-14 01:00:002023-01-14 03:47:0010
10005796log_archiveCompletedNULL2022-12-13 00:00:00NULL80

Worked solution and explanation

This is a top-N on a stated key, dressed up as an ops tail check. Everyone writes the cap at five. What separates candidates is reading 'most recent by job ID' literally and sorting on job_id, not on started, which looks like the natural recency column. On this table the two disagree: the five highest IDs started anywhere from 2022 to 2026. A sort on started descending returns five different jobs from September 2026, and the answer misses on every row.

Walking it

Step 1: Take the key the business named

'Most recent by job ID' is the ordering contract. job_id is unique, so sorting it descending yields one unambiguous newest-to-oldest sequence with no tie to settle.

Step 2: Sort before you cap

ORDER BY job_id DESC decides which five rows survive; LIMIT 5 only counts them off. Without the sort the engine hands back whatever it scans first, here the oldest IDs starting at 1047, and nothing promises even that.

Step 3: Project the record column by column

Name all eight columns instead of SELECT *. The result keeps the preview's column order, and it does not silently change shape when someone adds a column to batch_jobs.

`ORDER BY job_id DESC` then `LIMIT 5`
SELECT job_id, job_name, status, rows_done, started, ended, priority, retries
FROM batch_jobs
ORDER BY job_id DESC
LIMIT 5
Sort by start time
ORDER BY started DESC LIMIT 5 returns the five latest starts, all in September 2026. It is a reasonable query for a question nobody asked.
Sort by job ID
ORDER BY job_id DESC LIMIT 5 returns IDs 10005800 down to 10005796, whose starts run from 2026 back to 2022. That is what the task defined as newest.

Common follow-up questions