Full answer
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;| id | day | amount | running_default |
|---|---|---|---|
| 1 | 2026-03-01 | 100 | 100 |
| 2 | 2026-03-02 | 50 | 175 |
| 3 | 2026-03-02 | 25 | 175 |
| 4 | 2026-03-03 | 10 | 185 |
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:
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;| id | day | amount | running_rows |
|---|---|---|---|
| 1 | 2026-03-01 | 100 | 100 |
| 2 | 2026-03-02 | 50 | 150 |
| 3 | 2026-03-02 | 25 | 175 |
| 4 | 2026-03-03 | 10 | 185 |
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:
SELECT id, SUM(amount) OVER (ORDER BY day) AS running_default
FROM txns ORDER BY id;| id | running_default |
|---|---|
| 1 | 100 |
| 2 | 175 |
| 3 | 175 |
| 4 | 185 |
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.
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