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.

An internal ledger and a bank statement should record the same transactions. Using the tables internal_ledger and bank_statement, find every transaction_id that differs between the two tables. 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 internal_ledger only: 'Missing from Bank' - In bank_statement only: 'Missing from Ledger' - In both tables with a different amount: 'Amount Mismatch' - Matching transactions are excluded, so the result may be empty. Tables: - internal_ledger: transaction_id INT (primary key), amount DECIMAL(12,2) NOT NULL (amount recorded in the ledger), transaction_date DATE NOT NULL, description VARCHAR(255) nullable (free text). - bank_statement: transaction_id INT (primary key), amount DECIMAL(12,2) NOT NULL (amount reported by the bank), transaction_date DATE NOT NULL, description VARCHAR(255) nullable (free text). Output contract: - Columns, in this order: transaction_id, ledger_amount, bank_amount, discrepancy_type. - ledger_amount is the internal_ledger amount, or NULL when the transaction is missing from the ledger. bank_amount is the bank_statement 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. 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.

Tables

internal_ledger(transaction_id INTEGER, amount DECIMAL(12,2), transaction_date DATE, description VARCHAR(255))

bank_statement(transaction_id INTEGER, amount DECIMAL(12,2), transaction_date DATE, description VARCHAR(255))

Hints

  1. 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.
  2. Only amount decides whether a transaction matches; transaction_date and description play no part in the comparison.
SquareData ScientistmediumData Manipulation (SQL/Python)

Reconcile an Internal Ledger Against a Bank Statement

An internal ledger and a bank statement should record the same transactions. Using the tables internal_ledger and bank_statement, find every transaction_id that differs between the two tables. 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 internal_ledger only: 'Missing from Bank'
  • In bank_statement only: 'Missing from Ledger'
  • In both tables with a different amount: 'Amount Mismatch'
  • Matching transactions are excluded, so the result may be empty.

Tables:

  • internal_ledger: transaction_id INT (primary key), amount DECIMAL(12,2) NOT NULL (amount recorded in the ledger), transaction_date DATE NOT NULL, description VARCHAR(255) nullable (free text).
  • bank_statement: transaction_id INT (primary key), amount DECIMAL(12,2) NOT NULL (amount reported by the bank), transaction_date DATE NOT NULL, description VARCHAR(255) nullable (free text).

Output contract:

  • Columns, in this order: transaction_id, ledger_amount, bank_amount, discrepancy_type.
  • ledger_amount is the internal_ledger amount, or NULL when the transaction is missing from the ledger. bank_amount is the bank_statement 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.

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.

Expected Output

Tables

internal_ledgerbank_statement
internal_ledger
ColumnType
transaction_idINTEGER
amountDECIMAL(12,2)
transaction_dateDATE
descriptionVARCHAR(255)
bank_statement
ColumnType
transaction_idINTEGER
amountDECIMAL(12,2)
transaction_dateDATE
descriptionVARCHAR(255)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions