How would you calculate a running balance that depends on transaction type (deposit or withdrawal) in SQL?
Quick Overview
Asks how to compute a per-account running balance in SQL when the transaction type decides whether an amount adds or subtracts. Tests window functions, frame and ordering choices, tie handling for same-timestamp rows, and folding a condition into an aggregate.
How would you calculate a running balance that depends on transaction type (deposit or withdrawal) in SQL?
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
How would you calculate a running balance or cumulative sum that depends on conditional logic such as transaction type (deposit or withdrawal)?
Assume a table `transactions(account, txn_id, dt, type, amount)`, where `amount` is stored as a positive number and `type` says whether the row is a deposit or a withdrawal. The balance should be computed per account, in transaction order.
```hint One expression to sum
Think about what single expression you could feed to a window function so that one running aggregate does all the work.
```
```hint Same-instant rows
Check what your running total shows when two transactions on the same account share a timestamp.
```
### What a Strong Answer Covers
- How the transaction type is turned into a value a running sum can use
- The window function, with its partition, ordering and frame
- Deterministic results when transactions share a timestamp
- Handling of unexpected transaction types, NULL amounts and data that is already signed
- How the same pattern extends to other conditional running metrics, and where it stops working
### Follow-up Questions
- How would you find, for each account, the first transaction at which the balance went negative?
- How would you include an opening balance stored in a separate accounts table?
- How would you compute the running balance without window functions, and how does the cost compare?
- How would you compute the same running balance in pandas?
Overview: Asks how to compute a per-account running balance in SQL when the transaction type decides whether an amount adds or subtracts. Tests window functions, frame and ordering choices, tie handling for same-timestamp rows, and folding a condition into an aggregate.
How would you calculate a running balance or cumulative sum that depends on conditional logic such as transaction type (deposit or withdrawal)?
Assume a table transactions(account, txn_id, dt, type, amount), where amount is stored as a positive number and type says whether the row is a deposit or a withdrawal. The balance should be computed per account, in transaction order.
What a Strong Answer Covers Guidance
How the transaction type is turned into a value a running sum can use
The window function, with its partition, ordering and frame
Deterministic results when transactions share a timestamp
Handling of unexpected transaction types, NULL amounts and data that is already signed
How the same pattern extends to other conditional running metrics, and where it stops working
Follow-up Questions Guidance
How would you find, for each account, the first transaction at which the balance went negative?
How would you include an opening balance stored in a separate accounts table?
How would you compute the running balance without window functions, and how does the cost compare?
How would you compute the same running balance in pandas?