Skip to content

SQL Window Functions

Pro

A deep dive into OVER, PARTITION BY, all four ranking functions, aggregate windows, LAG/LEAD, ROWS-vs-RANGE frames, multi-dialect quirks, and performance.

8 modules · Module 1 is free; Modules 2+ require Pro.

View the full course

What this course covers

A module-by-module concept outline. Open the course to learn each topic with animated explanations, in-browser code, practice challenges, and a knowledge check.

Module 1. The Mental Model: OVER, PARTITION BY, ORDER BY

Free
Topics
Why window functions existThe OVER() clause anatomyPARTITION BY — slicing the dataORDER BY in OVER — sequencing the slice
Sections
  1. 1Why Window Functions Exist
  2. 2Anatomy of OVER() — The Three Dials
  3. 3PARTITION BY — Many Windows, One Query
  4. 4ORDER BY in OVER — Sequence Matters
  5. 5Where Windows Run in the Pipeline
  6. 6NULLs in ORDER BY Inside OVER

Module 2. Ranking Functions: ROW_NUMBER, RANK, DENSE_RANK, NTILE

Pro
Topics
ROW_NUMBER — unique sequential ranksRANK — competition ranks with gapsDENSE_RANK — tiers without gapsNTILE — equal-sized buckets
Sections
  1. 1ROW_NUMBER — The Default Rank
  2. 2RANK vs DENSE_RANK — Handling Ties
  3. 3NTILE — Bucketing into Quartiles & Deciles
  4. 4Top-N Per Group — The Interview Pattern
  5. 5ROW_NUMBER for Dedup — The One Canonical Recipe

Module 3. Aggregate Window Functions

Pro
Topics
SUM/AVG/COUNT/MIN/MAX with OVERRunning totalsGrand totals & percent-of-totalMultiple windows in one query
Sections
  1. 1Aggregate-as-Window — The Mental Switch
  2. 2Running Totals & Cumulative Sums
  3. 3Percent of Total & Share Analysis
  4. 4Combining Multiple Windows in One Query
  5. 5FILTER Previews & the COUNT(DISTINCT) Caveat
  6. 6Mixing Aggregates with Ranking — A Preview

Module 4. LAG, LEAD & Comparing Rows

Pro
Topics
LAG — look at the previous rowLEAD — look at the next rowPeriod-over-period growthGap & sequence analysis
Sections
  1. 1LAG — Looking Backward
  2. 2LEAD — Looking Forward
  3. 3Period-Over-Period Growth
  4. 4Gap Analysis & Sequence Patterns
  5. 5LAG With Offsets & The Forward-Fill Trap
  6. 6Production Pattern — Cohort Retention

Module 5. Frame Clauses: ROWS vs RANGE

Pro
Topics
Default frames — and why they biteROWS — physical row countingRANGE — logical value countingMoving averages and centered windows
Sections
  1. 1The Default Frame & Why It Bites
  2. 2ROWS — Counting Physical Positions
  3. 3RANGE — Counting Logical Values
  4. 4Moving Averages & Centered Frames
  5. 5EXCLUDE Clauses & Numeric RANGE BETWEEN
  6. 6The Five Mistakes Reviewers Catch in PRs

Module 6. Multi-Dialect Reality: SQLite vs PostgreSQL

Pro
Topics
The SQL standard vs vendor realityPostgreSQL extensions: FILTER, IGNORE NULLSRANGE BETWEEN INTERVAL — only in some dialectsQUALIFY (BigQuery / Snowflake) and workarounds
Sections
  1. 1Why Dialects Diverge
  2. 2FILTER & IGNORE NULLS — Postgres Superpowers
  3. 3NULLS FIRST / NULLS LAST — The Silent Cross-Dialect Trap
  4. 4RANGE Intervals — A PostgreSQL-Only Trick
  5. 5QUALIFY & the WHERE-on-Windows Workaround
  6. 6The Portability Decision Tree

Module 7. FIRST_VALUE, LAST_VALUE, NTH_VALUE & FILTER

Pro
Topics
FIRST_VALUE & LAST_VALUE — and the LAST_VALUE trapNTH_VALUE — sampling the Nth rowPERCENT_RANK & CUME_DISTConditional aggregation inside windows
Sections
  1. 1FIRST_VALUE — Anchoring to the Start
  2. 2LAST_VALUE — And the Frame Trap
  3. 3NTH_VALUE, PERCENT_RANK, CUME_DIST
  4. 4PERCENTILE_CONT & PERCENTILE_DISC for Median & p95
  5. 5Conditional Aggregation Inside Windows
  6. 6Sessionization — The Itzik Ben-Gan Recipe
  7. 7Production Pattern — Cohort Retention Heatmap

Module 8. Performance & Anti-Patterns

Pro
Topics
Windows vs self-joins — when each winsCommon anti-patterns and how to spot themEXPLAIN reading for window queriesRefactoring slow analytics queries
Sections
  1. 1Windows vs Self-Joins — When Each Wins
  2. 2Anti-Patterns You Will See in Production
  3. 3Reading EXPLAIN for Window Queries
  4. 4Refactoring Slow Analytics Queries
  5. 5Capstone — Every Module in One Query

Ready to start SQL Window Functions?

Module 1 is free. Unlock the full course with Pro.

Go to the course