Sliding Window Frames: ROWS & RANGE
Sliding Window Frames: ROWS & RANGE
In intermediate SQL, window functions calculate aggregates across whole partitions. In advanced analytical engineering, you often need to restrict calculations to a moving subset of rows relative to the current row—such as a 7-day rolling average or a trailing 3-order moving sum.
This dynamic sliding subset is called the Window Frame, configured using the ROWS or RANGE sub-clauses inside OVER().
Frame Specification Syntax
Frame Boundary Keywords:
UNBOUNDED PRECEDING: Start from the very first row in the partition.n PRECEDING: Startnrows (or values) before the current row.CURRENT ROW: The current row being evaluated.n FOLLOWING: Endnrows (or values) after the current row.UNBOUNDED FOLLOWING: Extend to the very last row in the partition.
ROWS vs RANGE: The Critical Difference
| Dimension | ROWS | RANGE |
|---|---|---|
| Unit of Measurement | Physical row count | Logical value offsets |
| Tie Handling | Distinguishes identical values row-by-row | Treats rows with identical ORDER BY values as a single peer group |
| Performance | Faster (counts physical offsets) | Slower (evaluates value ranges) |
ORDER BY inside OVER() without declaring a frame, MySQL uses:
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
This default means that tied values are included together in the running calculation!Practical Example: Trailing 3-Day Moving Average
Logical Value Offsets with RANGE in MySQL 8.0
In MySQL 8.0+, RANGE supports numeric and temporal offsets:
Multiple Choice Questions
1. What is the fundamental difference between ROWS and RANGE in window frame specifications?
A. ROWS only works on numeric data, while RANGE only works on dates B. ROWS defines the frame by physical row count, while RANGE defines it by logical value offsets C. RANGE is deprecated in MySQL 8.0 D. ROWS can only look forward, never backward Answer: B Explanation: ROWS counts physical offset positions relative to the current row, whereas RANGE calculates logical value intervals based on the ORDER BY expression.
2. What is the default window frame when ORDER BY is provided in an OVER() clause without an explicit frame clause?
A. ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING B. RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW C. ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING D. RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING Answer: B Explanation: The SQL standard default with ORDER BY is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, causing tied values to be evaluated together.
3. Which frame specification defines a 3-row sliding window including the previous row, current row, and next row?
A. ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING B. RANGE BETWEEN 3 PRECEDING AND CURRENT ROW C. ROWS 3 PRECEDING D. SLIDE 3 ROWS Answer: A Explanation: ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING captures exactly one physical row prior, the current row, and one physical row subsequent.
4. How can you calculate a 14-day rolling revenue window when some dates have zero sales and are missing from the table?
A. ROWS BETWEEN 14 PRECEDING AND CURRENT ROW B. RANGE BETWEEN INTERVAL 14 DAY PRECEDING AND CURRENT ROW C. GROUP BY DAY(date) D. UNBOUNDED FOLLOWING Answer: B Explanation: RANGE with temporal INTERVAL calculates the calendar date range accurately regardless of missing rows, whereas ROWS would improperly count 14 recorded transactions.
5. What does UNBOUNDED FOLLOWING designate as a frame boundary?
A. The beginning of the table B. The end of the current partition C. The next transaction ID D. The current row Answer: B Explanation: UNBOUNDED FOLLOWING sets the upper boundary to the very last row of the partition.
Cumulative Aggregates: Running Totals & Moving Averages
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| None | Cumulative Aggregates: Running Totals & Moving Averages |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.