Two Names on the Ledger

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
Amazon

Problem

Finance is reconciling revenue for two customers, alice and aaron42, and wants to see how their spend stacks up across both accounts, one transaction at a time. From the earliest transaction ID to the latest, return each transaction's ID, the customer, its amount, and the combined revenue accumulated through it.

Two accounts. One ledger. Watch the spend stack up.

Tables

transactions

Columns: transaction_id INTEGER, user_id INTEGER, product_id INTEGER, quantity INTEGER, total_amount REAL, transaction_date TEXT

transactions: 10 of 200 rows
transaction_iduser_idproduct_idquantitytotal_amounttransaction_date
10671971001223.462026-02-02
11342941050336.932026-03-03
12013911099450.42026-04-04
12684881148563.872026-05-05
13355851197002026-06-06
14026821246290.812026-07-07
146977912953104.282026-08-08
153687613444117.752026-09-09
160397313935-452026-01-10
1670107014421144.692026-02-11

users

Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT

users: 10 of 200 rows
user_idusernameemailsignup_dateaccount_statusage_bucket
100alicealice@example.com2025-02-02inactive25-34
197aaron42aaron42@example.com2026-03-03suspended35-44
294 ameliaamelia@example.com2024-04-04pending_verification45-54
391arjun arjun@example.com2025-05-05active55-64
488ava99ava99@example.com2026-06-06inactive65+
585andrew_kandrew_k@example.com2024-07-07suspendedNULL
682anikaanika@example.com2025-08-08 16:34:00pending_verification18-24
779aidenaiden@example.com2026-09-09 14:53:00active25-34
876aria_baria_b@example.com2024-10-10inactive35-44
973brianbrian@example.com2025-11-11suspended45-54

Expected output

First 5 of 26 rows
transaction_idusernametotal_amountrunning_total
1067aaron4223.4623.46
2005alice212.04235.5
2072aaron42225.51461.01
3010alice414.09875.0999999999999
3077aaron42427.561302.6599999999999

Worked solution and explanation

What this is really testing

Strip off the reconciliation story and this is a cumulative sum scoped to a filtered set of rows. The skill being probed: can you keep every transaction row visible while attaching a tally that grows down the ordered list? Anyone can sum revenue for two users. The trap is collapsing the detail: candidates reach for a grouped SUM, get one number per user, and lose the per-transaction rows the prompt explicitly asks for. The other failure is computing the tally over everybody and only then filtering to alice and aaron42, which leaves the accumulation contaminated by transactions that should never have been in scope.

Building it

Step 1: Join transactions to users

transactions stores only user_id, so you cannot filter on a name directly. An inner join to users on user_id brings username into reach and, being inner, quietly drops any named user who has no transactions.

Step 2: Scope to the two customers

WHERE u.username IN ('alice', 'aaron42') narrows the set first. Doing it here, not after the window, is the whole game: it guarantees the tally sums only these two users' spend.

Step 3: Accumulate without collapsing

SUM(t.total_amount) OVER (ORDER BY t.transaction_id) computes a per-row cumulative figure: each row carries the sum of its own amount plus every earlier transaction_id. Because it is a window aggregate, the original transaction rows survive untouched.

Cumulative revenue for two named users
SELECT t.transaction_id,
       u.username,
       t.total_amount,
       SUM(t.total_amount) OVER (ORDER BY t.transaction_id) AS running_total
FROM transactions t
INNER JOIN users u ON t.user_id = u.user_id
WHERE u.username IN ('alice', 'aaron42')
ORDER BY t.transaction_id

Filter to the two users first, then accumulate over transaction_id.

Grouped sum (wrong shape)
GROUP BY u.username with SUM(total_amount) returns one row per user. The per-transaction detail and the row-by-row tally the prompt asks for are gone.
Window sum (correct)
SUM(...) OVER (ORDER BY transaction_id) keeps every transaction row and attaches the cumulative figure to each, which is exactly the requested output.

Common follow-up questions