LAG, LEAD, FIRST_VALUE and LAST_VALUE: Comparing Rows With Their Neighbours
How to read values from other rows of a window: LAG and LEAD for the previous and next row, offsets and defaults, day-over-day changes and gaps between events, FIRST_VALUE, LAST_VALUE and NTH_VALUE and the frame trap, and filling gaps with the last known value, with or without IGNORE NULLS.
kvi5hnuCode Sprout team member11 min readOn this page
Many questions compare a row with the row before or after it. How much did takings change since yesterday? How many days passed between a customer's orders? Is this reading higher than the last one? In a spreadsheet, you'd point at the cell above. In SQL, rows have no position until you define an order, and before window functions, "the previous row" needed a self-join on "the latest earlier date", which is long and slow.
The value window functions answer these questions directly. LAG and LEAD read a value from a row a given number of rows back or forward in the window's order. FIRST_VALUE, LAST_VALUE and NTH_VALUE read a value from a given position in the window frame. This article covers all five, the frame trap that makes LAST_VALUE look broken, and how to fill gaps with the last known value.
The examples use the daily_sales table from article 28: takings from 1 to 8 March, with no row for 4 March.
LAG and LEAD
LAG(expr) returns the value of expr from the previous row in the window's ORDER BY. LEAD(expr) returns it from the next row:
-- Daily takings of a market stall; no trading on 4 March
CREATE TABLE daily_sales (
day DATE PRIMARY KEY,
amount DECIMAL(8, 2) NOT NULL
);
INSERT INTO daily_sales VALUES
(DATE '2026-03-01', 120.00),
(DATE '2026-03-02', 80.00),
(DATE '2026-03-03', 150.00),
(DATE '2026-03-05', 60.00),
(DATE '2026-03-06', 200.00),
(DATE '2026-03-07', 90.00),
(DATE '2026-03-08', 110.00);
\pset null '(null)'
-- Compare each day with the day before and the day after
SELECT day,
amount,
LAG(amount) OVER (ORDER BY day) AS previous_amount,
amount - LAG(amount) OVER (ORDER BY day) AS change,
LEAD(amount) OVER (ORDER BY day) AS next_amount,
LAG(day) OVER (ORDER BY day) AS previous_day
FROM daily_sales
ORDER BY day;
day | amount | previous_amount | change | next_amount | previous_day
------------+--------+-----------------+---------+-------------+--------------
2026-03-01 | 120.00 | (null) | (null) | 80.00 | (null)
2026-03-02 | 80.00 | 120.00 | -40.00 | 150.00 | 2026-03-01
2026-03-03 | 150.00 | 80.00 | 70.00 | 60.00 | 2026-03-02
2026-03-05 | 60.00 | 150.00 | -90.00 | 200.00 | 2026-03-03
2026-03-06 | 200.00 | 60.00 | 140.00 | 90.00 | 2026-03-05
2026-03-07 | 90.00 | 200.00 | -110.00 | 110.00 | 2026-03-06
2026-03-08 | 110.00 | 90.00 | 20.00 | (null) | 2026-03-07
(7 rows)
For 5 March, LAG(amount) is 150.00, the amount on 3 March, the previous row. It's not 4 March, which has no row. LEAD(amount) is 200.00, from 6 March. amount - LAG(amount) is the change since the previous row: down 90.00 on 5 March.

