What is the purpose of the ROWS BETWEEN clause in SQL window functions?
What is the purpose of the ROWS BETWEEN clause in SQL window functions?
326
04-Sep-2023
Updated on 25-Sep-2023
Aryan Kumar
25-Sep-2023The "ROWS BETWEEN" clause in SQL window functions is used to define a window or a subset of rows within the result set over which the window function operates. It allows you to specify the range or frame of rows that should be considered when performing calculations with window functions.
The "ROWS BETWEEN" clause typically includes the following options:
The "ROWS BETWEEN" clause is particularly useful for performing calculations that involve a specific range of rows within a partition. For example, you can calculate rolling averages, cumulative sums, or other aggregates over a defined window of rows.
Here's an example of how you might use the "ROWS BETWEEN" clause in SQL:
In this example, the "ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING" clause specifies a window that includes the current row and the row immediately before and after it. This allows you to calculate a rolling sum of order amounts for each customer within this specific window.