Reconcile stored account balances against transaction history in SQL

Quick 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.

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.

Coinbase logo
Coinbase
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

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.

Input Table(s)

accounts

ColumnTypeMeaning
account_idINTAccount identifier
user_nameVARCHAR(100) NOT NULLAccount holder's name
current_balanceDECIMAL(12,2) NOT NULLBalance currently stored on the account

Primary key: account_id.

transactions

ColumnTypeMeaning
transaction_idINTTransaction identifier
account_idINTAccount the transaction belongs to
transaction_typeVARCHAR(10) NOT NULL'credit' or 'debit'
amountDECIMAL(12,2) NOT NULLPositive amount of the transaction
transaction_dateDATE NOT NULLDate 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_iduser_namecurrent_balance
1alice150.00
2bob80.00
3carol0.00
4dave25.00
5erin40.00
6frank0.00

transactions

transaction_idaccount_idtransaction_typeamounttransaction_date
11credit200.002024-01-03
21debit50.002024-01-05
32credit100.002024-01-04
42debit30.002024-01-06
53credit10.002024-01-07
65credit55.002024-01-08

Expected result:

account_iduser_namecurrent_balancecomputed_balancediscrepancy
4dave25.000.0025.00
5erin40.0055.00-15.00
2bob80.0070.0010.00
3carol0.0010.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.