SQL Moving Averages and Frames
What you will learn
- What a frame is
- How to write a
ROWS BETWEENframe - How to build a moving average
- Why the first rows use fewer values
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Run the cells from top to bottom.
1. The frame
A window function sees a frame: the rows around the current row that it adds up. Lesson 8.4 used the default frame, which grows from the first row to the current row. You can choose another with ROWS BETWEEN start AND end:
| Bound | Meaning |
|---|---|
UNBOUNDED PRECEDING |
from the first row of the partition |
n PRECEDING |
n rows before the current row |
CURRENT ROW |
the current row |
n FOLLOWING |
n rows after the current row |
UNBOUNDED FOLLOWING |
to the last row of the partition |
2. A moving average
A moving average smooths a series by averaging a few neighbours. A 3-row average uses the current row and the two before it:
WITH monthly AS (
SELECT SUBSTR(order_date, 1, 7) AS month, COUNT(*) AS orders
FROM orders GROUP BY SUBSTR(order_date, 1, 7)
)
SELECT month, orders,
ROUND(AVG(orders) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 1) AS moving_avg_3
FROM monthly
ORDER BY month
LIMIT 6;
month orders moving_avg_3
------- ------ ------------
2024-01 17 17.0
2024-02 18 17.5
2024-03 27 20.7
2024-04 24 23.0
2024-05 43 31.3
2024-06 40 35.7
(6 rows)
WITH monthly AS (
SELECT SUBSTR(txn_date, 1, 7) AS month, COUNT(*) AS txns
FROM transactions GROUP BY SUBSTR(txn_date, 1, 7)
)
SELECT month, txns,
ROUND(AVG(txns) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 1) AS moving_avg_3
FROM monthly
ORDER BY month
LIMIT 6;
month txns moving_avg_3
------- ---- ------------
2018-02 2 2.0
2018-03 5 3.5
2018-04 6 4.3
2018-05 7 6.0
2018-06 11 8.0
2018-07 10 9.3
(6 rows)
WITH monthly AS (
SELECT SUBSTR(class_date, 1, 7) AS month, COUNT(*) AS records
FROM attendance GROUP BY SUBSTR(class_date, 1, 7)
)
SELECT month, records,
ROUND(AVG(records) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 1) AS moving_avg_3
FROM monthly
ORDER BY month
LIMIT 6;
month records moving_avg_3
------- ------- ------------
2025-02 1596 1596.0
2025-03 798 1197.0
2025-08 630 1008.0
2025-09 1260 896.0
2026-02 1152 1014.0
2026-03 576 996.0
(6 rows)
The first row's average is just itself; the second row's average uses two values; from the third row on, three. That is the edge effect: the frame cannot reach rows that do not exist.
3. Show the frame size
Count the rows in the frame to see the edge effect, and only trust full frames:
WITH monthly AS (
SELECT SUBSTR(order_date, 1, 7) AS month, COUNT(*) AS orders
FROM orders GROUP BY SUBSTR(order_date, 1, 7)
)
SELECT month, orders,
COUNT(*) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS rows_in_frame
FROM monthly
ORDER BY month
LIMIT 4;
month orders rows_in_frame
------- ------ -------------
2024-01 17 1
2024-02 18 2
2024-03 27 3
2024-04 24 3
(4 rows)
WITH monthly AS (
SELECT SUBSTR(txn_date, 1, 7) AS month, COUNT(*) AS txns
FROM transactions GROUP BY SUBSTR(txn_date, 1, 7)
)
SELECT month, txns,
COUNT(*) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS rows_in_frame
FROM monthly
ORDER BY month
LIMIT 4;
month txns rows_in_frame
------- ---- -------------
2018-02 2 1
2018-03 5 2
2018-04 6 3
2018-05 7 3
(4 rows)
WITH monthly AS (
SELECT SUBSTR(class_date, 1, 7) AS month, COUNT(*) AS records
FROM attendance GROUP BY SUBSTR(class_date, 1, 7)
)
SELECT month, records,
COUNT(*) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS rows_in_frame
FROM monthly
ORDER BY month
LIMIT 4;
month records rows_in_frame
------- ------- -------------
2025-02 1596 1
2025-03 798 2
2025-08 630 3
2025-09 1260 3
(4 rows)
To report only full frames, wrap the query in a CTE and keep rows where rows_in_frame = 3.
4. Centered and forward frames
Put the current row in the middle to smooth both ways, or look only ahead:
CREATE TABLE lib_series (n INTEGER, v INTEGER);
INSERT INTO lib_series VALUES (1, 10), (2, 20), (3, 60), (4, 20), (5, 10);
SELECT n, v,
AVG(v) OVER (ORDER BY n ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS centered_3,
SUM(v) OVER (ORDER BY n ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS this_and_next,
SUM(v) OVER (ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS whole_series
FROM lib_series ORDER BY n;
n v centered_3 this_and_next whole_series
- -- ------------------ ------------- ------------
1 10 15.0 30 120
2 20 30.0 80 120
3 60 33.333333333333336 80 120
4 20 30.0 30 120
5 10 15.0 10 120
(5 rows)
CREATE TABLE lib_series (n INTEGER, v INTEGER);
INSERT INTO lib_series VALUES (1, 10), (2, 20), (3, 60), (4, 20), (5, 10);
SELECT n, v,
AVG(v) OVER (ORDER BY n ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS centered_3,
SUM(v) OVER (ORDER BY n ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS this_and_next,
SUM(v) OVER (ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS whole_series
FROM lib_series ORDER BY n;
n v centered_3 this_and_next whole_series
- -- ------------------ ------------- ------------
1 10 15.0 30 120
2 20 30.0 80 120
3 60 33.333333333333336 80 120
4 20 30.0 30 120
5 10 15.0 10 120
(5 rows)
CREATE TABLE lib_series (n INTEGER, v INTEGER);
INSERT INTO lib_series VALUES (1, 10), (2, 20), (3, 60), (4, 20), (5, 10);
SELECT n, v,
AVG(v) OVER (ORDER BY n ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS centered_3,
SUM(v) OVER (ORDER BY n ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS this_and_next,
SUM(v) OVER (ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS whole_series
FROM lib_series ORDER BY n;
n v centered_3 this_and_next whole_series
- -- ------------------ ------------- ------------
1 10 15.0 30 120
2 20 30.0 80 120
3 60 33.333333333333336 80 120
4 20 30.0 30 120
5 10 15.0 10 120
(5 rows)
The spike (60) is spread over its neighbours in the centered average.
5. Highest and lowest in a frame
Any aggregate works in a frame. The highest value among the last 3 rows:
SELECT product_id, price,
MAX(price) OVER (ORDER BY product_id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS highest_of_last_3
FROM products
ORDER BY product_id
LIMIT 6;
product_id price highest_of_last_3
---------- ----- -----------------
1 6.6 6.6
2 44.1 44.1
3 29.5 44.1
4 5.4 44.1
5 26.4 29.5
6 79.0 79.0
(6 rows)
SELECT account_id, balance_cents,
MAX(balance_cents) OVER (ORDER BY account_id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS highest_of_last_3
FROM accounts
ORDER BY account_id
LIMIT 6;
account_id balance_cents highest_of_last_3
---------- ------------- -----------------
1 192400 192400
2 52078 192400
3 62780 192400
4 99900 99900
5 153100 153100
6 218400 218400
(6 rows)
SELECT course_id, credits,
MAX(credits) OVER (ORDER BY course_id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS highest_of_last_3
FROM courses
ORDER BY course_id
LIMIT 6;
course_id credits highest_of_last_3
--------- ------- -----------------
1 4 4
2 3 4
3 4 4
4 4 4
5 4 4
6 4 4
(6 rows)
6. ROWS, RANGE and GROUPS
According to the SQLite documentation there are three frame types: ROWS counts physical rows (what we used), RANGE works on the values of the ORDER BY column, and GROUPS counts groups of tied rows. ROWS is the least surprising, so use it unless you need the others. Missing months in the series also matter: a ROWS frame averages the previous rows, not the previous calendar months, so fill the gaps first with a generated calendar (lesson 7.7).
Worked example: choose the frame
| Goal | Frame |
|---|---|
| running total | ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
| 7-day moving average | ROWS BETWEEN 6 PRECEDING AND CURRENT ROW (one row per day) |
| centered 3-point smoothing | ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING |
| compare with the whole series | ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING |
A 7-row frame means 7 days only if there is exactly one row per day. The goals are made up.
Common mistakes
- Treating a short frame at the start as a full average.
- A
ROWSframe over a series with gaps, which averages the wrong period. - Forgetting the
ORDER BY, so the frame has no meaning. - Rounding too early, which hides small changes.
Key takeaways
- A frame is the set of rows around the current row that a window function uses.
ROWS BETWEEN n PRECEDING AND CURRENT ROWbuilds moving averages and sums.- The first rows have smaller frames; count the rows or filter for full frames.
- Fill gaps in the series before using a
ROWSframe.
Practice
1. Write the frame for a 3-row moving average ending at the current row.
Show answer
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
2. Why is the first moving average based on fewer values?
Show answer
There are no rows before the first one, so the frame is smaller.
3. What does ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING cover?
Show answer
The row before, the current row and the row after.
4. Why fill gaps before a 7-row frame?
Show answer
7 rows only equal 7 days if every day has a row.
5. How do you keep only full frames?
Show answer
Count the rows in the frame in a CTE and keep rows where the count is the full size.
6. Which frame type counts physical rows?
Show answer
ROWS.