Lag/Lead with Frame Windows
Combine LAG/LEAD with frame windows to compute period deltas and detect gaps in time series.
Lag/Lead with Frame Windows is a free SQL Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
LAG/LEAD Recap
LAG(col, n) returns the value n rows before the current row in the window. LEAD(col, n) looks ahead.
SELECT day, revenue,
LAG(revenue) OVER (ORDER BY day) AS prev_day_revenue
FROM daily_revenue;LAG with Default
The third argument is a default when no neighbour exists:
LAG(revenue, 1, 0) OVER (ORDER BY day)
-- Returns 0 instead of NULL for the very first row.Computing Deltas
Day-over-day change:
SELECT day, revenue,
revenue - LAG(revenue, 1, 0) OVER (ORDER BY day) AS delta
FROM daily_revenue;Multi-Step LAG
Compare to N days ago — useful for week-over-week, year-over-year:
LAG(revenue, 7) OVER (ORDER BY day) -- 7 days ago
LAG(revenue, 365) OVER (ORDER BY day) -- 1 year agoLEAD for Forecast Slots
"What's the next event?" patterns:
SELECT id, ts,
LEAD(ts) OVER (PARTITION BY user_id ORDER BY ts) AS next_ts,
LEAD(ts) OVER (PARTITION BY user_id ORDER BY ts) - ts AS gap_to_next
FROM events;Per-User Patterns
LAG/LEAD respect PARTITION BY:
SELECT user_id, ts, action,
LAG(action) OVER (PARTITION BY user_id ORDER BY ts) AS prev_action
FROM events;
-- prev_action is the user's previous event, not globally.Detecting State Changes
Combine LAG with CASE:
SELECT id, status,
CASE WHEN status <> LAG(status) OVER (ORDER BY ts) THEN 'changed' ELSE 'same' END
FROM orders;FIRST_VALUE / LAST_VALUE
The first or last value within the frame:
FIRST_VALUE(status) OVER (PARTITION BY order_id ORDER BY ts) AS first_status
-- The status when the order was first seen.
LAST_VALUE(status) OVER (
PARTITION BY order_id ORDER BY ts
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS final_statusNTH_VALUE
The Nth value of an ordered window:
NTH_VALUE(revenue, 3) OVER (
ORDER BY revenue DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
)
-- Third-highest revenue value.Per-Group Frame Functions
Combine LAG with PARTITION BY for grouped neighbours:
SELECT user_id, ts, page,
LAG(page) OVER (PARTITION BY user_id ORDER BY ts) AS prev_page,
LEAD(page) OVER (PARTITION BY user_id ORDER BY ts) AS next_page
FROM page_views;Performance
Window functions sort once per partition. Index the (PARTITION BY, ORDER BY) columns when partitions are large.
Recap
LAG/LEAD make row-to-row comparison cheap.
- Default value avoids NULL
- Combine with CASE for state-change detection
- FIRST_VALUE/LAST_VALUE/NTH_VALUE round out the family
Quick Check
You want each row to include the gap (in seconds) until the user's next event. Which function?
Frequently asked questions
Is the “Lag/Lead with Frame Windows” lesson free?
Yes — the full text of “Lag/Lead with Frame Windows” is free to read here on the web, and the SQL Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Academy course, upgrade to CoddyKit PRO.
What will I learn in “Lag/Lead with Frame Windows”?
Combine LAG/LEAD with frame windows to compute period deltas and detect gaps in time series. You practise SQL Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SQL Academy?
No prior experience is required. SQL Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Lag/Lead with Frame Windows” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SQL Academy lesson?
Yes. Every SQL Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Frame Clauses: ROWS vs RANGE
- Lag/Lead with Frame Windows
- Bucketing with NTILE and Cume_Dist
- Real-World Reporting Patterns