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
| pipe_id | pipe_name | status | rows_in | rows_out | start_at | dur_secs |
|---|---|---|---|---|---|---|
| 131 | etl_users | failed | 137 | 97 | 2026-02-02 01:00:00 | 28 |
| 162 | etl_events | running | 274 | 194 | 2026-03-03 02:00:00 | 51 |
| 193 | sync_crm | queued | 411 | 291 | 2026-04-04 03:00:00 | 74 |
| 224 | agg_daily | skipped | 548 | 388 | 2026-05-05 04:00:00 | 97 |
| 255 | agg_weekly | Success | 685 | 485 | 2026-06-06 05:00:00 | NULL |
| 286 | export_s3 | FAILED | 822 | 582 | 2026-07-07 06:00:00 | 143 |
| 317 | import_csv | success | NULL | 679 | 2026-08-08 07:00:00 | 166 |
| 348 | ml_train | failed | 1096 | 776 | 2026-09-09 08:00:00 | 189 |
| 379 | dbt_build | running | 1233 | NULL | 2026-01-10 09:00:00 | 212 |
| 410 | etl_orders | queued | 1370 | 970 | 2026-02-11 10:00:00 | NULL |
Expected output
| 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
SELECT pipe_name
FROM data_pipes
ORDER BY dur_secs DESC
LIMIT 1-- 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. AMAXsubquery joined back costs a second pass and returns every run tied at the top.- Sort and take one
ORDER BY dur_secs DESC LIMIT 1does the least work and reads as the question: longest first, take the top.
Common follow-up questions
- Two pipelines tie on the maximum `dur_secs`. Which one does your query return, and how would you return all of them? (Tests tie awareness: add a tie-break key to `ORDER BY`, or filter on `dur_secs = (SELECT MAX(dur_secs) FROM data_pipes)` to keep every tied row.)
- What happens to your result on an engine that sorts NULL first under `DESC`? (Tests NULL placement in sorts: the engine default versus an explicit `NULLS LAST`.)
- How would you confirm duplicate rows in `data_pipes` aren't inflating a pipeline's apparent run history? (Tests data quality checks, such as comparing `COUNT(*)` with `COUNT(DISTINCT pipe_id)`.)
- If `data_pipes` grows to hundreds of millions of rows, how would you partition or index on `start_at` to keep this cheap? (Tests partitioning time-series data and pruning to a recent window before the sort.)