Interview Questions/SQL/Running Total Revenue per Customer

Running Total Revenue per Customer

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Running Total Revenue per Customer

You are given a table of customer purchases. Each purchase has a customer ID, an amount, and a date. Write a SQL query that returns every purchase row enriched with a running_total column: the cumulative sum of amount for that customer up to and including the current row, ordered by purchase_date. Return all columns (id, customer_id, amount, purchase_date, running_total), ordered by customer_id then purchase_date. Table: Purchases

Column NameTypeDescription
idINTPrimary key
customer_idINTID of the customer
amountINTPurchase amount
purchase_dateDATEDate of the purchase

Examples

Example 1

Use SUM() as a window function partitioned by customer_id and ordered by purchase_date with ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

query.sql