Heavy Hitters
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
Problem
The eng director wants to know which repositories are absorbing the most new code. In repo_commits, total the lines added per repo, then return the repos whose total beats the average lines added per repo, along with that total and how many commits produced it, biggest first.
Some repos never sleep.
Tables
repo_commits
Columns: commit_id INTEGER, repo_name TEXT, author TEXT, message TEXT, added INTEGER, removed INTEGER, commit_at TEXT
| 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 | lines_added | commit_count |
|---|---|---|
| mobile-app | 9056 | 34 |
| ml-pipeline | 8478 | 34 |
| infra | 7900 | 34 |
Worked solution and explanation
What this problem is really testing
This is an aggregate over an aggregate wearing a code-review costume. The director wants repos whose total lines added beats the average total across repos, and the whole game is that 'average total across repos' is a mean of per-repo sums, one level up from the raw commit rows. Anyone can group and sum. The tell is whether you compute the benchmark against the grouped totals or fumble it against the raw rows.
Building it up
Step 1: Collapse to per-repo totals
Group repo_commits by repo_name, emitting SUM(added) as the per-repo total and COUNT(*) as the commit count behind it. Name it in a CTE (repo_volume) because you are about to use the total twice: once for the output rows and once to compute the bar they must clear.
Step 2: Average the totals, not the rows
The threshold is (SELECT AVG(lines_added) FROM repo_volume): the mean of the per-repo totals, computed over the CTE, not the raw table. This scalar subquery is the whole reason you materialized the totals first.
Step 3: Filter strictly above, heaviest first
Keep repos with lines_added > (that average). Strict >, so a repo sitting exactly on the average drops out, then ORDER BY lines_added DESC so the heaviest repo leads.
The solution
WITH repo_volume AS (
SELECT repo_name,
SUM(added) AS lines_added,
COUNT(*) AS commit_count
FROM repo_commits
GROUP BY repo_name
)
SELECT repo_name, lines_added, commit_count
FROM repo_volume
WHERE lines_added > (SELECT AVG(lines_added) FROM repo_volume)
ORDER BY lines_added DESCCommon follow-up questions
- How would you show each repo's total next to the overall average in the same result? (Tests `AVG() OVER ()` as a window function to avoid a second scan of the CTE.)
- What changes if the director wants repos above the median total instead of the mean? (Tests a manual median via row numbering, since SQLite has no `PERCENTILE_CONT`.)
- How would you add each heavy hitter's percentage of all lines added? (Tests `SUM() OVER ()` to express each repo's share of the grand total.)