Build Success vs Failure by Repo

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
Tesla

Problem

The engineering lead needs a per-repo build health snapshot before freezing deploys. For each repository, show how many builds have a status of 'success', how many have a status of 'failure', and the average build duration.

Green versus red, repo by repo.

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 5 of 6 rows
repo_namesuccess_countfailure_countavg_duration
backend-api60416.5882352941176
data-platform80458.75
frontend60NULL
infra60435.5882352941176
mobile-app80473.5882352941176

Worked solution and explanation

What this is really asking

status carries more than two values (success, failure, plus running, canceled, timeout), but avg_duration is over every build. The conditional aggregation has to count two values and average across all of them in one pass.


Break down the requirements

Step 1: Group once per repo

Single GROUP BY repo_name over the 3M row table. Every metric lives in the same aggregate, no joins or subqueries.

Step 2: Branch the counts with CASE

SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END) and the mirror for 'failure'. The ELSE 0 keeps in-flight and canceled builds in the table without inflating either count.

Step 3: Average duration over everything

AVG(dur_secs) runs across all statuses for the repo. No WHERE clause filters them out; the engineering lead wants total throughput, not just terminal builds.


The solution

BUILD SUCCESS VS FAILURE BY REPO
SELECT repo_name,
       SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END) AS success_count,
       SUM(CASE WHEN status = 'failure' THEN 1 ELSE 0 END) AS failure_count,
       AVG(dur_secs) AS avg_duration
FROM ci_builds
GROUP BY repo_name
;

COMMON FOLLOW-UP QUESTIONS

Common follow-up questions