Compute each account's final balance after applying all transfers
Company: Revolut
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Each `transfers` row moves `amount` from `from_account_id` to `to_account_id`. Compute each account's final balance: its initial `balance`, plus the total it received, minus the total it sent. Return every account, including accounts that never sent or received a transfer.
```hint Two roles
Every transfer touches two accounts. Check what your query does for an account that sends several transfers and also receives one.
```
### Input Table(s)
Table `balances`, primary key `account_id`:
| Column | Type | Meaning |
|---|---|---|
| account_id | INT | Unique id of the account (primary key) |
| account_name | VARCHAR(100) | Account holder's name, never NULL |
| balance | DECIMAL(15,2) | Initial balance before any transfer, never NULL |
Table `transfers`, primary key `transfer_id`; `from_account_id` and `to_account_id` both reference `balances.account_id`:
| Column | Type | Meaning |
|---|---|---|
| transfer_id | INT | Unique id of the transfer (primary key) |
| from_account_id | INT | Account the money leaves |
| to_account_id | INT | Account the money arrives in |
| amount | DECIMAL(15,2) | Amount moved, always greater than 0 |
| transfer_date | TIMESTAMP | When the transfer happened, never NULL |
### Output Contract
- Columns, in this order: `account_id`, `account_name`, `final_balance`.
- Exactly one row per account in `balances`.
- `final_balance` is `balance + (sum of amount received) - (sum of amount sent)`, with two decimal places. All inputs have two decimals, so exact decimal arithmetic needs no rounding.
- Sort by `account_id` ascending.
### Example
Table `balances`:
| account_id | account_name | balance |
|---|---|---|
| 1 | Alice | 100.00 |
| 2 | Bob | 50.00 |
| 3 | Chen | 0.00 |
| 4 | Dana | 20.00 |
Table `transfers`:
| transfer_id | from_account_id | to_account_id | amount | transfer_date |
|---|---|---|---|---|
| 10 | 1 | 2 | 30.00 | 2024-03-01 09:00:00 |
| 11 | 1 | 3 | 20.00 | 2024-03-02 10:30:00 |
| 12 | 2 | 1 | 5.50 | 2024-03-03 14:00:00 |
| 13 | 2 | 3 | 10.00 | 2024-03-04 16:45:00 |
Expected result:
| account_id | account_name | final_balance |
|---|---|---|
| 1 | Alice | 55.50 |
| 2 | Bob | 64.50 |
| 3 | Chen | 30.00 |
| 4 | Dana | 20.00 |
Alice sends 30.00 and 20.00 and receives 5.50, so she ends at `100.00 + 5.50 - 50.00 = 55.50`. Bob receives 30.00 and sends 5.50 and 10.00, so he ends at `50.00 + 30.00 - 15.50 = 64.50`. Chen only receives (20.00 and 10.00), and Dana has no transfers, so she keeps 20.00. The final balances add up to 170.00, the same as the initial total, because transfers only move money between accounts.
### Constraints and Clarifications
- `balances` has at most 10,000 rows and `transfers` at most 100,000 rows.
- `amount` is always greater than 0.
- `from_account_id` and `to_account_id` are never NULL, always reference existing accounts, and are never equal in the same row.
- Every transfer counts, whatever its `transfer_date`.
- An account can appear in many transfers on either side. An account with no transfers keeps its initial balance.
- Apply every transfer as given. Nothing is rejected for insufficient funds, so a negative final balance is reported as computed.
Overview: Write a SQL query that starts from each account's opening balance, adds every incoming transfer, subtracts every outgoing one, and lists all accounts. It tests how you combine the sender and receiver roles of one table without double counting, keep accounts that have no transfers, and handle exact decimal amounts.