Top Commit Authors by Repo
A hard SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- hard
- Seniority
- L5
Problem
We're building a per-repo contributor leaderboard from our commit history, where an author's weight is the total lines they have added and only authors past 1,750 added lines make the board. For each repo, show its top 3 qualifying authors from the heaviest down, each written as the author's name, their line total, and their share of all the lines added across the entire codebase, rounded to the nearest whole percent. Where a repo has fewer than 3 qualifying authors, label the empty slots (for example 'No second author').
Three authors per repo. The top committers.
Tables
repo_commits
Columns: commit_id INTEGER, repo_name TEXT, author TEXT, message TEXT, added INTEGER, removed INTEGER, commit_at TIMESTAMP
| commit_id | repo_name | author | message | added | removed | commit_at |
|---|---|---|---|---|---|---|
| 3061 | backend-api | bob | Add unit tests | 17 | 7 | 2026-02-02 01:09:00 |
| 3122 | infra | charlie | Refactor auth module | 34 | 14 | 2026-03-03 02:18:00 |
| 3183 | ml-pipeline | charlie | Update deps | 51 | 21 | 2026-04-04 03:27:00 |
| 3244 | mobile-app | dana | Improve perf | 68 | 28 | 2026-05-05 04:36:00 |
| 3305 | data-platform | frank | Add caching layer | 85 | 35 | 2026-06-06 05:45:00 |
| 3366 | frontend | Alice | Fix typo in readme | 102 | 42 | 2026-07-07 06:54:00 |
| 3427 | backend-api | Bob | NULL | 119 | 49 | 2026-08-08 07:03:00 |
| 3488 | infra | alice | WIP | 136 | 56 | 2026-09-09 08:12:00 |
| 3549 | ml-pipeline | bob | 153 | 63 | 2026-01-10 09:21:00 | |
| 3610 | mobile-app | charlie | Add search endpoint | 170 | 70 | 2026-02-11 10:30:00 |
Expected output
| repo_name | best_author | second_best_author | third_best_author |
|---|---|---|---|
| backend-api | bob (2330 lines, 5%) | Bob (1848 lines, 4%) | No third author |
| data-platform | frank (2576 lines, 6%) | Bob (2024 lines, 4%) | No third author |
| infra | charlie (2500 lines, 5%) | alice (1984 lines, 4%) | Alice (1800 lines, 4%) |
| ml-pipeline | dana (2619 lines, 6%) | bob (2120 lines, 5%) | Bob (1936 lines, 4%) |
| mobile-app | eve (2772 lines, 6%) | charlie (2256 lines, 5%) | alice (2072 lines, 4%) |
Worked solution and explanation
What this really is
This is a qualified top-N-per-group wearing a leaderboard costume. Anyone can total SUM(added) per author. The skill is doing 4 things in the right order. First, drop everyone under the 1,750-line floor BEFORE you rank. Second, rank the survivors inside each repo. Third, compute each author's share of the whole codebase's added lines with real division. Fourth, pivot exactly 3 slots across one row, with a label where a repo runs short. Rank before you filter and a light contributor steals a slot. Divide one integer by another and bob's 4.99 percent floors to 4. Skip the COALESCE and a repo with 2 heavy authors shows a NULL where a name belongs.
Working through it
Step 1: Total lines added per author
GROUP BY repo_name, author with SUM(added) AS total_added. This is the expensive scan-and-aggregate, and everything downstream runs on this small grouped result. bob and Bob group separately because they are distinct accounts.
Step 2: Apply the 1,750-line floor before ranking
WHERE total_added >= 1750 on the grouped totals, before the window runs. A below-floor author then never gets a rank number, so they cannot occupy a top-3 slot. It also thins several repos below 3 qualifiers (frontend keeps only alice), which is why the placeholder exists at all.
Step 3: Each author's share of the codebase
An uncorrelated scalar subquery (SELECT SUM(added) FROM repo_commits) gives the codebase-wide total across every repo and author. It and total_added are both integers, so CAST(total_added AS DOUBLE) makes the numerator real before the divide, and ROUND then lands on the nearest whole percent. Drop the cast and the engine divides integer by integer: bob's 4.99 percent in backend-api comes out 4 where the board expects 5.
Step 4: Rank inside each repo
ROW_NUMBER() OVER (PARTITION BY repo_name ORDER BY total_added DESC, author ASC). The author ASC key is a deterministic tiebreak, so 2 equal totals never swap the winner between runs.
Step 5: Pivot ranks 1, 2, 3 into columns
Conditional aggregation: MAX(CASE WHEN rn = 1 THEN ... END) collapses the ranked rows to one row per repo with 3 named columns. Wrap the second and third slots in COALESCE(..., 'No second author') and its third-slot twin so a rank the repo cannot fill reads as a label. Rank one needs no COALESCE: any repo that reaches the board has at least one qualifier.
Step 6: Format the cell
Concatenate author || ' (' || total_added || ' lines, ' || codebase_share_pct || '%)' so a cell reads bob (2330 lines, 5%).
The solution
WITH per_author AS (
SELECT repo_name, author, SUM(added) AS total_added
FROM repo_commits
GROUP BY repo_name, author
),
ranked AS (
SELECT repo_name, author, total_added,
ROUND(100 * CAST(total_added AS DOUBLE) / (SELECT SUM(added) FROM repo_commits)) AS codebase_share_pct,
ROW_NUMBER() OVER (PARTITION BY repo_name
ORDER BY total_added DESC, author ASC) AS rn
FROM per_author
WHERE total_added >= 1750
)
SELECT repo_name,
MAX(CASE WHEN rn = 1 THEN author || ' (' || total_added || ' lines, ' || codebase_share_pct || '%)' END) AS best_author,
COALESCE(MAX(CASE WHEN rn = 2 THEN author || ' (' || total_added || ' lines, ' || codebase_share_pct || '%)' END), 'No second author') AS second_best_author,
COALESCE(MAX(CASE WHEN rn = 3 THEN author || ' (' || total_added || ' lines, ' || codebase_share_pct || '%)' END), 'No third author') AS third_best_author
FROM ranked
GROUP BY repo_name- Integer division (share reads low)
ROUND(100 * total_added / (SELECT SUM(added) FROM repo_commits))divides integers, sobob's 4.99 percent is already4whenROUNDruns and the cell readsbob (2330 lines, 4%).- Cast first, then divide and round
ROUND(100 * CAST(total_added AS DOUBLE) / (SELECT SUM(added) FROM repo_commits))keeps the fraction, so 4.99 rounds to5and the cell readsbob (2330 lines, 5%).
Common follow-up questions
Common follow-up questions
- What changes if 2 authors tie on `total_added` and the spec says keep both? (Forces a `DENSE_RANK` versus `ROW_NUMBER` discussion and shows how the pivot breaks when one slot holds 2 names.)
- How would you measure each share against the author's own repo total instead of the whole codebase? (Probes whether the candidate swaps the codebase-wide denominator for a per-repo one and reuses the qualifying CTE instead of the raw table.)
- What if you need the top 10 per repo, not the top 3? (Tests whether the pivot pattern scales, or whether they switch to a long format with one row per repo and rank.)
- How would you exclude bot authors like `dependabot[bot]`? (Opens up a `dim_author` join or a name-pattern filter instead of a hardcoded list.)