Commit Royalty
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
- Asked in
- Spotify
Problem
Find the repository with the most commits in a monthly top 10 by lines added, along with how many it placed. We build a separate top 10 for each month of 2026 from repo_commits, taking the ten biggest commits per month across all repositories.
In a sea of commits, only a few wear the crown.
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 | top10_count |
|---|---|
| mobile-app | 17 |
Worked solution and explanation
What this really is
This is top-N-per-group dressed up as an engineering leaderboard. The question is not which repository has the biggest commit or the most commits. It is which repository keeps showing up among each month's ten biggest. Anyone can sort by added. The skill is making that order restart every calendar month and cutting at ten inside each month before you count. Every month of 2026 holds 13 or 14 commits, so the cut decides the answer. Skip it and you are just counting commits per repository ('mobile-app' at 21 instead of 17). Use one global ORDER BY added DESC LIMIT 10 and only ten commits qualify, leaving 'mobile-app' and 'ml-pipeline' tied at 3.
SELECT repo_name, COUNT(*) AS top10_count
FROM (
SELECT repo_name
FROM repo_commits
WHERE strftime('%Y', commit_at) = '2026'
ORDER BY added DESC
LIMIT 10
) AS year_top
GROUP BY repo_name
ORDER BY top10_count DESC
LIMIT 1Wrong: ten commits for the whole year, not ten per month.
Build it in three moves
Step 1: Keep only 2026 commits
Filter repo_commits with strftime('%Y', commit_at) = '2026' before anything else. Earlier years live in the same table, and each of their months would otherwise form its own top 10 and pad every count.
Step 2: Restart the order every month
Number commits with DENSE_RANK() over PARTITION BY strftime('%Y-%m', commit_at) ORDER BY added DESC. The partition key is the derived year-month, not the raw commit_at: partition on the timestamp and every commit sits alone in its partition and ranks 1. Rank on added alone, as asked, not on added - removed.
Step 3: Cut at ten, then count
Keep rnk <= 10 in the outer query, since a window result cannot be filtered in the WHERE of the SELECT that computes it. Then GROUP BY repo_name with COUNT(*). Every qualifying commit counts, so a repository holding four spots in one month gets four. The preview's 17 is more than nine months, which is how you know commits are counted, not months.
The solution
WITH monthly_ranked AS (
SELECT repo_name,
DENSE_RANK() OVER (PARTITION BY strftime('%Y-%m', commit_at) ORDER BY added DESC) AS rnk
FROM repo_commits
WHERE strftime('%Y', commit_at) = '2026'
)
SELECT repo_name, COUNT(*) AS top10_count
FROM monthly_ranked
WHERE rnk <= 10
GROUP BY repo_name
ORDER BY top10_count DESC
LIMIT 1Common follow-up questions
- Why partition the window by year-month rather than just by month? (Tests multi-year thinking: partitioning on the month alone would lump January 2025 with January 2026 if the year filter were ever widened.)
- How would you find repositories that place in the top 10 in at least 6 different months? (Tests post-aggregation filtering: carry the month through the CTE and add `HAVING COUNT(DISTINCT commit_month) >= 6` after the `GROUP BY`.)
- What does the answer become if `added` includes auto-generated lockfile changes? (Tests data quality awareness: filter those commits by path or `message` keywords before ranking, since one lockfile commit can add tens of thousands of lines.)