The One That Flew

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
ESPN

Problem

Surface the date and duration of the shortest completed build in ci_builds, the record the CI/CD team is adopting as its performance baseline, keeping in mind that a build only has a duration once it actually finishes. When several builds share that shortest time, the earliest of them sets the baseline.

The fastest build ever. When did it happen?

Tables

ci_builds

Columns: build_id INTEGER, repo_name TEXT, branch TEXT, status TEXT, dur_secs INTEGER, trigger TEXT, built_at TEXT

ci_builds: 10 of 200 rows
build_idrepo_namebranchstatusdur_secstriggerbuilt_at
5047backend-apidevelopfailed34pull_request2026-02-02 01:13:00
5094infrafeature/authcanceled53schedule2026-03-03 02:26:00
5141ml-pipelinefix/login-bugrunning72manual2026-04-04 03:39:00
5188mobile-apprelease/v2.0queued91tag2026-05-05 04:52:00
5235data-platformhotfix/paymentsuccess110webhook2026-06-06 05:05:00
5282frontendfeature /spacesfailedNULLpush2026-07-07 06:18:00
5329backend-apimaincanceled148pull_request2026-08-08 07:31:00
5376infradeveloprunning167schedule2026-09-09 08:44:00
5423ml-pipelinefeature/authqueued186manual2026-01-10 09:57:00
5470mobile-appfix/login-bugsuccess205tag2026-02-11 10:10:00

Expected output

First 1 of 1 rows
built_atmin_duration
2026-05-12 23:35:0020

Worked solution and explanation

What this really tests

Underneath the 'performance baseline' framing this is an argmin: you need the whole row that owns the smallest duration, not just the smallest number. Two things bite. First, dur_secs is NULL for builds that never finished, and the status label will not tell you which those are: a 'failed' build often carries a real duration, so the duration itself is the only honest signal. In SQLite NULL sorts as the smallest value, so ORDER BY dur_secs ASC puts an unfinished build right at the top. Second, the shortest time on this data is held by more than one build, so without a second sort key the record date is whichever tied row the engine happens to hand back. Anyone can write the sort; the separation is the NULL guard plus a tie rule that lands on the earliest built_at.


Getting there

Step 1: Drop the builds that never finished

A NULL dur_secs is not a fast build, it is an incomplete one. Restrict to dur_secs IS NOT NULL first, otherwise the smallest-sorts-first behavior of NULL hands you an unfinished build as the record.

Step 2: Order by duration, then break the tie

Sort the survivors by dur_secs ASC and take the top row with LIMIT 1. Add built_at ASC as a second sort key so that when two builds tie on duration, the earliest one is returned every time instead of an arbitrary row.


The solution

Fastest completed build
SELECT built_at, dur_secs AS min_duration
FROM ci_builds
WHERE dur_secs IS NOT NULL
ORDER BY dur_secs ASC, built_at ASC
LIMIT 1;

Common follow-up questions