Running Wallet Balance
Problem
A mobile wallet logs every money movement for each user. Each row in `wallet_events` records a `wallet_id`, the `event_date`, a `direction` that is either 'TopUp' (money added) or 'Payment' (money spent), and the `cents` involved (always a positive integer).
For each event, report the wallet's running balance up to and including that event. The balance starts at 0; a 'TopUp' adds `cents` and a 'Payment' subtracts `cents`. Process each wallet's events in date order.
Return `wallet_id`, `event_date`, and the resulting `running_cents`, ordered by `wallet_id` then `event_date`.
Tables
Example rows — the live problem includes the full dataset.
| wallet_id | event_date | direction | cents |
|---|---|---|---|
| 1 | 2024-03-01 | TopUp | 5000 |
| 1 | 2024-03-03 | Payment | 1200 |
| 1 | 2024-03-07 | Payment | 800 |
Expected output
Your answer should return 5 rows with the columns wallet_id, event_date, running_cents.
Starter code (SQL)
SELECT *
FROM wallet_events;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