SQL window functions: ranking, running totals and LAG
12 steps · about 4 h 45 min · Intermediate
- How Window Functions work in SQL with animated Gifs (article, Dataschool, 10 min)
Animations show the window growing and sliding, which gives you the mental model before you see any syntax.
- PostgreSQL: Documentation: 3.5. Window Functions (doc, PostgreSQL official docs, 15 min)
The official short introduction covers OVER, PARTITION BY, ORDER BY, and why window functions can't go in WHERE.
- SQL Window Functions (tutorial, sqltutorial.org, 20 min)
It contrasts SUM as an aggregate with SUM as a window function, with a Try it button for each query.
- SQL Window Functions (interactive, Mode SQL Tutorial, 40 min)
You practise ROW_NUMBER, NTILE, LAG and LEAD on the free bikeshare dataset in a browser editor.
- Mastering Window Functions in SQL for Data Analysis: 1 Hour Mini-Course (video, YouTube, 1 h)
A single video walks through the main functions on analyst-style examples.
- Mastery with SQL - Now Free on YouTube! (video, Mastery with SQL, 30 min)
The short ranking and aggregate window lessons use PostgreSQL and come with exercises.
- SQL Window Functions Tutorial: ROW_NUMBER, RANK, LAG and Running Totals (article, Omebly, 15 min)
It sets ROW_NUMBER, RANK and DENSE_RANK side by side on ties, then covers running totals and filtering with a CTE.
- SQL Window Functions — ROW_NUMBER, RANK, LAG and Practical Examples (article, sqlformat.io, 20 min)
It applies the functions to top-N per group, month-over-month growth with LAG, login streaks and moving averages.
- SQL Window Functions Explained: OVER, PARTITION BY, Ranking, Running Totals & Frames (article, Generalist Programmer, 25 min)
It goes deeper on ROWS BETWEEN frames and gives reusable recipes such as percent of total and period-over-period.
- SQL Practice Window Functions and CTES: SQL 30 Day Challenge Day 28 (video, YouTube, 25 min)
Practice problems that combine window functions, CTEs and joins.
- PostgreSQL: Documentation: Window Functions (built-in list) (reference, PostgreSQL official docs, 15 min)
Use it as the lookup for lag, lead, first_value, last_value and how frames affect them.
- SQL Window Functions Cheat Sheet (2026) (reference, TheQueryLab, 10 min)
A one-page recap table of when to use each function, including common mistakes.