The Opening Run

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
L4
Asked in
Meta

Problem

The data platform team wants to see how many rows each batch job processed on its very first run, as a baseline for measuring throughput improvements over time. Reading down the roster alphabetically by job name, show each job alongside the rows done from that first execution.

Before the tuning, before the records. What the first pass carried home.

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 10 rows
job_namerows_done
backup_db9506
cache_warm4365
daily_reportNULL
data_cleanup194
email_blast1261

Worked solution and explanation

Why this problem exists in real interviews

Interviewers use the batch_jobs table here to probe row numbering within partitions combined with nested subqueries. The columns job_name, status, rows_done force candidates to reason about the correct grain before writing any aggregation.


Break down the requirements

Step 1: Partition by job_name

Number each job's runs chronologically with ROW_NUMBER() partitioned by job_name and ordered by started ascending, so the earliest run gets rn = 1.

Step 2: Filter to rank 1

Keep only the first run per job by filtering rn = 1.


The solution

Row-number for first run row count
SELECT job_name, rows_done
FROM (
    SELECT job_name, rows_done,
           ROW_NUMBER() OVER (PARTITION BY job_name ORDER BY started ASC) AS rn
    FROM batch_jobs
) ranked
WHERE rn = 1
ORDER BY job_name

Common follow-up questions