Pessimistic locking transfer check: project sender and receiver balances in SQL
Company: Revolut
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A transfer service uses pessimistic locking: before moving money, a transaction locks both account rows with `SELECT ... FOR UPDATE`, checks that the sender can cover the amount, and only then updates the balances. Write a query that simulates the validation step without changing any data. For each pending transfer, return one row for the sender and one row for the receiver, showing the account, its current balance, its role in the transfer, the transfer amount, the balance it would have afterwards, and whether the transfer is approved.
```hint Where the decision comes from
The approval decision depends on one account's balance, but it has to appear on two rows. Make sure the receiver's row can see the value it needs.
```
### Input Table(s)
Table `accounts`
| Column | Type | Meaning |
|---|---|---|
| account_id | INT | Account identifier |
| account_holder | VARCHAR(100) | Name of the holder, NOT NULL |
| balance | DECIMAL(15,2) | Current balance, NOT NULL |
Primary key: `account_id`.
Table `pending_transfers`
| Column | Type | Meaning |
|---|---|---|
| transfer_id | INT | Transfer identifier |
| from_account_id | INT | Sending account, references `accounts.account_id` |
| to_account_id | INT | Receiving account, references `accounts.account_id` |
| amount | DECIMAL(15,2) | Amount to move, NOT NULL |
| requested_at | TIMESTAMP | When the transfer was requested (DATETIME in the original schema), NOT NULL |
Primary key: `transfer_id`.
### Output Contract
- Columns, in this order: `account_id`, `account_holder`, `balance`, `Role`, `transfer_id`, `TransferAmount`, `ProjectedBalance`, `TransferStatus`. The names are case-sensitive, so the mixed-case ones must keep their capital letters.
- Two rows per pending transfer: one for the sending account and one for the receiving account.
- `balance` is the account's current balance, and `TransferAmount` is the transfer's `amount`.
- `Role` is `'SENDER'` on the sending account's row and `'RECEIVER'` on the receiving account's row.
- `TransferStatus` is `'APPROVED'` when the sender's `balance` is greater than or equal to `amount`, otherwise `'INSUFFICIENT_FUNDS'`. Both rows of a transfer carry the same status.
- `ProjectedBalance` for an approved transfer is `balance - amount` on the sender row and `balance + amount` on the receiver row. For a rejected transfer it equals the current `balance` on both rows.
- Money values keep two decimal places; no extra rounding is needed.
- Order by `transfer_id` ascending, then `account_id` ascending. The two accounts of a transfer always differ, so this order is unique.
### Example
Input `accounts`:
| account_id | account_holder | balance |
|---|---|---|
| 1 | Alice | 500.00 |
| 2 | Bob | 100.00 |
| 3 | Chen | 0.00 |
Input `pending_transfers`:
| transfer_id | from_account_id | to_account_id | amount | requested_at |
|---|---|---|---|---|
| 101 | 1 | 2 | 200.00 | 2024-05-01 09:00:00 |
| 102 | 2 | 3 | 150.00 | 2024-05-01 09:05:00 |
| 103 | 2 | 1 | 100.00 | 2024-05-01 09:10:00 |
Expected result:
| account_id | account_holder | balance | Role | transfer_id | TransferAmount | ProjectedBalance | TransferStatus |
|---|---|---|---|---|---|---|---|
| 1 | Alice | 500.00 | SENDER | 101 | 200.00 | 300.00 | APPROVED |
| 2 | Bob | 100.00 | RECEIVER | 101 | 200.00 | 300.00 | APPROVED |
| 2 | Bob | 100.00 | SENDER | 102 | 150.00 | 100.00 | INSUFFICIENT_FUNDS |
| 3 | Chen | 0.00 | RECEIVER | 102 | 150.00 | 0.00 | INSUFFICIENT_FUNDS |
| 1 | Alice | 500.00 | RECEIVER | 103 | 100.00 | 600.00 | APPROVED |
| 2 | Bob | 100.00 | SENDER | 103 | 100.00 | 0.00 | APPROVED |
Transfer 101 is approved because Alice has 500.00, so she drops to 300.00 and Bob rises to 300.00. Transfer 102 is checked against Bob's current 100.00, which is less than 150.00, so both balances stay as they are. Transfer 103 is approved because 100.00 covers exactly 100.00; Alice's row comes first because her `account_id` is smaller.
### Constraints and Clarifications
- `accounts` has at most 100 rows and `pending_transfers` at most 50.
- `balance` and `amount` are non-negative and never NULL.
- `from_account_id` and `to_account_id` always reference existing accounts and are never equal.
- Each transfer is checked on its own against the current balances in `accounts`. Transfers are not applied one after another, so an approved transfer does not change the balance used to check any other transfer.
- An account can take part in several transfers and then appears in several output rows.
- `requested_at` is not used in the output or the ordering.
- The query is read-only: it simulates the check and takes no locks.
Overview: Write a SQL query that simulates the validation step of a pessimistic-locking money transfer, returning a sender row and a receiver row for each pending transfer with its projected balance and approval status. It tests self-joins, conditional logic with CASE, and reasoning about how SELECT FOR UPDATE transactions avoid race conditions and deadlocks.