← Problems16. Running balance per accountMediumSQL
00:00 / 20:00

Running balance per account

Medium·Acceptance ·Asked at Microsoft, PayPal

Return every transaction with the account balance after that transaction. Transactions are ordered by posted_at; when two share a timestamp, the lower txn_id posts first.

Input schema

transactions txn_id bigint account_id bigint posted_at timestamp amount numeric -- negative for debits

Example

txn_id account_id amount balance 1 500 100.00 100.00 2 500 -30.00 70.00 3 500 200.00 270.00

Constraints

  • Output all input columns plus balance
  • Order by account_id, then posted_at, then txn_id
  • An account's balance starts at zero

Topics

window functions

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.sql
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8 · ⌘↵ run, ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.