← Back to SQL, page 6

sql-0116 · Hard senior

A running total over transactions jumps by two amounts at once on days with two transactions. Why, and how do you fix it?

With ORDER BY in the window and no frame clause, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. RANGE includes all peers, meaning rows with the same ORDER BY value, so both transactions on the same day show the day's combined total. Use ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW and a unique tie-breaker for a row-by-row running total.

Full answer
sql
CREATE TABLE txns (id INTEGER PRIMARY KEY, day TEXT, amount INTEGER);
INSERT INTO txns VALUES (1,'2026-03-01',100),(2,'2026-03-02',50),(3,'2026-03-02',25),(4,'2026-03-03',10);
sql
SELECT id, day, amount,
       SUM(amount) OVER (ORDER BY day) AS running_default
FROM txns
ORDER BY id;
iddayamountrunning_default
12026-03-01100100
22026-03-0250175
32026-03-0225175
42026-03-0310185

Both rows for 2 March show 175. RANGE treats rows with the same day as peers and includes all of them in the frame of each. So the "running" total jumps from 100 to 175 in one step and the intermediate value is never shown.

ROWS counts physical rows instead, so each row adds only itself. A tie-breaker (id) makes the order of the two same-day rows defined:

sql
SELECT id, day, amount,
       SUM(amount) OVER (ORDER BY day, id
                         ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_rows
FROM txns
ORDER BY id;
iddayamountrunning_rows
12026-03-01100100
22026-03-0250150
32026-03-0225175
42026-03-0310185

Which is right depends on the question. "Balance at the end of each day" wants the RANGE behaviour, and a GROUP BY day would be even clearer. "Balance after each transaction" wants ROWS.

PostgreSQL behaves the same:

sql
SELECT id, SUM(amount) OVER (ORDER BY day) AS running_default
FROM txns ORDER BY id;
idrunning_default
1100
2175
3175
4185

A third frame type, GROUPS (in PostgreSQL 11+ and SQLite 3.28+), counts groups of peers, which is handy for "this day and the previous two days" when days have several rows.

sql
CREATE TABLE txns (id INTEGER PRIMARY KEY, day TEXT, amount INTEGER);
INSERT INTO txns VALUES (1,'2026-03-01',100),(2,'2026-03-02',50),(3,'2026-03-02',25),(4,'2026-03-03',10);

SELECT id, day, amount,
       SUM(amount) OVER (ORDER BY day) AS running_default
FROM txns
ORDER BY id;

SELECT id, day, amount,
       SUM(amount) OVER (ORDER BY day, id
                         ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_rows
FROM txns
ORDER BY id;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • Default frame with ORDER BY is RANGE ... CURRENT ROW
  • RANGE includes all peers with the same ORDER BY value
  • ROWS includes only physical rows up to the current one
  • Add a unique tie-breaker for deterministic row-by-row totals

Common mistakes

  • Relying on the default frame for per-transaction running balances
  • ROWS without a tie-breaker, which makes same-day order arbitrary

Follow-ups

  • What does GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW mean?
  • How would you show the end-of-day balance only once per day?

Tips

  • Name the default frame exactly; interviewers look for RANGE