Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A running total report uses SUM(amount) OVER (PARTITION BY account_id ORDER BY tx_date). On some days the running total jumps by several transactions at once and two rows show the same balance, though the final balance is correct. Explain, fix, and say whether balances should be computed this way at read time for a ledger with 2 billion rows.

With ORDER BY and no frame clause, the window frame defaults to RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which includes every peer row with the same tx_date, so all transactions of one day show the day's closing total. The fix is a unique order and an explicit frame: ORDER BY tx_date, id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. For a 2 billion row ledger we should not recompute from the beginning on every read; we store periodic balance snapshots, add the window sum only for rows after the snapshot, index (account_id, tx_date, id) and partition the ledger by time.

Why this question is asked

This bug is a favourite because the report is almost right. The final balance is correct, most rows look fine, and only on busy days do several rows jump together and repeat the same number. Anyone who has seen it once never forgets the cause, and anyone who has not tends to suspect the data. Interviewers ask it to see whether we understand window frames rather than copying them, and whether we think about running totals as a design choice on a very large table. In this answer, we will reproduce the jump, explain the default frame, fix it, and then design how balances should be read from a ledger with 2 billion rows, including the follow-up about back-dated transactions.

1Reproducing the Jump

Let us take one account with four transactions, two of them on the same day.

SQL
INSERT INTO ledger (id, account_id, tx_date, amount) VALUES
(1, 7, '2026-03-01', 100),
(2, 7, '2026-03-02', -30),
(3, 7, '2026-03-02', -20),
(4, 7, '2026-03-03', 50);
SELECT id, tx_date, amount,
SUM(amount) OVER (PARTITION BY account_id ORDER BY tx_date) AS balance
FROM ledger;
-- 1 | 03-01 | 100 | 100
-- 2 | 03-02 | -30 | 50 should be 70
-- 3 | 03-02 | -20 | 50
-- 4 | 03-03 | 50 | 100 the final balance is right

Notice the pattern from the question. On March 2 the balance jumps from 100 straight to 50, skipping 70, and both rows of that day show 50. On the last row everything is right again.

2Why the Default Frame Includes the Whole Day

A window frame decides which rows of the partition count for the current row. When we write ORDER BY without a frame, SQL uses a default: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. The word RANGE is the cause. In RANGE mode, "current row" does not mean this physical row; it means this row and all its peers, the rows with the same value in the ORDER BY column. Rows 2 and 3 both have tx_date March 2, so they are peers, and for each of them the frame includes both. Each gets 100 - 30 - 20 = 50.

So the database is doing exactly what the SQL standard says. The query simply never told it how to order transactions within a day, so it treated them as happening at the same moment.

The frame for row 2
FrameRows included for row 2Balance on row 2
default RANGE, ORDER BY tx_date1, 2 and 3 (3 is a peer)50
ROWS, ORDER BY tx_date, id1 and 270

3Fixing It With a Unique Order and ROWS

The fix has two parts. We add id to the ORDER BY, so the order inside a day is defined and stable, and we state the frame explicitly with ROWS, so the frame stops at the physical current row.

