Audit wallet balances against completed send and receive transactions

Quick Overview

Write a PostgreSQL query that audits every wallet's stored balance against its completed send and receive transactions and flags overdrafts and mismatches. It tests conditional aggregation, keeping inactive wallets through a LEFT JOIN, NULL handling with COALESCE, and ordered CASE logic.

Audit wallet balances against completed send and receive transactions

Company: Coinbase

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A wallet service stores one row per wallet in `wallets` and one row per side of every transfer in `transactions`. A transfer writes two rows that share a `transfer_id`: a `'SEND'` row on the paying wallet and a `'RECEIVE'` row on the receiving wallet. Each wallet records the balance it started with (`initial_balance`) and the balance currently stored on it (`balance`). Produce a balance audit for every wallet with columns `wallet_id`, `user_id`, `currency`, `current_balance`, `total_sent`, `total_received`, `send_count`, `receive_count`, `balance_status`, ordered by `wallet_id` ascending. Requirements: - Totals and counts consider only `'COMPLETED'` transactions (0 if none). - `balance_status` is `'OVERDRAFT'` if `balance < 0`. - Else it is `'INCONSISTENT'` if `balance != initial_balance - total_sent + total_received`. - Otherwise it is `'OK'`. ```hint Where the filter goes Decide where the `'COMPLETED'` filter belongs so that a wallet with no completed activity still gets its row. ``` ### Input Table(s) `wallets` (primary key `wallet_id`) | column | type | meaning | |---|---|---| | wallet_id | INTEGER | Wallet identifier | | user_id | INTEGER | User who owns the wallet | | currency | CHAR(3) | ISO currency code of the wallet | | initial_balance | NUMERIC(18,2) | Balance when the wallet was opened | | balance | NUMERIC(18,2) | Balance currently stored on the wallet | `transactions` (primary key `transaction_id`; `wallet_id` references `wallets.wallet_id`) | column | type | meaning | |---|---|---| | transaction_id | INTEGER | Row identifier | | transfer_id | INTEGER | Shared by the two rows of one transfer | | wallet_id | INTEGER | Wallet this side of the transfer belongs to | | direction | VARCHAR | `'SEND'` or `'RECEIVE'` | | amount | NUMERIC(18,2) | Amount moved, always positive | | status | VARCHAR | `'PENDING'`, `'COMPLETED'` or `'FAILED'` | ### Output Contract One row per wallet in `wallets`, including wallets with no transactions at all, with these columns in this order: | column | definition | |---|---| | wallet_id | From `wallets` | | user_id | From `wallets` | | currency | From `wallets` | | current_balance | `wallets.balance` | | total_sent | Sum of `amount` over the wallet's completed `'SEND'` rows, 0 if none | | total_received | Sum of `amount` over the wallet's completed `'RECEIVE'` rows, 0 if none | | send_count | Number of the wallet's completed `'SEND'` rows, 0 if none | | receive_count | Number of the wallet's completed `'RECEIVE'` rows, 0 if none | | balance_status | `'OVERDRAFT'`, `'INCONSISTENT'` or `'OK'`, as defined above | Rows are ordered by `wallet_id` ascending, which is unique. No rounding is applied: amounts keep their two decimal places and compare as numbers, and the counts are integers. ### Example `wallets` | wallet_id | user_id | currency | initial_balance | balance | |---|---|---|---|---| | 1 | 10 | USD | 100.00 | 75.00 | | 2 | 11 | USD | 50.00 | 85.00 | | 3 | 12 | USD | 20.00 | -5.00 | | 4 | 13 | USD | 40.00 | 65.00 | | 5 | 14 | EUR | 0.00 | 0.00 | `transactions` | transaction_id | transfer_id | wallet_id | direction | amount | status | |---|---|---|---|---|---| | 1 | 100 | 1 | SEND | 30.00 | COMPLETED | | 2 | 100 | 2 | RECEIVE | 30.00 | COMPLETED | | 3 | 101 | 1 | SEND | 15.00 | PENDING | | 4 | 101 | 3 | RECEIVE | 15.00 | PENDING | | 5 | 102 | 3 | SEND | 25.00 | COMPLETED | | 6 | 102 | 4 | RECEIVE | 25.00 | COMPLETED | | 7 | 103 | 4 | SEND | 10.00 | FAILED | | 8 | 103 | 1 | RECEIVE | 10.00 | FAILED | | 9 | 104 | 2 | SEND | 5.00 | COMPLETED | | 10 | 104 | 1 | RECEIVE | 5.00 | COMPLETED | Expected result: | wallet_id | user_id | currency | current_balance | total_sent | total_received | send_count | receive_count | balance_status | |---|---|---|---|---|---|---|---|---| | 1 | 10 | USD | 75.00 | 30.00 | 5.00 | 1 | 1 | OK | | 2 | 11 | USD | 85.00 | 5.00 | 30.00 | 1 | 1 | INCONSISTENT | | 3 | 12 | USD | -5.00 | 25.00 | 0 | 1 | 0 | OVERDRAFT | | 4 | 13 | USD | 65.00 | 0 | 25.00 | 0 | 1 | OK | | 5 | 14 | EUR | 0.00 | 0 | 0 | 0 | 0 | OK | Wallet 1 has one completed send of 30.00 and one completed receive of 5.00. Its pending send and failed receive are ignored, and `100.00 - 30.00 + 5.00 = 75.00` matches the stored balance. Wallet 2 should hold `50.00 - 5.00 + 30.00 = 75.00` but stores 85.00. Wallet 3 reconciles (`20.00 - 25.00 = -5.00`), yet its balance is negative and the overdraft check comes first. Wallet 4's failed send is ignored, and wallet 5 has no transactions, so its totals and counts are 0. ### Constraints and Clarifications - `wallets` has at most 10,000 rows and `transactions` at most 100,000. - `direction` is always `'SEND'` or `'RECEIVE'`, and `status` is always `'PENDING'`, `'COMPLETED'` or `'FAILED'`. - `amount` is positive and `currency` is a 3-letter ISO code. No column in either table is NULL. - Each `transfer_id` appears exactly twice, once as `'SEND'` and once as `'RECEIVE'`. Every row counts toward its own `wallet_id` and is judged by its own `status`. - `'PENDING'` and `'FAILED'` rows contribute to no total and no count. - A wallet with a negative balance is `'OVERDRAFT'` even when its balance reconciles.

