Net Lines

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3

Problem

Each row of repo_commits is one commit: the repository it landed in, the lines it added and the lines it removed, across several years of history. What is each repository's net line growth over that whole history, everything added minus everything removed?

Some authors build. Others trim. The net tells the truth.

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_namenet_lines
backend-api4060
data-platform4400
frontend3720
infra4800
ml-pipeline5140

Worked solution and explanation

What this really is

Without the dashboard framing, this is a grain question. The log has one row per commit and the answer has one row per repository, so the whole job is collapsing to that grain before you net anything. Everyone knows the net is added minus removed. What separates candidates is where that subtraction lives. Do it on each row and stop, and you return 200 commit-level differences. Let author or commit_at into the GROUP BY and each repository breaks into fragments. Either way, no row tells you how much a repository grew.

Step 1: Group on repo_name only

The question names one entity, the repository, so repo_name is the only grouping key. Nothing in it is per author or per period, and the commits span several years with no cutoff, so every row of repo_commits belongs in its repository's total.

Step 2: Total each side, then subtract

SUM(added) is everything the repository gained and SUM(removed) is everything it lost; their difference is the net. Name it net_lines so the result reads as an answer, not as an expression.

Net lines per repository
SELECT
  repo_name,
  SUM(added) - SUM(removed) AS net_lines
FROM repo_commits
GROUP BY repo_name
Net per commit, then total
SUM(added - removed) nets every row first. If either column is ever NULL on a commit, that difference is NULL, and SUM drops the whole commit.
Total each side, then net
SUM(added) - SUM(removed) lets each total skip only its own missing values. Both columns are always filled here, so the two forms agree, but this one stays correct if that changes.
The answer that forgot to collapse
SELECT
  repo_name,
  added - removed AS net_lines
FROM repo_commits

One row per commit: 200 differences and no repository total.

Common follow-up questions