SQL
SELECT id, tx_date, amount,
SUM(amount) OVER (PARTITION BY account_id
ORDER BY tx_date, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS balance
FROM ledger
ORDER BY account_id, tx_date, id;
-- 1 | 03-01 | 100 | 100
-- 2 | 03-02 | -30 | 70
-- 3 | 03-02 | -20 | 50
-- 4 | 03-03 | 50 | 100

Either part alone would already fix this example: with a unique ORDER BY, RANGE has no peers, and with ROWS the peers do not matter. We write both because they protect against different future changes, someone removing id from the ORDER BY or someone adding a column that ties. ROWS is also often cheaper for the database, because it does not need to look ahead for peers. If id does not reflect the real order of transactions within a day, we use whatever does, such as a posting timestamp, with id as the last tie-breaker.

4Why Reading Balances From the Beginning Does Not Scale

Now the design question. On a ledger with 2 billion rows, this query computes each balance by adding up every transaction since the account was opened. An account with 50,000 transactions over ten years has to read all 50,000 rows to show this month's statement of 30 lines. Multiply that by millions of statement views and the database spends most of its time re-adding the same old transactions.

Step throughReading a statement for March
  1. Find the last snapshot before March 1closing balance 1,240.00 on Feb 28
  2. Read March rows by index(account_id, tx_date, id), about 30 rows
  3. Add a running sum to the snapshotROWS frame, ordered by tx_date, id
  4. Return 30 linesinstead of summing 50,000 rows

4 steps in order. Play them, or step through them one at a time.

The fix is to store what we already know. At the end of each day or month, a job writes a balance snapshot per account: the closing balance as of that date. A statement then starts from the last snapshot before the period and adds a window sum only for the rows after it.

5Snapshots, Indexes and Partitions

Here is the read query with a snapshot table. COALESCE handles a new account with no snapshot yet.

SQL
CREATE TABLE balance_snapshot (
account_id bigint, as_of date, closing_balance numeric NOT NULL,
PRIMARY KEY (account_id, as_of));
CREATE INDEX ledger_acct_date_id ON ledger (account_id, tx_date, id);
WITH snap AS (
SELECT as_of, closing_balance FROM balance_snapshot
WHERE account_id = :acct AND as_of < DATE '2026-03-01'
ORDER BY as_of DESC LIMIT 1)
SELECT l.id, l.tx_date, l.amount,
COALESCE((SELECT closing_balance FROM snap), 0)
+ SUM(l.amount) OVER (ORDER BY l.tx_date, l.id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS balance
FROM ledger l
WHERE l.account_id = :acct
AND l.tx_date > COALESCE((SELECT as_of FROM snap), DATE '-infinity')
AND l.tx_date < DATE '2026-04-01'
ORDER BY l.tx_date, l.id;

The index on (account_id, tx_date, id) makes the ledger read a range scan that returns rows already in the window's order, so there is no sort. Partitioning the ledger by time, for example one partition per month, keeps each partition and its indexes a manageable size, lets queries skip old partitions entirely, and lets us move or archive old years cheaply. With monthly snapshots the window sum covers at most about a month of rows per account.

6Follow-up questions

How do you keep snapshots correct when back-dated transactions arrive?

Either recompute every snapshot of that account dated on or after the back-dated transaction, in the same transaction that inserts it or in a job that runs before statements are produced, or avoid the problem by snapshotting on the posting date, the date the entry was recorded, which only moves forward. Many ledgers do the second and show value-dated corrections as new entries, so closed snapshots never change.

Would RANK or ROW_NUMBER have the same peer issue?

Ranking functions do not use the frame at all, so the frame default does not affect them. Aggregate window functions like SUM, AVG, COUNT and also FIRST_VALUE and LAST_VALUE do use it, which is why LAST_VALUE with the default frame often surprises people too.

Is computing the running total in Java instead an option?

For one statement of 30 rows, yes, starting from the snapshot balance. But the database still has to read the rows, so the snapshot and the index matter just as much. Keeping it in SQL lets reports reuse the same logic.

7Conclusion

In this answer, we saw that SUM(amount) OVER (PARTITION BY account_id ORDER BY tx_date) uses the default frame RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which includes all peer rows with the same tx_date, so every transaction of a day shows the day's total. Ordering by tx_date, id with an explicit ROWS frame gives each row its own balance. For a 2 billion row ledger, we store periodic balance snapshots and add the window sum only for rows after the snapshot, index (account_id, tx_date, id) so each read is an ordered range scan, and partition the ledger by time.

What to say in the interviewAbout 100 seconds aloud

Open with

The cause is the default window frame. When you write ORDER BY in a window without a frame, the frame is RANGE between unbounded preceding and current row, and in RANGE mode the current row includes all its peers, meaning every row with the same tx date.

Then, if they want more

So all transactions on the same day get the total for the whole day: the balance jumps by several transactions at once and those rows repeat the same number, while the last row is still correct.

The fix is to give a unique order, tx date then id, or a posting timestamp if that reflects the real order, and an explicit ROWS between unbounded preceding and current row frame. Either alone fixes it, but I write both.

For a ledger with 2 billion rows, I would not compute balances from the beginning of time on every read, because a long-lived account would sum tens of thousands of rows for a 30-line statement.

I would store periodic balance snapshots, daily or monthly closing balances per account, and compute a statement as the last snapshot plus a window sum over the rows after it.

An index on account id, tx date and id makes that a range scan already in the right order, and I would partition the ledger by month so old data can be skipped and archived. For back-dated transactions, either recompute later snapshots or snapshot by posting date so closed snapshots never change.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A running total report uses SUM(amount) OVER (PARTITION BY account_id ORDER BY tx_date)....": https://scrivio-interview-library.netlify.app/databases/sql-coding-problems/31f94a5f30/