Window functions

Rank, accumulate and compare rows without collapsing them.

Notes

A window function computes a value across related rows while keeping every row in the output.

PARTITION BY defines the groups; ORDER BY inside the window defines the sequence.

Key points

  • RANK keeps ties; ROW_NUMBER does not.
  • SUM() OVER (... ORDER BY ...) gives a running total.
  • LAG looks at the previous row.