Highest Throughput Pipelines

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

Problem

How many rows did the biggest single run in data_pipes put out in each quarter of 2025? A capacity analyst is sizing hardware against those quarterly peaks, and every run counts whatever its status.

The pipes that carry the most water.

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 4 of 4 rows
quarter_startpeak_rows_out
2025-017178
2025-048633
2025-077663
2025-109118

Worked solution and explanation

What this really is

This is a calendar-bucket peak dressed up as capacity planning. Anyone writes MAX(rows_out). The bucket is what separates candidates: start_at is a timestamp, and the quarter has to be built from it, inside a one-year fence. Format the run's own month instead of truncating to the quarter and every quarter splits into three rows. Drop the fence and you get every quarter the table has ever seen, each one looking perfectly plausible.


Build it

Step 1: Fence the runs to 2025

EXTRACT(YEAR FROM start_at) = 2025 keeps only the year the analyst is sizing from. It is a row predicate, so it lives in WHERE and runs before anything is bucketed. Without it, the runs from every other year in data_pipes land in the output too.

Step 2: Snap each run to its quarter

DATE_TRUNC('quarter', start_at) maps a run on May 17 to April 1. Every run in April, May and June lands on the same value, which is exactly the bucket you want in the GROUP BY. Group on the truncated timestamp, not on a string built from the raw one.

Step 3: Take the peak, then label the bucket

MAX(rows_out) is the single biggest run in the bucket, and it skips NULL, so a run with no logged output never drags a peak down. TO_CHAR(DATE_TRUNC('quarter', start_at), 'YYYY-MM') turns the quarter start into the label the preview shows; it is a function of the grouped bucket, so it is legal in the SELECT. Leave status alone: a failed run still hit the system.

The solution

Year fence, quarter bucket, peak
SELECT
    TO_CHAR(DATE_TRUNC('quarter', start_at), 'YYYY-MM') AS quarter_start,
    MAX(rows_out) AS peak_rows_out
FROM data_pipes
WHERE EXTRACT(YEAR FROM start_at) = 2025
GROUP BY DATE_TRUNC('quarter', start_at)
Format, then group
Grouping on TO_CHAR(start_at, 'YYYY-MM') buckets by the run's own month. The output looks like quarter labels, but there are up to twelve of them and each peak covers a third of a quarter.
Truncate, then format
DATE_TRUNC('quarter', start_at) collapses the three months first, and TO_CHAR only dresses the result. Four buckets, each holding every run of its quarter.
A month posing as a quarter
SELECT
    TO_CHAR(start_at, 'YYYY-MM') AS quarter_start,
    MAX(rows_out) AS peak_rows_out
FROM data_pipes
WHERE EXTRACT(YEAR FROM start_at) = 2025
GROUP BY TO_CHAR(start_at, 'YYYY-MM')

Runs cleanly and returns monthly peaks under quarter-shaped labels.

Common follow-up questions