Before continuing: This entry is part of the window functions guide. If you are new to the topic, start with OVER.
Example
WITH sales(day, amount) AS (
VALUES ('2026-08-01', 10),
('2026-08-02', 20),
('2026-08-03', 30),
('2026-08-04', 40)
)
SELECT day,
amount,
SUM(amount) OVER (
ORDER BY day
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) AS sum_3_rows
FROM sales
ORDER BY day;A running sum of markers can identify groups of consecutive dates. In the case of consecutive days and gaps and islands, ROWS from the start up to the current row is used to number each user's streaks.
How it is used
2 PRECEDING means “up to two rows back”, not “two days back”. Its meaning depends on where the rows sit in the ORDER BY clause of the window.
What changes from case to case
Bounds you should recognise
UNBOUNDED PRECEDING: from the start of the partition.CURRENT ROW: the current row inROWSmode; in other modes, the idea of peer rows also comes into play.n PRECEDING: n rows before, in a frame usingROWS.n FOLLOWING: n rows after, in a frame usingROWS.UNBOUNDED FOLLOWING: to the end.
ROWS is not the same as RANGE
ROWS counts physical rows in the window's order. RANGE works with the sort value and its peers, and its exact rules for offsets depend on the engine. GROUPS works with groups of peer rows on engines that support it.
When you are learning: start with ROWS when you want a specific number of preceding or following rows. Add a deterministic sort if the result depends on which row counts as the previous one.
Avoid this confusion
Confusing the partition with the frame
PARTITION BY category can mark off all the products in a category, while the frame can use only the last three rows of that category to calculate a moving average. They are two different levels.