At the edges, there's no row to read. The first row has no previous row, so LAG returns NULL there, and the last row has no next row, so LEAD returns NULL. Any calculation with those NULLs, such as the change column, is NULL too.
LAG and LEAD need an ORDER BY in OVER to be meaningful, since "previous" means nothing without an order (SQL Server and Oracle require it). They ignore the frame clause: they always count rows within the whole partition.
Offsets and defaults
Both functions take two optional arguments: LAG(expr, offset, default).
-- Daily takings of a market stall; no trading on 4 March
CREATE TABLE daily_sales (
day DATE PRIMARY KEY,
amount DECIMAL(8, 2) NOT NULL
);
INSERT INTO daily_sales VALUES
(DATE '2026-03-01', 120.00),
(DATE '2026-03-02', 80.00),
(DATE '2026-03-03', 150.00),
(DATE '2026-03-05', 60.00),
(DATE '2026-03-06', 200.00),
(DATE '2026-03-07', 90.00),
(DATE '2026-03-08', 110.00);
\pset null '(null)'
-- Offsets and defaults: two rows back, and 0 instead of NULL at the edge
SELECT day,
amount,
LAG(amount, 2) OVER (ORDER BY day) AS two_rows_back,
LAG(amount, 1, 0) OVER (ORDER BY day) AS previous_or_zero,
ROUND(100.0 * (amount - LAG(amount) OVER (ORDER BY day))
/ LAG(amount) OVER (ORDER BY day), 1) AS pct_change,
day - LAG(day) OVER (ORDER BY day) AS days_since_previous
FROM daily_sales
ORDER BY day;
day | amount | two_rows_back | previous_or_zero | pct_change | days_since_previous
------------+--------+---------------+------------------+------------+---------------------
2026-03-01 | 120.00 | (null) | 0 | (null) | (null)
2026-03-02 | 80.00 | (null) | 120.00 | -33.3 | 1
2026-03-03 | 150.00 | 120.00 | 80.00 | 87.5 | 1
2026-03-05 | 60.00 | 80.00 | 150.00 | -60.0 | 2
2026-03-06 | 200.00 | 150.00 | 60.00 | 233.3 | 1
2026-03-07 | 90.00 | 60.00 | 200.00 | -55.0 | 1
2026-03-08 | 110.00 | 200.00 | 90.00 | 22.2 | 1
(7 rows)
- Offset:
LAG(amount, 2)reads two rows back. The default offset is 1. For weekly comparisons on daily data with no gaps,LAG(amount, 7)gives the same weekday a week earlier. - Default:
LAG(amount, 1, 0)returns 0 instead of NULL when there's no row to read. The default only replaces the missing row, not a NULL value that the row actually contains. Note that it's shown as0rather than0.00, because it's the literal 0 converted tonumeric, not a value from the column. - Percentage change:
(amount - previous) / previous * 100. If the previous value could be 0, protect the division withNULLIF(LAG(amount) OVER (...), 0). - Gaps:
LAGworks on any column, not only the one you're measuring.day - LAG(day)gives the number of days since the previous row, which reveals that 4 March is missing. That's the basis of gap detection in the next article.
When the same OVER clause is repeated several times, a named WINDOW (article 26) keeps the query short, as the next example shows.
Comparing events per entity
With PARTITION BY, LAG and LEAD stay within each partition, which is how you compare each customer's, account's or device's events with its own previous events:
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
category VARCHAR(20) NOT NULL,
brand VARCHAR(20),
price DECIMAL(8, 2) NOT NULL,
stock INTEGER NOT NULL,
weight_g INTEGER
);
INSERT INTO products VALUES
(1, 'Ridge 2 tent', 'tents', 'Alpina', 249.99, 12, 2100),
(2, 'Summit 4 tent', 'tents', 'Alpina', 489.00, 3, 4300),
(3, 'Glow head torch', 'lighting', 'Lumen', 34.50, 40, 90),
(4, 'Basecamp lantern', 'lighting', NULL, 27.00, 15, NULL),
(5, 'Trail 30 backpack', 'packs', 'Kestrel', 89.95, 22, 1150),
(6, 'Day 18 backpack', 'packs', 'Kestrel', 54.00, 0, 640),
(7, 'Merino socks', 'clothing', 'Woolly', 16.00, 75, 80);
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
city VARCHAR(30),
referred_by INTEGER REFERENCES customers (customer_id)
);
INSERT INTO customers VALUES
(1, 'Asha Rao', 'Pune', NULL),
(2, 'Ben Okafor', 'Leeds', 1),
(3, 'Chen Wei', 'Pune', 1),
(4, 'Dana Cruz', 'York', 2),
(5, 'Eli Novak', 'Leeds', NULL),
(6, 'Farah Haddad', NULL, 3);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers (customer_id), -- NULL = guest checkout
placed_on DATE NOT NULL,
status VARCHAR(10) NOT NULL
);
INSERT INTO orders VALUES
(101, 1, DATE '2026-03-02', 'shipped'),
(102, 2, DATE '2026-03-05', 'shipped'),
(103, 1, DATE '2026-03-09', 'delivered'),
(104, 3, DATE '2026-03-15', 'pending'),
(105, 4, DATE '2026-03-20', 'cancelled'),
(106, NULL, DATE '2026-03-22', 'shipped');
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders (order_id),
product_id INTEGER REFERENCES products (product_id),
qty INTEGER NOT NULL,
unit_price DECIMAL(8, 2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);
INSERT INTO order_items VALUES
(101, 1, 1, 249.99), (101, 7, 3, 16.00),
(102, 3, 2, 34.50),
(103, 5, 1, 89.95), (103, 3, 1, 34.50), (103, 7, 2, 15.00),
(104, 2, 1, 489.00),
(105, 6, 1, 54.00),
(106, 7, 1, 16.00);
\pset null '(null)'
-- Per customer: days since their previous order, and whether another order follows
SELECT customer_id,
order_id,
placed_on,
placed_on - LAG(placed_on) OVER w AS days_since_previous,
LEAD(order_id) OVER w IS NOT NULL AS has_later_order
FROM orders
WHERE customer_id IS NOT NULL
WINDOW w AS (PARTITION BY customer_id ORDER BY placed_on)
ORDER BY customer_id, placed_on;
customer_id | order_id | placed_on | days_since_previous | has_later_order
-------------+----------+------------+---------------------+-----------------
1 | 101 | 2026-03-02 | (null) | t
1 | 103 | 2026-03-09 | 7 | f
2 | 102 | 2026-03-05 | (null) | f
3 | 104 | 2026-03-15 | (null) | f
4 | 105 | 2026-03-20 | (null) | f
(5 rows)
Asha's second order came 7 days after the first. Every customer's first order has no previous order in its partition, so days_since_previous is NULL, even though other customers ordered earlier. LEAD(order_id) IS NOT NULL tells whether the customer ordered again afterwards, which flags each customer's latest order.
Typical uses of this pattern:
- the time between a user's logins, or between a machine's failures
- each payment compared with the previous payment of the same account
- the status before and after each change in an audit log
- session detection: a new session starts when the gap since the previous event exceeds 30 minutes (article 30)
FIRST_VALUE, LAST_VALUE and NTH_VALUE
These three functions read a value from a position within the window frame:
FIRST_VALUE(expr): from the frame's first row.LAST_VALUE(expr): from the frame's last row.NTH_VALUE(expr, n): from the frame's nth row, or NULL if the frame has fewer than n rows.
Unlike LAG and LEAD, they use the frame, and that causes the most common surprise with window functions:
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
category VARCHAR(20) NOT NULL,
brand VARCHAR(20),
price DECIMAL(8, 2) NOT NULL,
stock INTEGER NOT NULL,
weight_g INTEGER
);
INSERT INTO products VALUES
(1, 'Ridge 2 tent', 'tents', 'Alpina', 249.99, 12, 2100),
(2, 'Summit 4 tent', 'tents', 'Alpina', 489.00, 3, 4300),
(3, 'Glow head torch', 'lighting', 'Lumen', 34.50, 40, 90),
(4, 'Basecamp lantern', 'lighting', NULL, 27.00, 15, NULL),
(5, 'Trail 30 backpack', 'packs', 'Kestrel', 89.95, 22, 1150),
(6, 'Day 18 backpack', 'packs', 'Kestrel', 54.00, 0, 640),
(7, 'Merino socks', 'clothing', 'Woolly', 16.00, 75, 80);
-- FIRST_VALUE works with the default frame; LAST_VALUE does not
SELECT category,
name,
price,
FIRST_VALUE(name) OVER (PARTITION BY category ORDER BY price) AS cheapest,
LAST_VALUE(name) OVER (PARTITION BY category ORDER BY price) AS last_value_default,
LAST_VALUE(name) OVER (PARTITION BY category ORDER BY price
ROWS BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING) AS dearest
FROM products
ORDER BY category, price;
category | name | price | cheapest | last_value_default | dearest
----------+-------------------+--------+------------------+--------------------+-------------------
clothing | Merino socks | 16.00 | Merino socks | Merino socks | Merino socks
lighting | Basecamp lantern | 27.00 | Basecamp lantern | Basecamp lantern | Glow head torch
lighting | Glow head torch | 34.50 | Basecamp lantern | Glow head torch | Glow head torch
packs | Day 18 backpack | 54.00 | Day 18 backpack | Day 18 backpack | Trail 30 backpack
packs | Trail 30 backpack | 89.95 | Day 18 backpack | Trail 30 backpack | Trail 30 backpack
tents | Ridge 2 tent | 249.99 | Ridge 2 tent | Ridge 2 tent | Summit 4 tent
tents | Summit 4 tent | 489.00 | Ridge 2 tent | Summit 4 tent | Summit 4 tent
(7 rows)
FIRST_VALUE(name) ... ORDER BY price correctly returns each category's cheapest product. But LAST_VALUE(name) with the same window returns each row's own name rather than the dearest product. The reason is the default frame (article 28). With an ORDER BY in OVER, the frame ends at the current row and its peers. So for the lantern, the frame contains only the lantern, and its last row is the lantern itself.
The fix is to extend the frame to the end of the partition:
LAST_VALUE(name) OVER (PARTITION BY category ORDER BY price
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
Two simpler alternatives avoid the frame entirely: FIRST_VALUE(name) OVER (PARTITION BY category ORDER BY price DESC) reverses the order so the dearest product comes first, and MAX(price) OVER (PARTITION BY category) gives the highest value directly when you need the value rather than another column of its row.
NTH_VALUE has the same trap:
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
category VARCHAR(20) NOT NULL,
brand VARCHAR(20),
price DECIMAL(8, 2) NOT NULL,
stock INTEGER NOT NULL,
weight_g INTEGER
);
INSERT INTO products VALUES
(1, 'Ridge 2 tent', 'tents', 'Alpina', 249.99, 12, 2100),
(2, 'Summit 4 tent', 'tents', 'Alpina', 489.00, 3, 4300),
(3, 'Glow head torch', 'lighting', 'Lumen', 34.50, 40, 90),
(4, 'Basecamp lantern', 'lighting', NULL, 27.00, 15, NULL),
(5, 'Trail 30 backpack', 'packs', 'Kestrel', 89.95, 22, 1150),
(6, 'Day 18 backpack', 'packs', 'Kestrel', 54.00, 0, 640),
(7, 'Merino socks', 'clothing', 'Woolly', 16.00, 75, 80);
\pset null '(null)'
-- NTH_VALUE: the second-cheapest product overall, on every row
SELECT name,
price,
NTH_VALUE(name, 2) OVER (ORDER BY price
ROWS BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING) AS second_cheapest,
NTH_VALUE(name, 2) OVER (ORDER BY price) AS with_default_frame
FROM products
ORDER BY price;
name | price | second_cheapest | with_default_frame
-------------------+--------+------------------+--------------------
Merino socks | 16.00 | Basecamp lantern | (null)
Basecamp lantern | 27.00 | Basecamp lantern | Basecamp lantern
Glow head torch | 34.50 | Basecamp lantern | Basecamp lantern
Day 18 backpack | 54.00 | Basecamp lantern | Basecamp lantern
Trail 30 backpack | 89.95 | Basecamp lantern | Basecamp lantern
Ridge 2 tent | 249.99 | Basecamp lantern | Basecamp lantern
Summit 4 tent | 489.00 | Basecamp lantern | Basecamp lantern
(7 rows)
With a full frame, every row shows the second-cheapest product, the lantern. With the default frame, the first row's frame contains only the socks, so there's no second row yet, and the result is NULL. From the second row on, the frame is long enough.
Rule of thumb: whenever you use LAST_VALUE or NTH_VALUE, write the frame explicitly. FIRST_VALUE works with the default frame because every frame that starts at UNBOUNDED PRECEDING has the same first row.
NTH_VALUE isn't available in SQL Server. There, use ROW_NUMBER in a subquery and pick the row with the number you need.
Filling gaps with the last known value
A common data-cleaning task is to carry the last known value forward: a sensor sends a reading only when it can, and missing readings should repeat the previous one. LAG can't do this directly, because it reads the previous row even if that row's value is NULL:
\pset null '(null)'
-- Readings with gaps: carry the last known value forward
CREATE TABLE readings (taken_at INTEGER PRIMARY KEY, temp DECIMAL(4, 1));
INSERT INTO readings VALUES (1, 18.5), (2, NULL), (3, NULL), (4, 19.2), (5, NULL), (6, 20.1);
-- LAG returns the NULL itself, not the last non-NULL value
SELECT taken_at, temp, LAG(temp) OVER (ORDER BY taken_at) AS lag_temp
FROM readings
ORDER BY taken_at;
-- Without IGNORE NULLS: number the groups that start at each known value, then fill
SELECT taken_at,
temp,
MAX(temp) OVER (PARTITION BY grp) AS filled
FROM (SELECT taken_at, temp,
COUNT(temp) OVER (ORDER BY taken_at) AS grp -- counts non-NULLs so far
FROM readings) AS t
ORDER BY taken_at;
taken_at | temp | lag_temp
----------+--------+----------
1 | 18.5 | (null)
2 | (null) | 18.5
3 | (null) | (null)
4 | 19.2 | (null)
5 | (null) | 19.2
6 | 20.1 | (null)
(6 rows)
taken_at | temp | filled
----------+--------+--------
1 | 18.5 | 18.5
2 | (null) | 18.5
3 | (null) | 18.5
4 | 19.2 | 19.2
5 | (null) | 19.2
6 | 20.1 | 20.1
(6 rows)
The first query shows the problem: at reading 3, LAG(temp) is the NULL from reading 2, not 18.5.
The second query uses a portable trick that works in every database with window functions:
COUNT(temp) OVER (ORDER BY taken_at)is a running count of non-NULL values. It increases at each known reading and stays the same through the NULLs after it. So each known value and the NULLs that follow it share the same count, which serves as a group number.MAX(temp) OVER (PARTITION BY grp)takes the one non-NULL value in each group and puts it on every row of the group.
The result fills readings 2 and 3 with 18.5 and reading 5 with 19.2.
IGNORE NULLS
The SQL standard has a direct solution: the IGNORE NULLS option, which makes the value functions skip NULLs:
SELECT taken_at, temp,
LAST_VALUE(temp) IGNORE NULLS OVER (ORDER BY taken_at) AS filled
FROM readings;
LAST_VALUE ... IGNORE NULLS with the default frame returns the last non-NULL value up to the current row, which is exactly "carry forward". LAG(temp) IGNORE NULLS OVER (...) gives the previous non-NULL value.
Support varies:
- Oracle has supported
IGNORE NULLSfor years. - SQL Server 2022 added it for
FIRST_VALUE,LAST_VALUE,LAGandLEAD. - PostgreSQL 19, released in November 2026, added
IGNORE NULLSandRESPECT NULLSforLAG,LEAD,FIRST_VALUE,LAST_VALUEandNTH_VALUE. PostgreSQL 17, which these examples run on, doesn't have it, which is why the example above is shown without running it. - MySQL and SQLite don't support it, so use the counting trick.
Performance
LAG, LEAD and FIRST_VALUE cost little beyond the sort that the window needs, and replace self-joins that are much more expensive. The self-join version of "previous order per customer" has to find, for each order, the latest earlier order of the same customer, typically with a correlated subquery that runs once per row. As in every window query, an index on the PARTITION BY and ORDER BY columns can avoid the sort.
What's current
- PostgreSQL 19 (November 2026) added
IGNORE NULLSandRESPECT NULLStoLAG,LEAD,FIRST_VALUE,LAST_VALUEandNTH_VALUE. Earlier versions need the counting trick to skip NULLs. - SQL Server 2022 added
IGNORE NULLStoFIRST_VALUE,LAST_VALUE,LAGandLEAD. SQL Server still has noNTH_VALUE. LAG,LEAD,FIRST_VALUEandLAST_VALUEare available in PostgreSQL, MySQL 8.0+, SQL Server 2012+, Oracle, SQLite 3.25+ and MariaDB 10.2+.
Common questions
What does LAG return on the first row?
NULL, or the third argument if you give one: LAG(x, 1, 0) returns 0.
Does LAG skip missing dates?
LAG reads the previous row, whatever its date. If days are missing, the previous row may be several days earlier. Generate a full date series first if you need the previous calendar day.
Why does LAST_VALUE return the current row's value?
Because the default frame with ORDER BY ends at the current row. Add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, or use FIRST_VALUE with the order reversed.
Do LAG and LEAD use the frame?
No. They count rows in the partition, in the window's order. Only FIRST_VALUE, LAST_VALUE, NTH_VALUE and aggregates use the frame.
How do I get the previous non-NULL value?
With LAG(x) IGNORE NULLS in Oracle, PostgreSQL 19, SQL Server 2022 and other databases that support it. Elsewhere, use the running-count trick: group rows by COUNT(x) OVER (ORDER BY ...) and take MAX(x) per group.
Can I compare with the same day last week?
If there's exactly one row per day with no gaps, LAG(amount, 7). If days can be missing, join the table to itself on other.day = day - 7, or use a date series.
What happens with ties in the ORDER BY?
LAG and LEAD follow the row order, and the order among tied rows is arbitrary. Add a tie-breaker so "previous" is well defined.
Summary
LAG(expr, offset, default)andLEAD(expr, offset, default)read a value from a row before or after the current one in the window's order, within the partition. They ignore the frame.- They return NULL, or the default, where no such row exists.
- Use them for changes between consecutive rows, percentage changes, time between events and comparisons per entity with
PARTITION BY. FIRST_VALUE,LAST_VALUEandNTH_VALUEread from positions in the frame. With the default frame,LAST_VALUEreturns the current row's value. WriteROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.- To carry the last known value forward, use
IGNORE NULLSwhere available (Oracle, PostgreSQL 19, SQL Server 2022), or group by a runningCOUNTof non-NULL values and takeMAXper group.
Practice questions
Write a query that shows each day's amount and the difference from the previous day's amount, showing 0 instead of NULL for the first day.
Answer
SELECT day, amount,
amount - LAG(amount, 1, amount) OVER (ORDER BY day) AS change
FROM daily_sales
ORDER BY day;
Using amount itself as the default makes the first day's change 0. COALESCE(amount - LAG(amount) OVER (ORDER BY day), 0) is an equivalent alternative.
What does LEAD(amount, 2) OVER (ORDER BY day) return for the 7 March row?
Answer
NULL. Two rows after 7 March would be the row after 8 March, which doesn't exist.
Write a query that returns, for each product, the name of the most expensive product in the same category, using FIRST_VALUE.
Answer
SELECT category, name, price,
FIRST_VALUE(name) OVER (PARTITION BY category ORDER BY price DESC) AS dearest
FROM products
ORDER BY category, price;
Ordering by price descending puts the dearest product first, so FIRST_VALUE works with the default frame. Using LAST_VALUE with ascending order would need an explicit frame to the end of the partition.
Find the orders that were placed fewer than 5 days after the previous order of the whole shop (any customer).
Answer
SELECT order_id, placed_on, gap
FROM (SELECT order_id, placed_on,
placed_on - LAG(placed_on) OVER (ORDER BY placed_on) AS gap
FROM orders) AS t
WHERE gap < 5
ORDER BY placed_on;
The gaps are 3 days (102), 4 days (103), 6 days (104), 5 days (105) and 2 days (106), so orders 102, 103 and 106 are returned. The gap must be computed in a derived table because window functions can't be used in WHERE.
A query uses NTH_VALUE(name, 3) OVER (ORDER BY price) and the first two rows show NULL. Why?
Answer
With ORDER BY and no frame clause, the frame ends at the current row. On the first two rows, the frame has fewer than three rows, so there's no third value yet. Add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING to give every row the same, complete frame.
Previous: Window Frames: Running Totals and Moving Averages With ROWS, RANGE and GROUPS
Next: Gaps and Islands: Finding Consecutive Ranges and Missing Values

More in SQL Course
Quiz: SQL Dialect Differences (PostgreSQL, MySQL, SQL Server, Oracle, SQLite)
Twenty-four questions on how the major SQL databases differ: limiting rows, concatenation, empty strings, identifier case, upserts, identity columns, transactional DDL, error handling, isolation defaults, booleans, set operations, RETURNING, dates and typing.
Quiz: SQL Delicate Details
Twenty-five questions on SQL's subtle corners: NULLs in aggregates, IN and CHECK, sort order of NULLs, rounding, CHAR padding, window frame defaults, identity gaps, LIKE escapes, empty groups, set operations and multi-column updates.
Quiz: SQL Interview Questions
Thirty SQL questions of the kind asked in developer and analyst interviews: WHERE vs HAVING, DELETE vs TRUNCATE, keys, joins, duplicates, Nth-highest values, window functions, ACID, isolation, locking, indexes, views, procedures and performance.
Quiz: SQL Course Final Exam (All 15 Parts)
The final exam for the SQL course: thirty questions across all fifteen parts, from query processing, NULLs, joins and aggregation through windows, data changes, types, design, indexes, security, transactions, procedural SQL and tuning.
Discussion
Sign in to ask a question or share what you learned.