Repository Commit Ranking

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
Airbnb

Problem

repo_commits holds one row per commit, with added recording the lines that commit introduced to its repository. Build the repository standings by total lines added, highest first, where repositories with equal totals share a position and the next total takes the following number.

Lines added tell the story of a repo's ambition.

Tables

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_nametotal_addedrank
mobile-app90561
ml-pipeline84782
infra79003
backend-api73224
data-platform72005

Worked solution and explanation

This is a leaderboard over an aggregate, dressed as a commit log. The table holds 200 commit rows and the answer holds six repository rows, so the first decision is grain: the position is computed over SUM(added) after collapsing to repo_name, never over commits. Anyone can write the sum. What separates candidates is the numbering they pick for the position and whether they can say what it does when two totals tie. DENSE_RANK() lets equal totals share a spot and gives the next total the very next number. Pick by habit and the first week two repos tie, the published board reads 1, 1, 3.

Getting there

Step 1: Collapse commits to one row per repository

GROUP BY repo_name with SUM(added) turns 200 commits into six totals. Every later decision runs on those six rows, so get the grain right before you think about positions.

Step 2: Order the window by the aggregate itself

Window functions are evaluated after grouping, so DENSE_RANK() OVER (ORDER BY SUM(added) DESC) sees the six grouped rows, and the SUM(added) inside OVER is each group's total. There is no need for a CTE to name the total first.

Step 3: Sort by the position

ORDER BY rank lists the standings top to bottom. It is the same order as total descending, and it reads the way the board is published.

Repository standings by lines added
SELECT repo_name, SUM(added) AS total_added, DENSE_RANK() OVER (ORDER BY SUM(added) DESC) AS rank
FROM repo_commits
GROUP BY repo_name
ORDER BY rank
Skipping numbering
With RANK(), two repos tied at 7,900 both get 3 and the next repo gets 5. The board has no fourth place.
Gap-free numbering
With DENSE_RANK(), the same two repos both get 3 and the next repo gets 4. Equal totals share a spot and no number is skipped.
Wrong grain: numbering commits
SELECT repo_name, added, DENSE_RANK() OVER (ORDER BY added DESC) AS rank
FROM repo_commits

Without collapsing to repo_name first, each of the 200 commits gets its own position.

Common follow-up questions