Calculating Running Balances & Period-over-Period Growth
Compute running balances and period growth with CTEs and window helpers.
In plain words
Start with what you had, add each change, and write down the total at the end of every month.
Think of it like…
A savings jar: what's in it on 31 March is what was in it on 1 March plus that month's deposits and withdrawals, and if you didn't touch it in April, it still holds the same amount at the end of April.
Where you’ve seen it
A wallet app reports each user's month-end balance and monthly change: opening balances come from the account migration table, transactions are summed per month, a running SUM gives each month's closing balance, a month spine fills months with no activity by carrying the last balance forward, and closing - LAG(closing) gives the change.
Words you’ll hear
- Opening balance
- The balance before the first transaction in your data.
- Closing balance
- The balance at the end of a period: opening + cumulative net flow.
- Flow vs stock
- Flows (deposits, revenue) add up over time; stocks (balances) are snapshots.
- Carry forward
- Using the last known balance for periods with no transactions.
Visual diagram
Theory & example queries
Closing balances for every period
Aggregate transactions to net flow per account per month first; then the running total is a small window over monthly rows: opening + SUM(net) OVER (PARTITION BY account_id ORDER BY month ROWS UNBOUNDED PRECEDING). The opening balance comes from wherever history starts: a migration snapshot, an opening-balances table, or zero for accounts created inside the data.
Months with no transactions have no row, so they vanish from the report. Cross join a month spine with the accounts (from each account's first month), LEFT JOIN the monthly net flow and COALESCE it to 0; the running sum then naturally carries the balance forward. This is cheaper and more correct than trying to fill gaps after computing balances.
| id | owner | balance |
|---|---|---|
| 1 | Maya | 1000 |
| 2 | Ravi | 500 |
WITH m AS ( SELECT a.account_id, s.month FROM accounts a CROSS JOIN generate_series( '2026-01-01'::date, '2026-12-01', interval '1 month' ) AS s (MONTH) WHERE s.month >= date_trunc('month', a.opened_at) ), net AS ( SELECT account_id, date_trunc('month', txn_at) AS MONTH, SUM(amount) AS net FROM transactions GROUP BY 1, 2 )SELECT m.account_id, m.month, COALESCE(ob.amount, 0) + SUM(COALESCE(net.net, 0)) OVER ( PARTITION BY m.account_id ORDER BY m.month ) AS closingFROM m LEFT JOIN net USING (account_id, MONTH) LEFT JOIN opening_balances ob USING (account_id);PostgreSQL is the lesson baseline unless noted otherwise.
Read this line together with the surrounding statement as part of the same SQL expression.
Representative account balance spine3 sample rows
| account_id | month | closing |
|---|---|---|
| 42 | 2026-01-01 | 1000 |
| 42 | 2026-02-01 | 1000 |
| 42 | 2026-03-01 | 1350 |
Note: The sample account opens with 1000, has no February net flow, and receives 350 in March. The COALESCE and running SUM therefore carry the January balance through February.
Key takeaway: Net per period, spine for empty periods, opening + running SUM.
Growth on balances vs flows
Balances are stocks: the balance for March is the closing value, and a quarter's balance is its last month's closing, not the sum of three months. Growth is (closing - LAG(closing) OVER w) / NULLIF(LAG(closing) OVER w, 0), where NULLIF avoids dividing by zero for accounts that started empty; report those as NULL or "new" rather than infinite growth.
Flows such as deposits or revenue are summed within a period, and growth compares period totals. Mixing the two is a classic error: comparing this month's deposits with last month's closing balance, or averaging daily balances by summing them. For an average balance, take the time-weighted average of daily closing balances, which again needs a daily spine.
SELECT MONTH, SUM(closing) AS total_balance, SUM(closing) - LAG(SUM(closing)) OVER ( ORDER BY MONTH ) AS change, ROUND( 100.0 * ( SUM(closing) - LAG(SUM(closing)) OVER ( ORDER BY MONTH ) ) / NULLIF( LAG(SUM(closing)) OVER ( ORDER BY MONTH ), 0 ), 1 ) AS pctFROM month_end_balancesGROUP BY MONTHORDER BY MONTH;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative monthly balance growth3 sample rows
| month | total_balance | change | pct |
|---|---|---|---|
| 2026-01-01 | 20000 | NULL | NULL |
| 2026-02-01 | 21000 | 1000 | 5 |
| 2026-03-01 | 20500 | -500 | -2.4 |
Note: Sample totals are across accounts for each month. The first period has no previous balance, and NULLIF would return a NULL percentage if a previous total were zero.
Summing across accounts within a month is fine; summing across months isn't.
Key takeaway: Balances: last value per period, sum across accounts, never across time.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Find the opening balance
Take each account's starting balance from a snapshot or opening-balances table, or 0 for new accounts.
opening_balances(account_id, amount)
When to use it & trade-offs
What it is
A running balance is an opening balance plus the cumulative sum of transactions; period-over-period growth compares one period's value with the previous one. Combining them correctly means three extra steps beyond a simple running total: start from the right opening balance, produce a closing balance for every period (including months with no transactions), and compute growth on balances, which are snapshots, differently from flows like revenue.
Why it matters: Wallets, loans, inventory, subscriptions and bank accounts all need "balance at the end of each month" and "how much did it grow". These reports go wrong in subtle ways: a missing opening balance shifts every number, months without activity disappear, and summing balances across months produces nonsense. Finance and fintech interviews test exactly these details.
When to reach for it
- Wallet, loan and bank account statements
- Inventory on hand at month end
- Deferred revenue and outstanding balance reports
When to avoid or be careful
- Summing balances across months or quarters
- Dropping months with no transactions
- Dividing by a previous balance of zero
Ways to produce period-end balances
Strengths: Always consistent with transactions; easy to recompute.
Watch out: Needs the opening balance and a spine for empty periods.
Strengths: Fast reads; average balance is a simple AVG.
Watch out: Snapshots must be produced reliably and reconciled.
Strengths: Matches what users see on a statement.
Watch out: More rows than needed for monthly reporting.
Interview signals & numbers to know
When this topic comes up
- “Month-end balance per customer”
- “Balance growth month over month”
- “Months with no transactions are missing”
- Fintech and ledger questions
- Stock vs flow metrics
Benchmarks & rules of thumb
- opening + Σ net
- closing balance formula
- NULLIF(prev, 0)
- safe growth denominator
- last value
- how balances roll up over time
Common mistakes & follow-ups
Common interview mistakes
- Starting the running sum at 0 for migrated accounts that had an opening balance.
- Reporting a quarter's balance as the sum of its three month-end balances.
- Running the window over raw transactions and then picking a 'month-end' row, which is wrong for months without activity.
Likely interviewer follow-ups
- How would you report each account's balance at the end of every month, including inactive months?
- Why can't you sum balances across months?
- How do you compute an average daily balance?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Calculating Running Balances & Period-over-Period Growth with hands-on SQL practice and interview Q&A.