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_

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.

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

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