Running Total of Donations per Donor
Problem
A charity records each donation a supporter makes on a given date.
Table: `gifts`
| Column | Type |
|-------------|------|
| donor_id | int |
| campaign_id | int |
| gift_date | date |
| amount | int |
`(donor_id, gift_date)` is the primary key.
Write a query that reports, for each donor and each of their gift dates, the **cumulative total** the donor has given up to and including that date. Return columns `donor_id`, `gift_date`, and `total_so_far`, in any order.
Tables
Example rows — the live problem includes the full dataset.
| donor_id | campaign_id | gift_date | amount |
|---|---|---|---|
| 1 | 2 | 2019-03-01 | 5 |
| 1 | 2 | 2019-05-02 | 6 |
| 1 | 3 | 2020-06-25 | 1 |
Expected output
Your answer should return 5 rows with the columns donor_id, gift_date, total_so_far.
Starter code (SQL)
SELECT *
FROM gifts;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock