Solve analytical questions with SQL window functions
8 steps · about 4 h 5 min · Intermediate
- See how a window preserves row detail (guide, PostgreSQL Global Development Group, 25 min)
Contrast window calculations with grouped aggregation before writing more complex expressions.
- Define partitions, order and frames (reference, PostgreSQL Global Development Group, 35 min)
The OVER clause controls which related rows each calculation can see.
- Rank rows with ties deliberately (reference, PostgreSQL Global Development Group, 30 min)
Choose row_number, rank or dense_rank according to how equal values should behave.
- Build the grouped baseline first (tutorial, PostgreSQL Global Development Group, 20 min)
A grouped answer makes it clearer when a window is necessary to retain individual records.
- Compare portable window syntax (reference, SQLite Consortium, 30 min)
A second implementation highlights the standard concepts and the places dialects differ.
- Calculate offsets and running values (guide, DuckDB Foundation, 35 min)
Use lag, lead and framed aggregates to compare events and build cumulative measures.
- Filter results after window evaluation (guide, DuckDB Foundation, 20 min)
QUALIFY expresses top-per-group filters without an extra nesting layer where the dialect supports it.
- Practise analytical queries on connected tables (interactive, PGExercises, 50 min)
Solve progressively harder questions and inspect the supplied answers only after attempting each query.