The Days That Line Up

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
General Assembly

Problem

For each pipeline run that finished successfully after January 1, 2026, match it to every batch job that started on the same calendar day. Report each pipeline name paired with a batch job priority, and for that pairing give the shortest, average, and longest pipeline duration in seconds.

No key ties these tables but the calendar. Read it right.

Tables

data_pipes

Columns: pipe_id INTEGER, pipe_name TEXT, status TEXT, rows_in INTEGER, rows_out INTEGER, start_at TEXT, dur_secs INTEGER

data_pipes: 10 of 200 rows
pipe_idpipe_namestatusrows_inrows_outstart_atdur_secs
131etl_usersfailed137972026-02-02 01:00:0028
162etl_eventsrunning2741942026-03-03 02:00:0051
193sync_crmqueued4112912026-04-04 03:00:0074
224agg_dailyskipped5483882026-05-05 04:00:0097
255agg_weeklySuccess6854852026-06-06 05:00:00NULL
286export_s3FAILED8225822026-07-07 06:00:00143
317import_csvsuccessNULL6792026-08-08 07:00:00166
348ml_trainfailed10967762026-09-09 08:00:00189
379dbt_buildrunning1233NULL2026-01-10 09:00:00212
410etl_ordersqueued13709702026-02-11 10:00:00NULL

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 18 rows
pipe_nameprioritymin_durationavg_durationmax_duration
agg_daily23271170.33333333333331937
agg_weekly1NULLNULLNULL
agg_weekly5NULLNULLNULL
dbt_build1205220522052
dbt_build5205220522052

Worked solution and explanation

The mental model

Infra teams ask this to see if you spot a missing foreign key and name it. data_pipes and batch_jobs share no real relationship, just a calendar day. They want to hear you say 'there is no FK here, I am inferring a same-day join' out loud, then handle the many-to-many cartesian fanout without misreading your aggregates. Silent assumption equals silent rejection.


What the aggregates actually measure

Here is the part that catches everyone. This is not a foreign-key join; it is a same-calendar-day match, so it is many-to-many. If one successful agg_daily run on a given day matches three batch jobs at priority 2, the join emits three copies of that run's dur_secs, all tagged priority 2. MIN and MAX shrug this off (a duplicated value never changes the extremes), but AVG does NOT: when several runs of the same pipeline match different numbers of jobs, each run's duration gets weighted by its own fanout, so the average is a fanout-weighted mean, not the plain average of the runs. SUM(dur_secs) or COUNT(*) would be inflated the same way. Being able to explain WHY the average is what it is, rather than just trusting it, is the senior signal here.


The solution

Same-day calendar join, grouped by priority
SELECT
    dp.pipe_name,
    bj.priority,
    MIN(dp.dur_secs) AS min_duration,
    AVG(dp.dur_secs) AS avg_duration,
    MAX(dp.dur_secs) AS max_duration
FROM data_pipes dp
INNER JOIN batch_jobs bj
    ON SUBSTR(dp.start_at, 1, 10) = SUBSTR(bj.started, 1, 10)
WHERE dp.start_at > '2026-01-01'
  AND LOWER(dp.status) = 'success'
GROUP BY dp.pipe_name, bj.priority

Common follow-up questions