Longest Running Pipeline

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
City and County of San Francisco

Problem

An on-call engineer chasing a slow batch window is going through data_pipes, which records every pipeline run along with how long it took. Return the name of the one pipeline behind the longest recorded run.

One pipeline outlasted them all.

Tables

data_pipes

Columns: pipe_id INTEGER, pipe_name TEXT, status TEXT, rows_in INTEGER, rows_out INTEGER, start_at TIMESTAMP, 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 1 of 1 rows
pipe_name
dbt_build

Worked solution and explanation

Why this problem exists in real interviews

This is an argmax wearing an ops-dashboard costume. The question wants the name of the row that holds the extreme, not the extreme itself. Candidates who think in aggregates write SELECT MAX(dur_secs) FROM data_pipes and get back 2282, a bare number with nothing to say which pipeline owns it. The skill being probed is recovering the owning row. The graded result is one name: the pipeline at the top of the longest-first ordering, 'dbt_build' on this seed.


Break down the requirements

Step 1: Read from data_pipes

Only 2 of its 7 columns matter: dur_secs drives the ordering and pipe_name is the output. status, with its mixed casing, is a distractor and never enters the query.

Step 2: Sort by dur_secs descending, then LIMIT 1

40 of the 200 rows have no dur_secs, and a run with no recorded duration cannot be the longest. Here DESC sorts NULL below every number, so those rows fall to the bottom. Postgres puts NULLs first under DESC, so add NULLS LAST when you port this.

Step 3: Project only pipe_name

Select pipe_name and nothing else. The grader compares column names and order, so carrying dur_secs along to eyeball it is a schema mismatch.


The solution

Direct sort for longest pipeline
SELECT pipe_name
FROM data_pipes
ORDER BY dur_secs DESC
LIMIT 1
The aggregate reflex
-- Returns the max duration: a number, not a pipeline
SELECT MAX(dur_secs) FROM data_pipes;

-- Standard SQL rejects this: pipe_name is neither grouped nor aggregated
SELECT pipe_name, MAX(dur_secs) FROM data_pipes;

The first returns a duration; the second leans on engine leniency to pick a name.

Window or join back
ROW_NUMBER() OVER (ORDER BY dur_secs DESC) numbers every row just to keep one. A MAX subquery joined back costs a second pass and returns every run tied at the top.
Sort and take one
ORDER BY dur_secs DESC LIMIT 1 does the least work and reads as the question: longest first, take the top.

Common follow-up questions