Repos with More Builds Than Commits

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

Looking at CI/CD health, find all repos where the number of builds meets or exceeds the number of commits. Show each repo with its build count, ranked from highest to lowest.

More builds than commits. Something is off.

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

repo_commits

Columns: commit_id INTEGER, repo_name TEXT, author TEXT, message TEXT, added INTEGER, removed INTEGER, commit_at TEXT

repo_commits: 10 of 200 rows
commit_idrepo_nameauthormessageaddedremovedcommit_at
3061backend-apibobAdd unit tests1772026-02-02 01:09:00
3122infracharlieRefactor auth module34142026-03-03 02:18:00
3183ml-pipeline charlieUpdate deps51212026-04-04 03:27:00
3244mobile-appdana Improve perf68282026-05-05 04:36:00
3305data-platformfrank Add caching layer 85352026-06-06 05:45:00
3366frontendAliceFix typo in readme102422026-07-07 06:54:00
3427backend-apiBobNULL119492026-08-08 07:03:00
3488infraaliceWIP136562026-09-09 08:12:00
3549ml-pipelinebob153632026-01-10 09:21:00
3610mobile-appcharlieAdd search endpoint170702026-02-11 10:30:00

Expected output

First 5 of 6 rows
repo_namebuild_count
mobile-app34
ml-pipeline34
infra34
frontend32
data-platform32

Worked solution and explanation

Why this problem exists in real interviews

The core skill being tested is self-join, applied to the ci_builds and repo_commits tables in a analytics context. Getting columns like build_id, repo_name, and commit_id right is where most candidates slip. The problem layers in HAVING for post-aggregation filtering as well.


Break down the requirements

Step 1: Join ci_builds to repo_commits

The join connects the two tables on their shared key. This brings the columns needed for filtering and aggregation into a single row set.

Step 2: Deduplicate the result with DISTINCT

SELECT DISTINCT removes duplicate rows from the output. This is necessary when joins or subqueries can produce repeated combinations.


The solution

Having filter for repos with more builds than
SELECT cb.repo_name, COUNT(DISTINCT cb.build_id) AS build_count
FROM ci_builds cb
INNER JOIN repo_commits rc ON cb.repo_name = rc.repo_name
GROUP BY cb.repo_name
HAVING COUNT(DISTINCT cb.build_id) >= COUNT(DISTINCT rc.commit_id)
ORDER BY build_count DESC

Common follow-up questions