Running Totals, LAG and LEAD
A window frame in SQL is the set of rows, within a window, that a window function actually uses for the current row, such as "all rows up to this one" or "the two rows before this one". It is used for running totals, moving averages, and any calculation that looks only at nearby rows. LAG and LEAD are window functions that reach one row back or forward, to compare each row with its neighbor. In this lesson, we will build running totals and moving averages with frames, compare payments with LAG and LEAD, and learn the two traps that make these queries return wrong numbers: RANGE with ties, and LAST_VALUE.
In One Minute
- With
ORDER BYinOVER,sum(x) OVER (ORDER BY d)is a running total. - A frame clause picks nearby rows:
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW. LAG(x)is the previous row's value;LEAD(x)is the next row's.- The default frame is
RANGE, which treats tied rows as one step; useROWSfor row-by-row totals. LAST_VALUEneeds a frame that reaches the end:ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
Syntax
function(x) OVER (
[PARTITION BY ...]
ORDER BY ...
{ ROWS | RANGE } BETWEEN frame_start AND frame_end
)| Frame boundary | Means |
|---|---|
UNBOUNDED PRECEDING | 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 | The last row of the partition |
Running Totals
sum(x) OVER (ORDER BY ... ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) adds up every row so far. The program's income over time:
SELECT payment_id, paid_on, amount_usd,
sum(amount_usd) OVER (ORDER BY paid_on
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total
FROM payments
ORDER BY paid_on; payment_id | paid_on | amount_usd | running_total
------------+------------+------------+---------------
1 | 2025-08-28 | 450.00 | 450.00
2 | 2025-10-30 | 250.00 | 700.00
6 | 2025-11-14 | 225.00 | 925.00
3 | 2025-12-03 | 250.00 | 1175.00
7 | 2025-12-19 | 225.00 | 1400.00
4 | 2025-12-29 | 550.00 | 1950.00
5 | 2026-01-05 | 200.00 | 2150.00
(7 rows)Explanation: For each payment, the frame runs from the first payment (in date order) to the current one, and sum adds them. The last row's running total equals the grand total, $2,150.00. With PARTITION BY student_id added, the running total restarts for each student:
SELECT payment_id, student_id, paid_on, amount_usd,
sum(amount_usd) OVER (PARTITION BY student_id ORDER BY paid_on
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS student_running
FROM payments
ORDER BY student_id, paid_on; payment_id | student_id | paid_on | amount_usd | student_running
------------+------------+------------+------------+-----------------
1 | 101 | 2025-08-28 | 450.00 | 450.00
2 | 101 | 2025-10-30 | 250.00 | 700.00
3 | 101 | 2025-12-03 | 250.00 | 950.00
4 | 101 | 2025-12-29 | 550.00 | 1500.00
5 | 101 | 2026-01-05 | 200.00 | 1700.00
6 | 103 | 2025-11-14 | 225.00 | 225.00
7 | 103 | 2025-12-19 | 225.00 | 450.00
(7 rows)Moving Averages
A frame of a fixed size slides along the rows, which gives a moving average. The average of each payment and the two before it:
SELECT payment_id, paid_on, amount_usd,
round(avg(amount_usd) OVER (ORDER BY paid_on
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 2) AS moving_avg_3
FROM payments
ORDER BY paid_on; payment_id | paid_on | amount_usd | moving_avg_3
------------+------------+------------+--------------
1 | 2025-08-28 | 450.00 | 450.00
2 | 2025-10-30 | 250.00 | 350.00
6 | 2025-11-14 | 225.00 | 308.33
3 | 2025-12-03 | 250.00 | 241.67
7 | 2025-12-19 | 225.00 | 233.33
4 | 2025-12-29 | 550.00 | 341.67
5 | 2026-01-05 | 200.00 | 325.00
(7 rows)Explanation: The first two rows have fewer than two rows before them, so their averages use one and two values. From the third row on, each average covers exactly three payments, smoothing out single large or small payments.
LAG and LEAD
LAG(x) returns x from the previous row of the window, and LEAD(x) from the next row. How many days passed between each student's payments?
SELECT payment_id, student_id, paid_on,
LAG(paid_on) OVER (PARTITION BY student_id ORDER BY paid_on) AS previous_payment,
paid_on - LAG(paid_on) OVER (PARTITION BY student_id ORDER BY paid_on) AS days_since
FROM payments
ORDER BY student_id, paid_on; payment_id | student_id | paid_on | previous_payment | days_since
------------+------------+------------+------------------+------------
1 | 101 | 2025-08-28 | NULL | NULL
2 | 101 | 2025-10-30 | 2025-08-28 | 63
3 | 101 | 2025-12-03 | 2025-10-30 | 34
4 | 101 | 2025-12-29 | 2025-12-03 | 26
5 | 101 | 2026-01-05 | 2025-12-29 | 7
6 | 103 | 2025-11-14 | NULL | NULL
7 | 103 | 2025-12-19 | 2025-11-14 | 35
(7 rows)Explanation: For each payment, LAG looks one row back within the same student. The first payment of each student has no previous row, so LAG returns NULL. LAG(x, 2) looks two rows back, and LAG(x, 1, default) returns default instead of NULL. LEAD works the same way, forward:
SELECT payment_id, student_id, amount_usd,
LEAD(amount_usd) OVER (PARTITION BY student_id ORDER BY paid_on) AS next_amount
FROM payments
ORDER BY student_id, paid_on; payment_id | student_id | amount_usd | next_amount
------------+------------+------------+-------------
1 | 101 | 450.00 | 250.00
2 | 101 | 250.00 | 250.00
3 | 101 | 250.00 | 550.00
4 | 101 | 550.00 | 200.00
5 | 101 | 200.00 | NULL
6 | 103 | 225.00 | 225.00
7 | 103 | 225.00 | NULL
(7 rows)The RANGE Trap
When a frame isn't written, and OVER has an ORDER BY, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. RANGE treats rows with the same ORDER BY value as one step. Ordering by amount, where there are ties, shows the difference:
SELECT payment_id, amount_usd,
sum(amount_usd) OVER (ORDER BY amount_usd) AS default_range,
sum(amount_usd) OVER (ORDER BY amount_usd, payment_id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS rows_frame
FROM payments
ORDER BY amount_usd, payment_id; payment_id | amount_usd | default_range | rows_frame
------------+------------+---------------+------------
5 | 200.00 | 200.00 | 200.00
6 | 225.00 | 650.00 | 425.00
7 | 225.00 | 650.00 | 650.00
2 | 250.00 | 1150.00 | 900.00
3 | 250.00 | 1150.00 | 1150.00
1 | 450.00 | 1600.00 | 1600.00
4 | 550.00 | 2150.00 | 2150.00
(7 rows)Explanation: Payments 6 and 7 are both $225.00. With the default RANGE frame, both rows include both payments, so both show 650.00: the "running" total jumps by two payments at once. With ROWS, each row adds only itself: 425.00, then 650.00. Ordering by paid_on earlier hid this because no two payments share a date.
The LAST_VALUE Trap
FIRST_VALUE(x) and LAST_VALUE(x) return x from the first and last row of the frame, not of the partition. With the default frame ending at the current row, LAST_VALUE just returns the current row:
SELECT payment_id, student_id, paid_on,
FIRST_VALUE(paid_on) OVER (PARTITION BY student_id ORDER BY paid_on) AS first_paid,
LAST_VALUE(paid_on) OVER (PARTITION BY student_id ORDER BY paid_on) AS last_wrong,
LAST_VALUE(paid_on) OVER (PARTITION BY student_id ORDER BY paid_on
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_right
FROM payments
ORDER BY student_id, paid_on; payment_id | student_id | paid_on | first_paid | last_wrong | last_right
------------+------------+------------+------------+------------+------------
1 | 101 | 2025-08-28 | 2025-08-28 | 2025-08-28 | 2026-01-05
2 | 101 | 2025-10-30 | 2025-08-28 | 2025-10-30 | 2026-01-05
3 | 101 | 2025-12-03 | 2025-08-28 | 2025-12-03 | 2026-01-05
4 | 101 | 2025-12-29 | 2025-08-28 | 2025-12-29 | 2026-01-05
5 | 101 | 2026-01-05 | 2025-08-28 | 2026-01-05 | 2026-01-05
6 | 103 | 2025-11-14 | 2025-11-14 | 2025-11-14 | 2025-12-19
7 | 103 | 2025-12-19 | 2025-11-14 | 2025-12-19 | 2025-12-19
(7 rows)Explanation: last_wrong equals each row's own date, because the frame stops at the current row. last_right uses a frame that covers the whole partition, so every row shows the student's latest payment date.
Common Mistakes
Trusting the default frame. It's RANGE, which merges ties. Write ROWS BETWEEN ... for running totals.
LAST_VALUE with the default frame. It returns the current row. Extend the frame to UNBOUNDED FOLLOWING.
Forgetting PARTITION BY with LAG. The first payment of Navika would "follow" Srihitha's last one.
No ORDER BY in OVER. Running totals and LAG need an order to mean anything.
Interview Corner
How do you calculate a running total in SQL?
sum(amount) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), with PARTITION BY to restart it per group.
What do LAG and LEAD do?
They return a column's value from a previous (LAG) or following (LEAD) row in the window, NULL if there's none, so each row can be compared with its neighbors.
What's the difference between ROWS and RANGE frames?
ROWS counts physical rows. RANGE works on values, so rows that tie on the ORDER BY value are treated as one group and get the same result.
Practice
Challenge: For each student, show each payment's date, amount, and the change in amount from their previous payment (amount_usd - LAG(amount_usd)), sorted by student and date.
Show solution
SELECT student_id, paid_on, amount_usd,
amount_usd - LAG(amount_usd) OVER (PARTITION BY student_id ORDER BY paid_on) AS change
FROM payments
ORDER BY student_id, paid_on; student_id | paid_on | amount_usd | change
------------+------------+------------+---------
101 | 2025-08-28 | 450.00 | NULL
101 | 2025-10-30 | 250.00 | -200.00
101 | 2025-12-03 | 250.00 | 0.00
101 | 2025-12-29 | 550.00 | 300.00
101 | 2026-01-05 | 200.00 | -350.00
103 | 2025-11-14 | 225.00 | NULL
103 | 2025-12-19 | 225.00 | 0.00
(7 rows)Each student's first payment has no previous one, so the change is NULL. Srihitha's payments rise and fall; Navika's two installments are equal, so her change is 0.00.
Key Takeaways
| You want to | Write |
|---|---|
| Running total | sum(x) OVER (ORDER BY d ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) |
| 3-row moving average | avg(x) OVER (ORDER BY d ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) |
| Previous value | LAG(x) OVER (PARTITION BY g ORDER BY d) |
| Last value of the group | LAST_VALUE(x) OVER (... ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) |
Was this tutorial helpful?