Top 10 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
- Spotify
Problem
We run a batch-processing platform that logs every scheduled run of each job. Build a leaderboard of the jobs holding the five highest standings by total rows processed, showing each job's name and its standing, busiest first.
The ten biggest batch jobs.
Tables
batch_jobs
Columns: job_id INTEGER, job_name TEXT, status TEXT, rows_done INTEGER, started TIMESTAMP, ended TIMESTAMP, priority INTEGER, retries INTEGER
| job_id | job_name | status | rows_done | started | ended | priority | retries |
|---|---|---|---|---|---|---|---|
| 1047 | user_export | running | 97 | 2026-02-02 01:00:00 | 2026-02-02 03:11:00 | 3 | 0 |
| 1094 | data_cleanup | failed | 194 | 2026-03-03 02:00:00 | 2026-03-03 04:22:00 | 6 | 0 |
| 1141 | email_blast | queued | 291 | 2026-04-04 03:00:00 | 2026-04-04 05:33:00 | 9 | 0 |
| 1188 | index_rebuild | canceled | 388 | 2026-05-05 04:00:00 | NULL | 2 | 0 |
| 1235 | cache_warm | Completed | 485 | 2026-06-06 05:00:00 | 2026-06-06 07:55:00 | 5 | 0 |
| 1282 | log_archive | FAILED | NULL | 2026-07-07 06:00:00 | 2026-07-07 08:06:00 | 8 | 0 |
| 1329 | metric_agg | completed | 679 | 2026-08-08 07:00:00 | 2026-08-08 09:17:00 | 1 | 0 |
| 1376 | backup_db | running | 776 | 2026-09-09 08:00:00 | NULL | 4 | 0 |
| 1423 | sync_elastic | failed | 873 | 2026-01-10 09:00:00 | 2026-01-10 11:39:00 | 7 | 5 |
| 1470 | daily_report | queued | 970 | 2026-02-11 10:00:00 | 2026-02-11 12:50:00 | 0 | 0 |
Expected output
| job_name | rnk |
|---|---|
| sync_elastic | 1 |
| metric_agg | 2 |
| cache_warm | 3 |
| email_blast | 4 |
| user_export | 5 |
Worked solution and explanation
What this really asks
This is a grain problem dressed up as a leaderboard. batch_jobs holds one row per run, but the board holds one row per job, so the first decision is what a row of the answer represents. Anyone can sort and keep five. The trap is ranking runs: the biggest single run in the table belongs to 'daily_report' (9,700 rows), so a run-level board crowns it, yet its runs total only seventh. Fix the grain at job_name with a summed rows_done first, cut by standing rather than by row, and the board falls out.
The moves
Step 1: Fix the grain at one row per job
GROUP BY job_name with SUM(rows_done) turns many runs into one total per job, and that total is the only number the board compares. SUM skips the NULL rows_done values, so a run with no recorded count adds nothing instead of wiping out its job's total.
Step 2: Give each total a standing
DENSE_RANK() OVER (ORDER BY SUM(rows_done) DESC) ranks the per-job totals, not the runs, because the window is evaluated after GROUP BY. Equal totals would share a standing and the next total takes the very next number.
Step 3: Cut at the fifth standing
A window result cannot be filtered in the SELECT that computes it, so the ranking sits in the derived table ranked and the outer WHERE rnk <= 5 keeps standings 1 to 5. A job tied at a kept standing survives, which LIMIT 5 cannot promise.
The solution
SELECT job_name, rnk
FROM (
SELECT job_name,
DENSE_RANK() OVER (ORDER BY SUM(rows_done) DESC) AS rnk
FROM batch_jobs
GROUP BY job_name
) ranked
WHERE rnk <= 5
ORDER BY rnkThe GROUP BY fixes the grain, the window ranks totals, the outer WHERE cuts by standing.
- Ranking runs
- Dense-ranks individual
rows_donevalues, so'daily_report'wins on one 9,700-row run and every name shows up twice. - Ranking job totals
- Sums per
job_namefirst, ranks the ten totals and keeps standings 1 to 5:'sync_elastic','metric_agg','cache_warm','email_blast','user_export'.
Common follow-up questions
- How would you produce the top 3 jobs within each `status`? (Tests moving from a global standing to a per-group one with `PARTITION BY`, and noticing that `status` mixes letter case.)
- How does the answer change if you must return exactly five rows even under ties? (Tests the difference between `DENSE_RANK`, `RANK` and `ROW_NUMBER` at the cutoff.)
- Why does a NULL `rows_done` not need an explicit filter here? (Tests understanding of how `SUM` treats NULL versus how `COUNT(rows_done)` would.)