Full answer
sql
CREATE TABLE daily_sales (
day TEXT PRIMARY KEY,
amount INTEGER NOT NULL
);
INSERT INTO daily_sales VALUES
('2026-03-01',120),('2026-03-02',80),('2026-03-03',150),
('2026-03-05',90),('2026-03-06',60),('2026-03-09',200);sql
SELECT day, amount,
SUM(amount) OVER () AS total,
SUM(amount) OVER (ORDER BY day) AS running
FROM daily_sales ORDER BY day;| day | amount | total | running |
|---|---|---|---|
| 2026-03-01 | 120 | 700 | 120 |
| 2026-03-02 | 80 | 700 | 200 |
| 2026-03-03 | 150 | 700 | 350 |
| 2026-03-05 | 90 | 700 | 440 |
| 2026-03-06 | 60 | 700 | 500 |
| 2026-03-09 | 200 | 700 | 700 |
The total column is 700 everywhere. The running column grows row by row, because ORDER BY inside OVER changes the frame, not just the order.
This is the most common source of surprise with window functions: adding ORDER BY to get a "nicer" window silently changes the numbers. When you want the grand total, leave the window empty or say ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
sql
CREATE TABLE daily_sales (
day TEXT PRIMARY KEY,
amount INTEGER NOT NULL
);
INSERT INTO daily_sales VALUES
('2026-03-01',120),('2026-03-02',80),('2026-03-03',150),
('2026-03-05',90),('2026-03-06',60),('2026-03-09',200);
SELECT day, amount,
SUM(amount) OVER () AS total,
SUM(amount) OVER (ORDER BY day) AS running
FROM daily_sales ORDER BY day;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- OVER () is one partition, whole-set frame
- SUM OVER () gives the grand total per row
- ORDER BY in OVER changes the default frame
- Write the frame explicitly when in doubt
Common mistakes
- Adding ORDER BY to OVER () and getting running totals by accident
- Thinking ORDER BY inside OVER only affects output order
Follow-ups
- What is the default frame with ORDER BY?
- How would you show share of total per row?
Tips
- Show both columns side by side