All problems
Northline's Busiest Customer
hardSQLJoinsSubqueries
Support wants to know who to interview first for "power user" feedback — the customer with the most transactions, ties broken by higher net balance change.
transactions
| 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 |
Task: Write a query that returns a single row with one column, name: the customer with the most transactions on file. Every transaction counts, whatever its type and whichever direction the money went. Two customers on the same number of transactions are separated by their net amount, higher net winning. Nobody here is level on both, so the two rules together name exactly one customer.
Example output — shape only, on an invented customer.
| name |
|---|
| Nadia Okonkwo |
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