Find missing and mismatched transactions between a ledger and a bank statement

Quick Overview

Write a SQL query that reconciles an internal ledger against a bank statement, flagging transactions missing from either side and transactions whose amounts disagree. It tests joins that keep unmatched rows from both tables, NULL handling in filters, and labeling rows with conditional logic.

Find missing and mismatched transactions between a ledger and a bank statement

Company: Square

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

An internal ledger and a bank statement should record the same transactions. Find every `transaction_id` that differs between `internal_ledger` and `bank_statement`. Return `transaction_id`, `ledger_amount`, `bank_amount` (`NULL` if missing from that table), and `discrepancy_type`, ordered by `transaction_id` ascending. Label each discrepancy as follows: - In the ledger only: `'Missing from Bank'` - In the bank statement only: `'Missing from Ledger'` - In both with a different `amount`: `'Amount Mismatch'` - Matching transactions are excluded, so the result may be empty. ```hint Keep both sides Two of the three labels describe rows that have no partner in the other table. Check that your join keeps unmatched rows from both tables, and that your filter does not throw them away again. ``` ### Input Table(s) `internal_ledger` (primary key `transaction_id`) | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Transaction identifier, primary key | | amount | DECIMAL(12,2) | Amount recorded in the ledger, NOT NULL | | transaction_date | DATE | Date of the transaction, NOT NULL | | description | VARCHAR(255) | Free-text description, nullable | `bank_statement` (primary key `transaction_id`) | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Transaction identifier, primary key | | amount | DECIMAL(12,2) | Amount reported by the bank, NOT NULL | | transaction_date | DATE | Date of the transaction, NOT NULL | | description | VARCHAR(255) | Free-text description, nullable | ### Output Contract - Columns, in this order: `transaction_id`, `ledger_amount`, `bank_amount`, `discrepancy_type`. - `ledger_amount` is the ledger's `amount`, or `NULL` when the transaction is missing from the ledger. `bank_amount` is the bank statement's `amount`, or `NULL` when the transaction is missing from the bank statement. - `discrepancy_type` is exactly one of `'Missing from Bank'`, `'Missing from Ledger'`, `'Amount Mismatch'`. - One row per discrepant `transaction_id`, ordered by `transaction_id` ascending. Each id appears at most once, so the order is unique. - Amounts are returned as stored, with no rounding. ### Example `internal_ledger` | transaction_id | amount | transaction_date | description | |---|---|---|---| | 1 | 100.00 | 2024-01-05 | Coffee beans | | 2 | 250.50 | 2024-01-06 | Office rent | | 3 | 75.25 | 2024-01-07 | Printer paper | | 5 | 40.00 | 2024-01-09 | Card refund | `bank_statement` | transaction_id | amount | transaction_date | description | |---|---|---|---| | 1 | 100.00 | 2024-01-05 | COFFEE BEANS | | 2 | 250.00 | 2024-01-06 | Office rent | | 4 | 12.99 | 2024-01-08 | Monthly fee | | 5 | 40.00 | 2024-01-10 | Card refund | Expected result: | transaction_id | ledger_amount | bank_amount | discrepancy_type | |---|---|---|---| | 2 | 250.50 | 250.00 | Amount Mismatch | | 3 | 75.25 | NULL | Missing from Bank | | 4 | NULL | 12.99 | Missing from Ledger | Transaction 2 is in both tables with different amounts. Transaction 3 exists only in the ledger and transaction 4 only in the bank statement. Transactions 1 and 5 are excluded: their amounts agree, and the different description and date do not count. ### Constraints and Clarifications - Each table has between 1 and 10,000 rows. - `transaction_id` is unique within each table. - `amount` is a non-negative decimal with up to 2 decimal places and is never NULL. - Only `amount` is compared. A transaction present in both tables with equal amounts is a match even when its `transaction_date` or `description` differs. - `description` may be NULL; it plays no part in the comparison. - If the two tables agree completely, return an empty result with the four columns.

Overview: Write a SQL query that reconciles an internal ledger against a bank statement, flagging transactions missing from either side and transactions whose amounts disagree. It tests joins that keep unmatched rows from both tables, NULL handling in filters, and labeling rows with conditional logic.

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

An internal ledger and a bank statement should record the same transactions. Find every transaction_id that differs between internal_ledger and bank_statement. Return transaction_id, ledger_amount, bank_amount (NULL if missing from that table), and discrepancy_type, ordered by transaction_id ascending.

Label each discrepancy as follows:

  • In the ledger only: 'Missing from Bank'
  • In the bank statement only: 'Missing from Ledger'
  • In both with a different amount : 'Amount Mismatch'
  • Matching transactions are excluded, so the result may be empty.

Input Table(s)

internal_ledger (primary key transaction_id)

ColumnTypeMeaning
transaction_idINTTransaction identifier, primary key
amountDECIMAL(12,2)Amount recorded in the ledger, NOT NULL
transaction_dateDATEDate of the transaction, NOT NULL
descriptionVARCHAR(255)Free-text description, nullable

bank_statement (primary key transaction_id)

ColumnTypeMeaning
transaction_idINTTransaction identifier, primary key
amountDECIMAL(12,2)Amount reported by the bank, NOT NULL
transaction_dateDATEDate of the transaction, NOT NULL
descriptionVARCHAR(255)Free-text description, nullable

Output Contract

  • Columns, in this order: transaction_id , ledger_amount , bank_amount , discrepancy_type .
  • ledger_amount is the ledger's amount , or NULL when the transaction is missing from the ledger. bank_amount is the bank statement's amount , or NULL when the transaction is missing from the bank statement.
  • discrepancy_type is exactly one of 'Missing from Bank' , 'Missing from Ledger' , 'Amount Mismatch' .
  • One row per discrepant transaction_id , ordered by transaction_id ascending. Each id appears at most once, so the order is unique.
  • Amounts are returned as stored, with no rounding.

Example

internal_ledger

transaction_idamounttransaction_datedescription
1100.002024-01-05Coffee beans
2250.502024-01-06Office rent
375.252024-01-07Printer paper
540.002024-01-09Card refund

bank_statement

transaction_idamounttransaction_datedescription
1100.002024-01-05COFFEE BEANS
2250.002024-01-06Office rent
412.992024-01-08Monthly fee
540.002024-01-10Card refund

Expected result:

transaction_idledger_amountbank_amountdiscrepancy_type
2250.50250.00Amount Mismatch
375.25NULLMissing from Bank
4NULL12.99Missing from Ledger

Transaction 2 is in both tables with different amounts. Transaction 3 exists only in the ledger and transaction 4 only in the bank statement. Transactions 1 and 5 are excluded: their amounts agree, and the different description and date do not count.

Constraints and Clarifications

  • Each table has between 1 and 10,000 rows.
  • transaction_id is unique within each table.
  • amount is a non-negative decimal with up to 2 decimal places and is never NULL.
  • Only amount is compared. A transaction present in both tables with equal amounts is a match even when its transaction_date or description differs.
  • description may be NULL; it plays no part in the comparison.
  • If the two tables agree completely, return an empty result with the four columns.