LESSON 58 / 62 · Window FramesWindow Frames, RANGE and ROWS
Window frames in SQL - a concept that defines a subset of rows considered in window functions. Learn the difference between a window frame and a partition, as well as how to define frame boundaries using ROWS and RANGE. Understand the differences between ROWS and RANGE in the context of SQL window functions.
In the context of SQL window functions, a "window frame" defines a subset of rows that are considered by the SQL function when performing calculations.
In other words, a window frame is a dynamic set of rows that "slides" through your query result, forming different data sets for each row, depending on your defined frame.
Window frame vs Partition
Although the terms "window frame" and "partition" may seem similar, they represent different concepts:
Partition (PARTITION BY). This is the division of the entire result set into non-overlapping subsets, where each subset contains rows with the same values in one or more columns. Window functions are applied separately to each partition, as if each were a separate data set.

Window frame. Defines which specific rows in each partition will be used for calculating the window function for each row. The frame can change from row to row.
For example, if the rule ROWS BETWEEN 1 PRECEDING AND CURRENT ROW is used, for each row the frame will consist of the row itself and one preceding row. This is like a "subpartition" within an existing partition.

That is:
- The first frame consists only of the 1st record, because there is no previous record. The single record is passed to the aggregate function AVG(price) and the result is added to the avg_price field.
- The second frame contains records 1 and 2, which are sent to AVG(price) and return (170 + 220) / 2 = 195.
- The third frame contains records 2 and 3, resulting in (220 + 150) / 2 = 185.
- and so on.
Note on window frame without ROWS/RANGE
If ROWS/RANGE is missing in the definition of a window function, then the default frame depends on whether ORDER BY is present.
If ORDER BY is absent, the frame coincides with the partition. In this case, the window function will process all rows within the partition, not limited to a subset. This means that the function result will be the same for all rows within the same partition.
If ORDER BY is present, the default rule is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. In this case, the window frame starts with the first row and ends with the current row, including all rows with the same sorting field values as the current row.
Defining window frames
Using the ROWS or RANGE syntax, we can define exactly which window frame will be passed to the window function for calculating the value for the current row.
The syntax for defining window frames looks like specifying a range relative to the current row.
MySQL 8.4
SELECT window_function(table_field)
OVER (
[PARTITION BY partition_column [, ...]]
[ORDER BY sort_column [, ...]]
{ROWS | RANGE} BETWEEN frame_start AND frame_end
)- [ ]optional part
- { a | b }one of the options
- ...can be repeated
The function calculated over the window, for example AVG or SUM.
The column whose values are passed to the function, for example price in AVG(price).
Divides the data into non-overlapping partitions: each one gets the rows with the same values in these columns.
Sets the order of rows within the window. This is especially important for ranking window functions.
How the bounds are counted: ROWS by rows, RANGE by the values of the sort columns.
The bound where the frame starts, for example 2 PRECEDING. The possible options are listed below.
- partition_column
- Optional
- sort_column
- Optional
- ROWS | RANGE
- Keyword
- frame_end
- The bound where the frame ends, for example CURRENT ROW.
For example, if we want only the two preceding records and the current row to be included in the window function calculation, then the syntax would look like this:
... ROWS|RANGE BETWEEN 2 PRECEDING AND CURRENT ROWIf we want the window function to include the current row and all subsequent ones, then the syntax would look like this:
... ROWS|RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWINGPossible window frame definitions
- UNBOUNDED PRECEDING, all rows preceding the current one
- N PRECEDING, N rows before the current row
- CURRENT ROW, the current row
- N FOLLOWING, N rows after the current row
- UNBOUNDED FOLLOWING, all subsequent rows
Window frame definition schema

The difference between ROWS and RANGE
For defining window frames, there are the keywords ROWS and RANGE. They work differently:
ROWS
Based on physical rows:
When using ROWS, the frame definition is based on the physical position of rows relative to the current row. For example, 1 PRECEDING means one row before the current one.
Precise frame:
Defining a frame with ROWS clearly limits the number of rows included in the frame, making it predictable and specific.

RANGE
Based on values:
RANGE, unlike ROWS, defines window frames based on column values, ordered according to ORDER BY in the window function.
Dynamic frames:
Frames defined with RANGE can vary depending on the data, making the frame flexible but potentially less predictable.

Let's check if you understood the difference between ROWS and RANGE correctly:
Which of the following statements best describes the difference between using ROWS and RANGE in the context of SQL window functions?
ROWS and RANGE are interchangeable, as both define physical rows in the window frameROWS defines window frames based on the physical position of rows, while RANGE is based on column values.RANGE uses the physical location of rows, while ROWS is based on column values.Both ROWS and RANGE define window frames solely based on column values.Check
Practice
Solve it in the editor. The query runs against a sample database in your browser.
Write a query and run it to see the results.
Write a query and run it to see the results.