8.1 5 Practical Examples of Using ROWS BETWEEN in SQL
8.1 5 Practical Examples of Using ROWS BETWEEN in SQL
Window functions (also called OVER functions) compute their result based on a
sliding window frame (i.e. a set of rows). They are similar to aggregate functions in
that you can calculate the average, total, or minimum/maximum value across a
group of rows. However, there are some important differences:
https://learnsql.com/blog/sql-window-functions-rows-clause/ 1/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
The best way to learn window functions is our interactive Window Functions
course. There are 218 exercises that will teach you how window functions work,
what functions there are, and how to apply them to real-world problems. You only
need a web browser and some basic SQL knowledge.
Code
When you use a window function in the SELECT statement, you basically calculate
another column with this function:
A window frame is defined using ROWS , RANGE , and GROUPS clauses. In this
article, we’ll focus on the ROWS clause and its options. To learn more about
https://learnsql.com/blog/sql-window-functions-rows-clause/ 2/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
window functions and defining window frames, check out this article with window
functions examples, this explanation guide, and of course, our two-page SQL
Window Functions Cheat Sheet.
https://learnsql.com/blog/sql-window-functions-rows-clause/ 3/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
Here are a couple of things to keep in mind when defining window frames with the
ROWS clause:
https://learnsql.com/blog/sql-window-functions-rows-clause/ 4/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
Example 1
To get started with the ROWS clause, we’ll use the following table with sales data
from a book store.
sales
1 2021-09-01 1515.45
2 2021-09-02 2345.35
3 2021-09-03 903.99
4 2021-09-04 2158.55
5 2021-09-05 1819.80
In our first example, we want to add another column that shows the total revenue
from the first date up to the current row’s date (i.e. running total). Here’s the query
we can use:
Code
https://learnsql.com/blog/sql-window-functions-rows-clause/ 5/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
Code
As you see, the query worked as intended and we got the running total in our third
column. On the first day, it equals the sales from this day – $1515.45; on the
second day, it equals the sum of sales from the first and second days – $3860.80;
in the next row, we get the sum of sales from the first three days – $4764.79, etc.
In our next examples, we’ll see how the ROWS clause works when the records are
divided into several groups.
https://learnsql.com/blog/sql-window-functions-rows-clause/ 6/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
To practice defining window frames, check out this interactive Window Functions
course with 200+ coding challenges.
Example 2
For the next couple of examples, we’ll use the table below. It contains fictional
data on average temperature (in °C) and total precipitation (in mm) in two Italian
cities (Rome and Florence) over five consecutive days.
weather
2021-
101 09- Rome 18.5 7
01
2021-
102 09- Florence 17.3 5
01
2021-
103 09- Rome 18.0 20
02
2021-
104 09- Florence 17.0 15
02
2021-
105 09- Rome 20.1 12
03
2021-
106 09- Florence 19.0 10
03
https://learnsql.com/blog/sql-window-functions-rows-clause/ 7/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
weather
2021-
107 09- Rome 20.2 0
04
2021-
108 09- Florence 19.6 0
04
2021-
109 09- Rome 22.5 0
05
2021-
110 09- Florence 20.4 0
05
Note also that we’ve put our window function inside the ROUND() function so that
the three-day moving average is rounded to one decimal place. Here’s the result:
2021-
Florence 09- 17.3 17.3
01
2021-
Florence 09- 17.6 17.5
02
2021-
Florence 09- 19.0 18.0
03
2021-
Florence 09- 19.6 18.7
04
https://learnsql.com/blog/sql-window-functions-rows-clause/ 8/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
05
2021-
Rome 09- 18.5 18.5
01
2021-
Rome 09- 19.0 18.8
02
2021-
Rome 09- 20.1 19.2
03
2021-
Rome 09- 20.2 19.8
04
2021-
Rome 09- 22.5 20.9
05
The moving average was calculated separately for Florence and Rome. For
September 1st, the moving average equals the average daily temperature, as we
don’t have any preceding records. Then, on September 2nd, the moving average is
calculated as the average temperature for the 1st and 2nd (17.5 °C in Florence and
18.8 °C in Rome, respectively). On September 3rd, we finally have enough data to
calculate the average temperature for three days (the two preceding and the
current day), which turns out to be 18.0 °C in Florence and 19.2°C in Rome. Then,
the three-day moving average for Sep 4th is calculated as the average temperature
for the 2nd, 3rd, and 4th, and so on.
Here’s one more thing to note: The order of records in the window frame has a
key role in specifying which rows to consider.
In the query above, we have ordered the records in the window frame by date in
ascending order (using the default setting), i.e. we’re starting with the earliest date.
Then, to include two days before the current day in our calculations, we have set
the lower bound as 2 PRECEDING .
However, we could get the exact same window frame by ordering the records in
descending order, and then changing the ROWS option to include 2 FOLLOWING
https://learnsql.com/blog/sql-window-functions-rows-clause/ 9/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
instead of 2 PRECEDING :
Code
Example 3
In this example, we’ll calculate the total precipitation for the last three days (i.e. a
three-day running total) separately for two cities.
Code
In this query, we again partition the data by city. We use the SUM() function to
calculate the total level of precipitation for the last three days, including the current
day. Also, note that we use an abbreviation when defining the window frame by
specifying only the lower bound: 2 PRECEDING .
https://learnsql.com/blog/sql-window-functions-rows-clause/ 10/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
2021-
Florence 09- 5 5
01
2021-
Florence 09- 15 20
02
2021-
Florence 09- 10 30
03
2021-
Florence 09- 0 25
04
2021-
Florence 09- 0 10
05
2021-
Rome 09- 7 7
01
2021-
Rome 09- 20 27
02
2021-
Rome 09- 12 39
03
2021-
Rome 09- 0 32
04
2021-
Rome 09- 0 12
05
Do you know how we got the 12 mm running total for Rome on Sep 5th? Try to
follow the results in our output table to make sure you understand how window
https://learnsql.com/blog/sql-window-functions-rows-clause/ 11/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
Example 4
For the next two examples, we’ll be using the data shown below. It includes daily
information on the number of new subscribers across three social networks:
Instagram, Facebook, and LinkedIn.
subscribers
2021-
11 09- Instagram 40
01
2021-
12 09- Facebook 12
01
2021-
13 09- LinkedIn 5
01
2021-
14 09- Instagram 67
02
2021-
15 09- Facebook 23
02
16 2021- LinkedIn 2
09-
https://learnsql.com/blog/sql-window-functions-rows-clause/ 12/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
subscribers
02
2021-
17 09- Instagram 34
03
2021-
18 09- Facebook 25
03
2021-
19 09- LinkedIn 10
03
2021-
20 09- Instagram 85
04
2021-
21 09- Facebook 28
04
2021-
22 09- LinkedIn 20
04
Let’s start by calculating the running totals for the number of new subscribers
separately for each network. Basically, for each day, we want to see how many
people have subscribed since we started collecting data until the current row’s
date.
Code
https://learnsql.com/blog/sql-window-functions-rows-clause/ 13/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
We start by calculating the total number of new subscribers using the SUM()
aggregate function. Then, we use the PARTITION BY clause to compute separate
calculations for each network. We also sort the records by date in the ascending
order (by default). Finally, we define the window frame as UNBOUNDED
PRECEDING to include all records up to the current one inclusively.
2021-
09- Facebook 12 12
01
2021-
09- Facebook 23 35
02
2021-
09- Facebook 25 60
03
2021-
09- Facebook 28 88
04
2021-
09- Instagram 40 40
01
2021-
09- Instagram 67 107
02
2021-
09- Instagram 34 141
03
2021-
09- Instagram 85 226
04
2021-
09- LinkedIn 5 5
01
2021-
09- LinkedIn 2 7
02
https://learnsql.com/blog/sql-window-functions-rows-clause/ 14/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
2021-
09- LinkedIn 10 17
03
2021-
09- LinkedIn 20 37
04
In the results table, you can see how the number of new subscribers is added to the
cumulative total for each new record. The running total is calculated separately for
each network, as specified in the window function.
Example 5
In our final example, I want to demonstrate how we can display the first and the
last value of a specific set of records using window functions and the ROWS
clause. This time, let’s add two columns to the output:
With this information calculated separately for each social network, we can see
how every day’s performance compares to where we’ve started and where we are
now.
Code
https://learnsql.com/blog/sql-window-functions-rows-clause/ 15/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
As you see, we are using the FIRST_VALUE() and the LAST_VALUE() functions to
get the information on the first and the last days, respectively. Note also how we
specify the window frame for each of the functions:
We don’t include the ROWS clause with the FIRST_VALUE() function because
the default behavior (i.e. RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT
ROW ) is fine for our purposes.
However, we do specify the window frame with the LAST_VALUE() function
because the default option would use the current row value as the last value
for each record; this is not what we are looking for in this example. We specify
the window frame as ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
FOLLOWING to make sure all records are considered.
2021-
09- Facebook 12 12
01
2021-
09- Facebook 23 12
02
2021-
09- Facebook 25 12
03
2021-
09- Facebook 28 12
04
https://learnsql.com/blog/sql-window-functions-rows-clause/ 16/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
2021-
09- Instagram 40 40
01
2021-
09- Instagram 67 40
02
2021-
09- Instagram 34 40
03
2021-
09- Instagram 85 40
04
2021-
09- LinkedIn 5 5
01
2021-
09- LinkedIn 2 5
02
2021-
09- LinkedIn 10 5
03
2021-
09- LinkedIn 20 5
04
As requested, we have the number of new subscribers on the first and the last day
calculated separately for each social network.
https://learnsql.com/blog/sql-window-functions-rows-clause/ 17/21
3/11/24, 10:51 PM 5 Practical Examples of Using ROWS BETWEEN in SQL
If you want to really master using SQL for data analysis, our Advanced SQL
learning path also includes GROUP BY Extensions in SQL and common table
expressions (CTEs). It’s a great way to build on your window functions knowledge.
Want to start with some reading first? Here are the top 8 articles covering SQL
window functions.
https://learnsql.com/blog/sql-window-functions-rows-clause/ 18/21