Overview: Write a PostgreSQL query that audits every wallet's stored balance against its completed send and receive transactions and flags overdrafts and mismatches. It tests conditional aggregation, keeping inactive wallets through a LEFT JOIN, NULL handling with COALESCE, and ordered CASE logic.

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

A wallet service stores one row per wallet in wallets and one row per side of every transfer in transactions. A transfer writes two rows that share a transfer_id: a 'SEND' row on the paying wallet and a 'RECEIVE' row on the receiving wallet. Each wallet records the balance it started with (initial_balance) and the balance currently stored on it (balance).

Produce a balance audit for every wallet with columns wallet_id, user_id, currency, current_balance, total_sent, total_received, send_count, receive_count, balance_status, ordered by wallet_id ascending.

Requirements:

  • Totals and counts consider only 'COMPLETED' transactions (0 if none).
  • balance_status is 'OVERDRAFT' if balance < 0 .
  • Else it is 'INCONSISTENT' if balance != initial_balance - total_sent + total_received .
  • Otherwise it is 'OK' .

Input Table(s)

wallets (primary key wallet_id)

columntypemeaning
wallet_idINTEGERWallet identifier
user_idINTEGERUser who owns the wallet
currencyCHAR(3)ISO currency code of the wallet
initial_balanceNUMERIC(18,2)Balance when the wallet was opened
balanceNUMERIC(18,2)Balance currently stored on the wallet

transactions (primary key transaction_id; wallet_id references wallets.wallet_id)

columntypemeaning
transaction_idINTEGERRow identifier
transfer_idINTEGERShared by the two rows of one transfer
wallet_idINTEGERWallet this side of the transfer belongs to
directionVARCHAR'SEND' or 'RECEIVE'
amountNUMERIC(18,2)Amount moved, always positive
statusVARCHAR'PENDING', 'COMPLETED' or 'FAILED'

Output Contract

One row per wallet in wallets, including wallets with no transactions at all, with these columns in this order:

columndefinition
wallet_idFrom wallets
user_idFrom wallets
currencyFrom wallets
current_balancewallets.balance
total_sentSum of amount over the wallet's completed 'SEND' rows, 0 if none
total_receivedSum of amount over the wallet's completed 'RECEIVE' rows, 0 if none
send_countNumber of the wallet's completed 'SEND' rows, 0 if none
receive_countNumber of the wallet's completed 'RECEIVE' rows, 0 if none
balance_status'OVERDRAFT', 'INCONSISTENT' or 'OK', as defined above

Rows are ordered by wallet_id ascending, which is unique. No rounding is applied: amounts keep their two decimal places and compare as numbers, and the counts are integers.

Example

wallets

wallet_iduser_idcurrencyinitial_balancebalance
110USD100.0075.00
211USD50.0085.00
312USD20.00-5.00
413USD40.0065.00
514EUR0.000.00

transactions

transaction_idtransfer_idwallet_iddirectionamountstatus
11001SEND30.00COMPLETED
21002RECEIVE30.00COMPLETED
31011SEND15.00PENDING
41013RECEIVE15.00PENDING
51023SEND25.00COMPLETED
61024RECEIVE25.00COMPLETED
71034SEND10.00FAILED
81031RECEIVE10.00FAILED
91042SEND5.00COMPLETED
101041RECEIVE5.00COMPLETED

Expected result:

wallet_iduser_idcurrencycurrent_balancetotal_senttotal_receivedsend_countreceive_countbalance_status
110USD75.0030.005.0011OK
211USD85.005.0030.0011INCONSISTENT
312USD-5.0025.00010OVERDRAFT
413USD65.00025.0001OK
514EUR0.000000OK

Wallet 1 has one completed send of 30.00 and one completed receive of 5.00. Its pending send and failed receive are ignored, and 100.00 - 30.00 + 5.00 = 75.00 matches the stored balance. Wallet 2 should hold 50.00 - 5.00 + 30.00 = 75.00 but stores 85.00. Wallet 3 reconciles (20.00 - 25.00 = -5.00), yet its balance is negative and the overdraft check comes first. Wallet 4's failed send is ignored, and wallet 5 has no transactions, so its totals and counts are 0.

Constraints and Clarifications

  • wallets has at most 10,000 rows and transactions at most 100,000.
  • direction is always 'SEND' or 'RECEIVE' , and status is always 'PENDING' , 'COMPLETED' or 'FAILED' .
  • amount is positive and currency is a 3-letter ISO code. No column in either table is NULL.
  • Each transfer_id appears exactly twice, once as 'SEND' and once as 'RECEIVE' . Every row counts toward its own wallet_id and is judged by its own status .
  • 'PENDING' and 'FAILED' rows contribute to no total and no count.
  • A wallet with a negative balance is 'OVERDRAFT' even when its balance reconciles.