Pipeline Run History

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
ESPN

Problem

For each pipeline in data_pipes, show when it first ran, when it most recently ran, and in how many different months it was active.

The lineage trail.

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

Expected output

First 5 of 10 rows
pipe_namefirst_runlast_runactive_months
agg_daily2025-02-15 14:00:002026-09-05 20:00:0015
agg_weekly2026-01-18 21:00:002026-09-08 11:00:009
dbt_build2025-01-22 01:00:002026-09-06 17:00:0015
etl_events2023-02-03 02:00:002026-09-07 14:00:0015
etl_orders2026-01-07 18:00:002026-09-07 18:00:009

Worked solution and explanation

What this problem is really asking

Under the pipeline-monitoring costume, this is a distinct-count over a truncated timestamp. The real question: across how many separate months was each pipeline active? Anyone can GROUP BY pipe_name and hang a COUNT(*) off it, but that counts every run, not months. The skill being probed is seeing that start_at is a timestamp, bucketing it to a month, and counting the DISTINCT buckets. Miss it and etl_orders reads as dozens of months active when it actually ran across only 9.


Why COUNT(*) lies here

Each row in data_pipes is one run, and every run carries its own start_at instant. So COUNT(*) and COUNT(DISTINCT start_at) are effectively the SAME number: the run count. Neither one answers 'how many months'. Two runs that both happened in '2026-09' are two rows but one active month, and the only way to collapse them is to truncate the timestamp to its year-month first, then dedup.


Building it

Step 1: Group by pipeline

Collapse the runs to one row per pipeline with GROUP BY pipe_name. Everything else is an aggregate over that pipeline's runs.

Step 2: Bound the run window

MIN(start_at) is the first run and MAX(start_at) is the most recent. These stay full timestamps: the run window is an instant, not a date.

Step 3: Count distinct active months

COUNT(DISTINCT strftime('%Y-%m', start_at)) gives the number of separate months the pipeline was active, folding repeated runs in a month into one.

Distinct active months per pipeline
SELECT
    pipe_name,
    MIN(start_at) AS first_run,
    MAX(start_at) AS last_run,
    COUNT(DISTINCT strftime('%Y-%m', start_at)) AS active_months
FROM data_pipes
GROUP BY pipe_name

Common follow-up questions