Reconcile stored account balances against transaction history in SQL
Company: Coinbase
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Each account's stored balance is kept in `accounts.current_balance`, and every movement of money on the account is recorded in `transactions`. A reconciliation job checks that the two agree.
Compute each account's balance from its transactions (`credit` adds, `debit` subtracts). For accounts where `current_balance` differs from it, return `account_id`, `user_name`, `current_balance`, `computed_balance`, and `discrepancy` (`current_balance - computed_balance`). Order by absolute `discrepancy` descending, then `account_id` ascending.
```hint Edge case
Work out the computed balance of an account that has no transactions, and whether that account can appear in the output.
```
### Input Table(s)
`accounts`
| Column | Type | Meaning |
|---|---|---|
| account_id | INT | Account identifier |
| user_name | VARCHAR(100) NOT NULL | Account holder's name |
| current_balance | DECIMAL(12,2) NOT NULL | Balance currently stored on the account |
Primary key: `account_id`.
`transactions`
| Column | Type | Meaning |
|---|---|---|
| transaction_id | INT | Transaction identifier |
| account_id | INT | Account the transaction belongs to |
| transaction_type | VARCHAR(10) NOT NULL | `'credit'` or `'debit'` |
| amount | DECIMAL(12,2) NOT NULL | Positive amount of the transaction |
| transaction_date | DATE NOT NULL | Date of the transaction |
Primary key: `transaction_id`. Foreign key: `account_id` references `accounts.account_id`.
### Output Contract
- Columns, in this order: `account_id`, `user_name`, `current_balance`, `computed_balance`, `discrepancy`.
- `computed_balance` is the sum of the account's `credit` amounts minus the sum of its `debit` amounts over all of its transactions. It is `0.00` for an account with no transactions.
- `discrepancy` is `current_balance - computed_balance`, so it is positive when the stored balance is higher.
- Return only the accounts where `current_balance` and `computed_balance` differ.
- Sort by the absolute value of `discrepancy` descending, then `account_id` ascending. `account_id` is unique, so the order is fully determined.
- All three amounts are exact decimals with two places; no rounding is needed.
### Example
`accounts`
| account_id | user_name | current_balance |
|---|---|---|
| 1 | alice | 150.00 |
| 2 | bob | 80.00 |
| 3 | carol | 0.00 |
| 4 | dave | 25.00 |
| 5 | erin | 40.00 |
| 6 | frank | 0.00 |
`transactions`
| transaction_id | account_id | transaction_type | amount | transaction_date |
|---|---|---|---|---|
| 1 | 1 | credit | 200.00 | 2024-01-03 |
| 2 | 1 | debit | 50.00 | 2024-01-05 |
| 3 | 2 | credit | 100.00 | 2024-01-04 |
| 4 | 2 | debit | 30.00 | 2024-01-06 |
| 5 | 3 | credit | 10.00 | 2024-01-07 |
| 6 | 5 | credit | 55.00 | 2024-01-08 |
Expected result:
| account_id | user_name | current_balance | computed_balance | discrepancy |
|---|---|---|---|---|
| 4 | dave | 25.00 | 0.00 | 25.00 |
| 5 | erin | 40.00 | 55.00 | -15.00 |
| 2 | bob | 80.00 | 70.00 | 10.00 |
| 3 | carol | 0.00 | 10.00 | -10.00 |
alice's transactions give 200.00 minus 50.00, which equals her stored 150.00, so she is left out. frank has no transactions and a stored balance of 0.00, so he matches too. dave has no transactions but a stored balance of 25.00, so he appears with the largest gap. bob and carol tie at an absolute discrepancy of 10.00, and bob comes first because his `account_id` is smaller.
### Constraints and Clarifications
- `accounts` has at most 10,000 rows and `transactions` at most 100,000 rows.
- `transaction_type` is `'credit'` or `'debit'`, in lowercase.
- `amount` is always positive.
- Each `account_id` in `transactions` references a valid `account_id` in `accounts`, and no column in either table is NULL.
- An account with no transactions has a computed balance of `0.00` and appears in the output only when its `current_balance` is not zero.
- Every transaction counts, whatever its `transaction_date`.
- Compare the balances exactly, with no tolerance: a difference of 0.01 is a discrepancy.
- Each `transactions` row is a separate movement, so two rows with the same account, type and amount both count.
Overview: Write a SQL query that rebuilds each account's balance from its credit and debit transactions and reports the accounts whose stored balance does not match, with the size and sign of the gap. It tests conditional aggregation, keeping accounts that have no transactions, filtering on an aggregate, and sorting by absolute value with a tiebreaker.