intermediateTop 30 Scenario-Based Questions

How do you calculate the running balance of a bank account from a transactions table?

CREATE TABLE transactions (txn_id BIGSERIAL, account_id INT, amount DECIMAL(10,2), -- positive=deposit, negative=withdrawal txn_date TIMESTAMP DEFAULT NOW(), description TEXT); SELECT txn_id, txn_date, amount, SUM(amount) OVER (PARTITION BY account_id ORDER BY txn_date, txn_id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_balance FROM transactions WHERE account_id = 42 ORDER BY txn_

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to You need to merge (upsert) customer records from a CSV import — insert new, update existing. How in PostgreSQL?← Back to all SQL questions