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.

A wallet service stores one row per wallet in the `wallets` table and one row per side of every transfer in the `transactions` table. 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`). Write a SQL query that produces a balance audit for every wallet with the columns `wallet_id`, `user_id`, `currency`, `current_balance`, `total_sent`, `total_received`, `send_count`, `receive_count`, `balance_status`, in that order, 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 tables: - `wallets` (primary key `wallet_id`): `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`): `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: one row per wallet in `wallets`, including wallets with no transactions at all, with these columns in this order: - `wallet_id`, `user_id`, `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. 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.

Tables

wallets(wallet_id INTEGER, user_id INTEGER, currency CHAR(3), initial_balance NUMERIC(18,2), balance NUMERIC(18,2))

transactions(transaction_id INTEGER, transfer_id INTEGER, wallet_id INTEGER, direction VARCHAR, amount NUMERIC(18,2), status VARCHAR)

Hints

  1. Decide where the 'COMPLETED' filter belongs so that a wallet with no completed activity still gets its row.
  2. A wallet with no completed rows reports 0 for every total and count, and that 0 is what the reconciliation check uses.
CoinbaseData ScientistmediumData Manipulation (SQL/Python)

Wallet Balance Audit

A wallet service stores one row per wallet in the wallets table and one row per side of every transfer in the transactions table. 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).

Write a SQL query that produces a balance audit for every wallet with the columns wallet_id, user_id, currency, current_balance, total_sent, total_received, send_count, receive_count, balance_status, in that order, 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 tables:

  • wallets (primary key wallet_id): 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): 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: one row per wallet in wallets, including wallets with no transactions at all, with these columns in this order:

  • wallet_id, user_id, 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.

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.

Expected Output

Tables

walletstransactions
wallets
ColumnType
wallet_idINTEGER
user_idINTEGER
currencyCHAR(3)
initial_balanceNUMERIC(18,2)
balanceNUMERIC(18,2)
transactions
ColumnType
transaction_idINTEGER
transfer_idINTEGER
wallet_idINTEGER
directionVARCHAR
amountNUMERIC(18,2)
statusVARCHAR

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