What Never Arrives

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
mid
Asked in
Verizon

Problem

Each ingestion job logs its runs in data_pipes and stamps its own status, so a run that finished shows up as 'success' in whatever mix of upper and lower case that job uses. The data platform team wants every pipeline with a successful run and the records it lost across those runs between what it read and what it wrote, biggest losses first. A run missing either count says nothing about its loss, and a pipeline whose successful runs all miss one still belongs at the bottom with its loss left blank.

Every run reads more than it delivers. Find the pipelines bleeding the most between the door and the destination.

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_namerecords_dropped
export_s39760
dbt_build8640
etl_events7520
agg_weekly6400
ml_train5440

Worked solution and explanation

What this problem really is

This is a per-group sum of a derived quantity dressed up as an ingestion audit. Anyone can write the GROUP BY and the SUM. What separates candidates is normalizing status before the aggregate runs. The jobs stamp both 'success' and 'Success', and an exact match against one spelling keeps only some of the finished runs. On this data the cost is stark: every lowercase 'success' run is missing rows_in, so WHERE status = 'success' hands back a blank loss for every pipeline. The query runs without error and the audit is empty.


Break down the requirements

Step 1: Scope to finished runs, ignoring case

status mixes 'Success' and 'success' alongside 'failed', 'FAILED', 'running', 'queued' and 'skipped'. LOWER(status) = 'success' keeps both spellings of a finished run and nothing else. The normalization belongs on the column, because the column is where the mixed casing lives.

Step 2: Subtract inside the aggregate

A run's loss is rows_in - rows_out, and SUM(rows_in - rows_out) totals it per pipeline. If either count is missing, the difference is NULL and SUM skips it, which is exactly what the statement asks for: such a run says nothing about loss. Do not substitute zero for the missing counts. A missing rows_in would then subtract that run's whole rows_out.

Step 3: Keep the pipeline with no usable counts

One pipeline has successful runs, but each of them is missing a count, so its SUM is NULL rather than 0. The group still exists, so it stays in the result, and ORDER BY records_dropped DESC puts that blank after every real number. Filtering incomplete runs out in WHERE would make the pipeline vanish, and the statement rules that out.


The solution

Records dropped per pipeline
SELECT pipe_name,
       SUM(rows_in - rows_out) AS records_dropped
FROM data_pipes
WHERE LOWER(status) = 'success'
GROUP BY pipe_name
ORDER BY records_dropped DESC
Exact match
WHERE status = 'success' keeps only the lowercase runs, and on this data none of them logged rows_in. Every pipeline comes back with a blank loss.
Normalized match
WHERE LOWER(status) = 'success' keeps 'Success' and 'success' alike, so the 'Success' runs that logged both counts reach the sum.

Common follow-up questions