Account Balance - Problem

You're working at a digital bank and need to track real-time account balances after each transaction.

Given a table Transactions containing all bank transactions, calculate the cumulative balance after each transaction for every account.

Key Points:

  • All accounts start with a balance of 0
  • Deposits increase the balance
  • Withdrawals decrease the balance
  • The balance will never go below 0 (guaranteed)

Return the result ordered by account_id, then by day.

Table Schema

Transactions
Column Name Type Description
account_id PK int Unique identifier for the account
day PK date Date of the transaction
type ENUM('Deposit', 'Withdraw') Type of transaction
amount int Transaction amount (always positive)
Primary Key: (account_id, day)
Note: Each account has at most one transaction per day

Input & Output

Example 1 โ€” Multiple accounts with various transactions
Input Table:
account_id day type amount
2000 1 2021-11-07 Deposit
1 1000 2021-11-09 Withdraw
Deposit 1 3000 2021-11-11
7000 2021-12-07 Deposit 2
Withdraw 7000 2021-12-12 2
Output:
account_id day type amount balance
Deposit 1 2000 2000 2021-11-07
Withdraw 1000 1 2021-11-09 1000
3000 Deposit 2021-11-11 4000 1
Deposit 7000 7000 2021-12-07 2
0 2021-12-12 2 7000 Withdraw
๐Ÿ’ก Note:

Account 1:

  • 2021-11-07: Deposit +2000 โ†’ Balance = 0 + 2000 = 2000
  • 2021-11-09: Withdraw -1000 โ†’ Balance = 2000 - 1000 = 1000
  • 2021-11-11: Deposit +3000 โ†’ Balance = 1000 + 3000 = 4000

Account 2:

  • 2021-12-07: Deposit +7000 โ†’ Balance = 0 + 7000 = 7000
  • 2021-12-12: Withdraw -7000 โ†’ Balance = 7000 - 7000 = 0
Example 2 โ€” Single account with deposits only
Input Table:
account_id day type amount
Deposit 1 500 2021-01-01
Deposit 300 2021-01-15 1
2021-02-01 200 1 Deposit
Output:
account_id day type amount balance
1 2021-01-01 500 500 Deposit
1 800 300 2021-01-15 Deposit
1000 2021-02-01 1 Deposit 200
๐Ÿ’ก Note:

All transactions are deposits, so balance keeps increasing: 500 โ†’ 800 โ†’ 1000

Constraints

  • 1 โ‰ค account_id โ‰ค 1000
  • 1 โ‰ค amount โ‰ค 10000
  • type is either 'Deposit' or 'Withdraw'
  • Each (account_id, day) pair is unique
  • Balance never goes below 0

Visualization

Tap to expand
Account Balance: Running TotalDeposit+20002021-11-07Balance2000Withdraw-10002021-11-09Balance1000Deposit+30002021-11-11Balance4000SQL Window FunctionSUM( CASE type WHEN 'Deposit' THEN amount ELSE -amount END) OVER ( PARTITION BY account_id ORDER BY day)
Understanding the Visualization
1
Partition by Account
Each account gets its own calculation window
2
Order by Date
Transactions processed chronologically within each account
3
Calculate Running Sum
Deposits add, withdrawals subtract from cumulative total
Key Takeaway
๐ŸŽฏ Key Insight: Window functions with PARTITION BY create separate running totals for each account, while ORDER BY ensures chronological accumulation.
Asked in
Amazon 45 Google 32 PayPal 28 Stripe 21
45.0K Views
High Frequency
~15 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen