Quick revision
The short answer
The running total is SUM(amount) OVER (ORDER BY day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), partitioned by year, and the 7-day moving average is AVG(amount) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW), which assumes exactly one row per day. With ORDER BY and no frame, the default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which includes every row with the same day and gives surprising totals for duplicates, so we spell the frame out; for gaps in the days we use a RANGE frame with an interval or fill the gaps with a calendar.
What to remember
In this answer, we wrote a year-to-date running total with ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW and a seven-day moving average with ROWS BETWEEN 6 PRECEDING AND CURRENT ROW, which assumes one row per day. With ORDER BY and no frame, the default RANGE frame includes all peers with the same day, which gives surprising totals for duplicate days, so we spell the frame out. For missing days we use a RANGE frame with an interval or fill the gaps with a calendar, and without ORDER BY the frame is the whole partition.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The running total is SUM(amount) OVER (ORDER BY day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), partitioned by year, and the 7-day moving average is AVG(amount) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW), which assumes exactly one row per day. With ORDER BY and no frame, the default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which includes every row with the same day and gives surprising totals for duplicates, so we spell the frame out; for gaps in the days we use a RANGE frame with an interval or fill the gaps with a calendar.
Why this question is asked
Running totals and moving averages are the bread and butter of reporting: revenue so far this year, a smoothed seven-day trend for a dashboard. They are also where window functions quietly return wrong numbers, because the default frame is not what most people assume. Interviewers ask this to see whether we know what a frame is and why spelling it out protects the report. In this answer, we will recap window frames, write the running total and the moving average, see what the default frame does with duplicate days, handle missing days, and note what happens without ORDER BY.
1A Quick Recap of Window Frames
A window function computes a value for each row over a set of related rows. PARTITION BY decides which rows belong together, and ORDER BY puts them in order. The frame then picks, for the current row, which rows of the ordered partition the function actually looks at, for example 'from the first row up to this one' or 'the six rows before this one and this one'.
Frames come in units. ROWS counts physical rows. RANGE works on the values of the ORDER BY column, so rows with the same value, called peers, are always together. GROUPS, supported in PostgreSQL 11 and later, counts groups of peers.
2The Running Total for the Year
A running total adds every amount from the start of the year up to the current day. We partition by year so the total restarts each January, and we write the frame explicitly.
UNBOUNDED PRECEDING means the first row of the partition, and CURRENT ROW means this row, so each value is the sum of everything so far. With the WHERE clause limited to 2025 the partition is not strictly needed, but it keeps the query correct when someone widens the date range to several years.
3The 7-Day Moving Average
A seven-day moving average for a day is the average of that day and the six days before it. With one row per day, that is the current row and the six rows before it.
Notice the first six days: the frame has fewer than seven rows, so the average is over the days available. A dashboard may prefer to show nothing there, which we can do with CASE WHEN COUNT(*) OVER (same frame) = 7 THEN ... END. Notice also the assumption: ROWS BETWEEN 6 PRECEDING only means 'six days' if there is exactly one row per day, with no duplicates and no gaps. We come back to both.
4The Default Frame and Duplicate Days
If we write SUM(amount) OVER (ORDER BY day) with no frame, the SQL standard default applies: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Because RANGE works on values, CURRENT ROW means 'the current row and all its peers', every row with the same day. On clean data with one row per day this gives the same answer as ROWS. On data with two rows for one day, it does not.
| Frame | Rows included | SUM |
|---|---|---|
| Default: RANGE UNBOUNDED PRECEDING to CURRENT ROW | Jan 1, both Jan 2 rows | 400 |
| ROWS UNBOUNDED PRECEDING to CURRENT ROW | Jan 1, this Jan 2 row | 350 |
| ROWS 6 PRECEDING to CURRENT ROW | Up to 6 rows before, and this one | 350 |
| No ORDER BY at all | Whole partition | 480 |
With the default frame, both rows of 2 January show 400, because each includes its peer. With ROWS, the first shows 350 and the second 400, a true row-by-row running total, though the order between the two peers is arbitrary unless we add a tiebreaker. Neither is wrong in itself, but if the author did not know which one they were getting, the report is wrong by accident. This is the reason to spell the frame out: it documents the intent and makes the result independent of a default most readers do not remember. The real fix for duplicates is usually to aggregate first, SELECT day, SUM(amount) FROM daily_sales GROUP BY day, and then apply the window.
5Handling Missing Days
Now the opposite problem: no sales on 5 January, so there is no row. ROWS BETWEEN 6 PRECEDING now reaches back eight calendar days instead of seven, and the average quietly covers a different period. There are two fixes.
The first is a RANGE frame with an interval offset, supported in PostgreSQL 11 and later and in MySQL 8.0. RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW includes every row whose day is within six days before the current day, however many rows that is.
Notice the difference in meaning. The RANGE version averages the days that had data, so a closed shop day does not pull the average down. The calendar version treats a missing day as zero sales. Which is right is a business question; the second is usually right for revenue, the first for measurements such as temperature.
6What Happens Without ORDER BY
If the OVER clause has no ORDER BY, there is no order, so there is no 'before' and 'after'. The frame is the whole partition, and every row gets the same value. SUM(amount) OVER (PARTITION BY date_trunc('year', day)) gives each row the full-year total, which is useful for a share of the year, amount / that total, but it is not a running total. Forgetting ORDER BY is a common way to get a column of identical numbers.
7Follow-up questions
Is ROWS faster than RANGE?
Often slightly, because ROWS frames can be computed with simple row counting while RANGE must find peer boundaries by comparing values. For running sums both are usually computed incrementally. The bigger reason to choose one is meaning, not speed.
How would you compute a month-to-date total that restarts each month?
Partition by month: SUM(amount) OVER (PARTITION BY date_trunc('month', day) ORDER BY day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). The partition boundary resets the sum on the first day of each month.
Does MySQL support these frames?
MySQL 8.0 and later support ROWS and RANGE frames, including RANGE with INTERVAL offsets on date columns, written as RANGE BETWEEN INTERVAL 6 DAY PRECEDING AND CURRENT ROW. It does not support GROUPS frames or EXCLUDE options as far as I know, so check your version if you need them.
8Conclusion
In this answer, we wrote a year-to-date running total with ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW and a seven-day moving average with ROWS BETWEEN 6 PRECEDING AND CURRENT ROW, which assumes one row per day. With ORDER BY and no frame, the default RANGE frame includes all peers with the same day, which gives surprising totals for duplicate days, so we spell the frame out. For missing days we use a RANGE frame with an interval or fill the gaps with a calendar, and without ORDER BY the frame is the whole partition.
Open with
For the running total I'd write SUM(amount) OVER (PARTITION BY the year, ORDER BY day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), so it adds everything from January 1st to the current day and restarts each year.
Then, if they want more
For the moving average I'd use AVG(amount) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW), which is seven rows, so it's only seven days if there is exactly one row per day.
I spell the frames out because the default, when there's an ORDER BY and no frame, is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, and RANGE includes all peers with the same ORDER BY value. If a day has two rows, both get the total including each other, which surprises people. Writing the frame makes the intent explicit.
Duplicates are usually best handled by aggregating to one row per day first. For gaps, ROWS 6 PRECEDING reaches back further than a week, so I'd either use RANGE BETWEEN INTERVAL 6 days PRECEDING AND CURRENT ROW, available since PostgreSQL 11, which averages the days that exist, or join to a calendar so missing days count as zero.
And without ORDER BY the frame is the whole partition, so every row gets the same value.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8.0, PostgreSQL 11
- 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.
- Official documentation
- PostgreSQL documentation, The SQL Language
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 "From daily_sales(day date, amount numeric), compute a running total for the year and a...": https://scrivio-interview-library.netlify.app/databases/advanced-sql/81704634e2/