The Weak Link

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
L4

Problem

The platform team is reviewing CI reliability and wants to understand how often each repository's builds break. For each repository, return the percentage of its builds that ended in a 'failed' status, rounded to two decimals.

Build failures happen. Which repos break the most?

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_namefailure_pct
backend-api23.53
data-platform18.75
frontend25
infra17.65
ml-pipeline17.65

Worked solution and explanation

What this really is

Strip the CI costume off and this is a per-group rate: for each repo, the fraction of builds whose status is 'failed'. Anyone can filter to failed rows and count them, but a raw count of failures ranks the busiest repos on top, not the flakiest. The real move is dividing the failed count by the group's total inside a single GROUP BY. The trap is reaching for COUNT(CASE WHEN status = 'failed' THEN 1 ELSE 0 END): COUNT tallies the ELSE zeros too, so every repo comes back at 100% and the leaderboard is meaningless.


Building the rate

Step 1: Flag failures per repo

Inside each repo group, turn every build into 1 when its status is 'failed' and 0 otherwise, then SUM those flags. That sum is the failed-build count for the repo, computed in the same pass as the total.

Step 2: Turn the count into a rate

Divide the failed sum by COUNT(*) for the group and multiply by 100.0. The 100.0 literal forces float division, so you get 25.0 rather than an integer-truncated 0. ROUND to two decimals.


The solution

Conditional aggregation for failure rate per repo
SELECT repo_name,
       ROUND(100.0 * SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) / COUNT(*), 2) AS failure_pct
FROM ci_builds
GROUP BY repo_name

Common follow-up questions