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

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_namernk
sync_elastic1
metric_agg2
cache_warm3
email_blast4
user_export5

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

Aggregate, rank, then filter the standings
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 rnk

The GROUP BY fixes the grain, the window ranks totals, the outer WHERE cuts by standing.

Ranking runs
Dense-ranks individual rows_done values, so 'daily_report' wins on one 9,700-row run and every name shows up twice.
Ranking job totals
Sums per job_name first, ranks the ten totals and keeps standings 1 to 5: 'sync_elastic', 'metric_agg', 'cache_warm', 'email_blast', 'user_export'.

Common follow-up questions