AnalystPath

Running Wallet Balance

SQLMediumMid level~15 min

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_events
wallet_idevent_datedirectioncents
12024-03-01TopUp5000
12024-03-03Payment1200
12024-03-07Payment800

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

Related SQL questions