Account Balance Calculation
PayPal 👶 Easy SQL
Beginner Mode

Objective

Write a SQL query to calculate the final account balance for each account based on all transactions.

Table Schema:

  • transactions
Column Type Description
transaction_id INTEGER Unique identifier for each transaction
account_id INTEGER ID of the account
transaction_type VARCHAR Type of transaction ('deposit' or 'withdrawal')
amount DECIMAL Transaction amount

Task Requirements:

  • Process all transactions for each account
  • Add deposits and subtract withdrawals
  • Calculate the final balance for each account
  • Sort results by account ID in ascending order

Output columns: account_id, final_balance

Examples

Example 1:

Input:

transactions
account_idamounttransaction_idtransaction_type
10110001deposit
1012002withdrawal
1025003deposit
1013004deposit
1021005withdrawal

Output:
account_idfinal_balance
1011100
102400
Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →