Top Batch Job Under Priority 1

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
L3
Asked in
Salesforce

Problem

The data platform team is profiling batch_jobs for throughput leaders: return every job that processed at least 95% as many rows as the single busiest job managed.

Priority one. Top performer.

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_idjob_namepriorityrows_done
5465cache_warm59215
5559metric_agg19409
5606backup_db49506
5653sync_elastic79603
5700daily_report09700

Worked solution and explanation

What this is really testing

Strip off the throughput story and this is a compare-every-row-against-a-table-wide-peak problem. The costume is 'throughput leaders'; the skill is re-deriving the busiest job's row count on the fly and keeping the whole band that runs close to it, not just the single winner. Reach for ORDER BY rows_done DESC LIMIT 1 and you hand back one job; paste a fixed number for the peak and your cutoff rots the moment the data moves.


Working through it

Step 1: Re-derive the peak, do not hardcode it

Compute the busiest job's row count with an uncorrelated scalar subquery: (SELECT MAX(b2.rows_done) FROM batch_jobs b2). It returns one number for the entire table, and because it is MAX, a NULL rows_done can never be the peak. Scale it by 0.95 to get the 5% band, so the cutoff tracks the data instead of a literal like 9215.

Step 2: Keep the whole pack with an inequality

Filter the outer rows with rows_done >= 0.95 * peak rather than sorting and taking the first. The inequality admits every job inside the band, so co-leaders and near-peers all survive. It also drops the null jobs for free: NULL >= anything is never true, so a job with no recorded row count silently falls out.


The solution

Every job within 5% of the peak in `batch_jobs`
SELECT b.job_id, b.job_name, b.priority, b.rows_done
FROM batch_jobs b
WHERE b.rows_done >= 0.95 * (
    SELECT MAX(b2.rows_done)
    FROM batch_jobs b2
  )

One uncorrelated scalar subquery sets the bar; the inequality keeps the pack.


Common follow-up questions