Kings for a Day

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L5

Problem

We run a batch-processing platform where jobs record how many rows they moved and the time they started. For each calendar day, find the job that moved the most rows, and when two jobs tie for a day's highest count, report all of them. Return the day, the job name, and its row count, earliest day first.

Every day, one job moves more rows than all the rest.

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 157 rows
job_datejob_namerows_done
2022-01-02user_export97
2022-01-06user_export5917
2022-02-03log_archive8342
2022-03-24user_export4947
2022-04-17log_archive1552

Worked solution and explanation

What this is really asking

This is a per-day leaderboard where the crown can be shared. Anyone can get each day's biggest count with a GROUP BY and MAX(rows_done), but the moment you aggregate, job_name is gone, and recovering it means a second pass over batch_jobs joined on the day and the count. The real trap is the shortcut in the other direction: number each day's rows and keep row one. It reads like the same answer, and on any day where two jobs share the top count it quietly crowns one and drops the other without an error. Score every job against its own day, keep everyone at the top, and ties come out for free.


Break down the requirements

Step 1: Bucket by date, ignore uncounted jobs

Cast started to a date so a job at 23:59 and one at 00:01 land in different buckets, and drop rows whose rows_done is null: a job that logged no count cannot be the day's biggest mover.

Step 2: Rank within each day

RANK() OVER (PARTITION BY DATE(started) ORDER BY rows_done DESC) restarts the numbering for every day. Jobs that share the day's top count all get rank 1, so a day can crown more than one winner, which is what the prompt asks for.

Step 3: Keep rnk = 1, earliest day first

Window functions cannot appear in WHERE, so the rank lives in a CTE. The outer query filters rnk = 1 and sorts by job_date.


The solution

Top job per day
WITH ranked AS (
  SELECT
    DATE(started) AS job_date,
    job_name,
    rows_done,
    RANK() OVER (
      PARTITION BY DATE(started)
      ORDER BY rows_done DESC
    ) AS rnk
  FROM batch_jobs
  WHERE rows_done IS NOT NULL
)
SELECT job_date, job_name, rows_done
FROM ranked
WHERE rnk = 1
ORDER BY job_date

Common follow-up questions

Common follow-up questions