Who's Pulling the Most Cash Out?
Northline Bank's fraud team reviews one customer a day by hand, and the queue is ordered by how much cash a customer has pulled out in total. Not the biggest single withdrawal — a customer taking money out repeatedly is the pattern they care about — so the figure is a customer's withdrawals added together.
Two details shape the answer. Money out is stored as a negative number, so adding a customer's withdrawals gives a negative total, and the review sheet wants a positive figure. And type matters: only rows marked withdrawal count here. The one transfer in the table is also money leaving an account, and it is deliberately not part of this measure — a decision worth noticing rather than assuming.
transactions — one row per money movement. amount is signed: positive for money in, negative for money out. type is one of deposit, withdrawal or transfer.
| id | customer_id | amount | type | txn_date |
|---|---|---|---|---|
| 1 | 1 | 500 | deposit | 2023-07-01 |
| 2 | 1 | -120 | withdrawal | 2023-07-03 |
| 3 | 2 | 1000 | deposit | 2023-07-02 |
| 4 | 2 | -300 | withdrawal | 2023-07-05 |
| 5 | 3 | 250 | deposit | 2023-07-04 |
| 6 | 3 | -600 | withdrawal | 2023-07-06 |
| 7 | 4 | 800 | deposit | 2023-07-07 |
| 8 | 4 | -450 | withdrawal | 2023-07-08 |
| 9 | 5 | 2000 | deposit | 2023-07-09 |
| 10 | 5 | -1500 | withdrawal | 2023-07-10 |
| 11 | 1 | -200 | transfer | 2023-07-11 |
| 12 | 3 | 400 | deposit | 2023-07-12 |
The table already exists in the database — there is nothing to create or load.
Task: Write a query returning exactly one row with two columns, customer_id and total_withdrawn — the customer with the largest total across their withdrawal rows, and that total written as a positive number. Deposits and transfers are excluded before anything is added up. If two customers were tied at the top only one row would still come back, and which of them it is would be unspecified.
Example output — shape only, on an invented customer and total.
| customer_id | total_withdrawn |
|---|---|
| 9 | 2740 |
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in