Lesson 2.2

Aggregation and window functions

Group-by collapses rows into one per group.

8mIntermediate13.2k students

Overview

Aggregate without collapsing the rows

Group-by collapses rows into one per group. A window function computes across a set of rows while keeping every row — which is how you show each order alongside the customer total on the same line.

Partitioning defines the window and ordering defines the sequence within it. Together they give you ranks, row numbers, running totals, and comparisons to the previous or next row.

The classic use is picking the latest row per group: number rows within each partition by date descending and keep the first. It replaces a correlated subquery that is both slower and harder to read.

In this lesson you will:

  • Aggregate without losing row detail
  • Rank and number rows within a group
  • Compute running totals and comparisons

Resources

Previous Lesson
Next Lesson
Aggregation and window functions — PostgreSQL for Application Developers — Vertex