Solve analytical questions with SQL window functions

8 steps · about 4 h 5 min · Intermediate

  1. 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.

  2. Define partitions, order and frames (reference, PostgreSQL Global Development Group, 35 min)

    The OVER clause controls which related rows each calculation can see.

  3. 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.

  4. 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.

  5. Compare portable window syntax (reference, SQLite Consortium, 30 min)

    A second implementation highlights the standard concepts and the places dialects differ.

  6. Calculate offsets and running values (guide, DuckDB Foundation, 35 min)

    Use lag, lead and framed aggregates to compare events and build cumulative measures.

  7. 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.

  8. Practise analytical queries on connected tables (interactive, PGExercises, 50 min)

    Solve progressively harder questions and inspect the supplied answers only after attempting each query.