Quick Overview

Write a SQL query that computes the current balance of every account from a transactions table, where deposits add to the balance and withdrawals subtract from it. It tests conditional aggregation, grouping by account and handling accounts that have only one kind of transaction.

Calculate each account's final balance from deposits and withdrawals

Company: PayPal

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

A `transactions` table records every deposit and withdrawal made on an account. Write a SQL query to calculate the current `balance` of every account: the sum of its deposits minus the sum of its withdrawals. Return `account_id` and `balance`, ordered by `account_id` ascending. ```hint Lopsided accounts Some accounts have only deposits or only withdrawals. Check that your approach still returns a number for them. ``` ### Input Table(s) Table `transactions` | Column | Type | Meaning | |---|---|---| | `transaction_id` | INT | Transaction id | | `account_id` | INT | Account the transaction belongs to (NOT NULL) | | `transaction_type` | VARCHAR(20) | `'deposit'` or `'withdrawal'` (NOT NULL) | | `amount` | DECIMAL(10,2) | Transaction amount, always positive (NOT NULL) | | `transaction_date` | DATE | Date of the transaction (NOT NULL) | Primary key: `transaction_id`. There is no separate accounts table. ### Output Contract - Columns, in this order: `account_id`, `balance`. - One row per distinct `account_id` in `transactions`. - `balance` is the sum of deposit amounts minus the sum of withdrawal amounts, kept as a decimal with two places and no other rounding. It can be zero or negative. - Rows ordered by `account_id` ascending. Each `account_id` appears once in the output, so the order is unique. ### Example Input `transactions`: | transaction_id | account_id | transaction_type | amount | transaction_date | |---|---|---|---|---| | 1 | 201 | deposit | 250.00 | 2024-02-01 | | 2 | 201 | withdrawal | 250.00 | 2024-02-05 | | 3 | 202 | withdrawal | 50.00 | 2024-02-03 | Expected output: | account_id | balance | |---|---| | 201 | 0.00 | | 202 | -50.00 | Account 201 deposited 250 and withdrew 250, resulting in a balance of 0. Account 202 only has a withdrawal of 50, so the balance is -50. ### Constraints and Clarifications - The number of rows in `transactions` is between 1 and 10,000. - `transaction_type` is either `'deposit'` or `'withdrawal'`. - `amount` is a positive decimal value. - Each `transaction_id` is unique, and no column is ever `NULL`. - Two rows with the same account, type, amount and date are separate transactions, and both count. - `transaction_date` does not filter anything: the balance covers every row of the account. - A negative balance is reported as it is.

Overview: Write a SQL query that computes the current balance of every account from a transactions table, where deposits add to the balance and withdrawals subtract from it. It tests conditional aggregation, grouping by account and handling accounts that have only one kind of transaction.

A `transactions` table records every deposit and withdrawal made on an account. Write a SQL query to calculate the current `balance` of every account: the sum of its deposits minus the sum of its withdrawals. Return `account_id` and `balance`, ordered by `account_id` ascending. Table `transactions`: - `transaction_id` (INTEGER, primary key): transaction id. - `account_id` (INTEGER, NOT NULL): account the transaction belongs to. - `transaction_type` (VARCHAR(20), NOT NULL): either `'deposit'` or `'withdrawal'`. - `amount` (DECIMAL(10,2), NOT NULL): transaction amount, always positive. - `transaction_date` (DATE, NOT NULL): date of the transaction. There is no separate accounts table. Output: - Columns, in this order: `account_id`, `balance`. - One row per distinct `account_id` in `transactions`. - `balance` is the sum of deposit amounts minus the sum of withdrawal amounts, kept as a decimal with two places and no other rounding. It can be zero or negative. - Rows ordered by `account_id` ascending. Each `account_id` appears once in the output, so the order is unique. Clarifications: - `transaction_type` is either `'deposit'` or `'withdrawal'`, and `amount` is a positive decimal value. - Each `transaction_id` is unique, and no column is ever NULL. - Two rows with the same account, type, amount and date are separate transactions, and both count. - `transaction_date` does not filter anything: the balance covers every row of the account. - A negative balance is reported as it is.

Tables

transactions(transaction_id INTEGER, account_id INTEGER, transaction_type VARCHAR(20), amount DECIMAL(10,2), transaction_date DATE)

Hints

  1. Some accounts have only deposits or only withdrawals. Check that your approach still returns a number for them.
  2. Every row of an account counts toward its balance, including identical repeated transactions and rows on any date.
PayPalData ScientisteasyData Manipulation (SQL/Python)

Account Balances from Deposits and Withdrawals

A transactions table records every deposit and withdrawal made on an account. Write a SQL query to calculate the current balance of every account: the sum of its deposits minus the sum of its withdrawals. Return account_id and balance, ordered by account_id ascending.

Table transactions:

  • transaction_id (INTEGER, primary key): transaction id.
  • account_id (INTEGER, NOT NULL): account the transaction belongs to.
  • transaction_type (VARCHAR(20), NOT NULL): either 'deposit' or 'withdrawal'.
  • amount (DECIMAL(10,2), NOT NULL): transaction amount, always positive.
  • transaction_date (DATE, NOT NULL): date of the transaction.

There is no separate accounts table.

Output:

  • Columns, in this order: account_id, balance.
  • One row per distinct account_id in transactions.
  • balance is the sum of deposit amounts minus the sum of withdrawal amounts, kept as a decimal with two places and no other rounding. It can be zero or negative.
  • Rows ordered by account_id ascending. Each account_id appears once in the output, so the order is unique.

Clarifications:

  • transaction_type is either 'deposit' or 'withdrawal', and amount is a positive decimal value.
  • Each transaction_id is unique, and no column is ever NULL.
  • Two rows with the same account, type, amount and date are separate transactions, and both count.
  • transaction_date does not filter anything: the balance covers every row of the account.
  • A negative balance is reported as it is.

Expected Output

Tables

transactions
transactions
ColumnType
transaction_idINTEGER
account_idINTEGER
transaction_typeVARCHAR(20)
amountDECIMAL(10,2)
transaction_dateDATE

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