SQL Docs
SQL referenceCore SQLFunctionsJoinsWindow functions

Window functions

Rank, aggregate, and compare rows within partitions while preserving row-level detail.

Window essentials

Core idea

A window function computes a value over a set of rows related to the current row, without collapsing the result set like a normal aggregate.

Example

SELECT
  month,
  revenue,
  SUM(revenue) OVER (
    ORDER BY month
  ) AS running_total
FROM monthly_revenue;

Common gotchas

  • PARTITION BY changes the group over which the calculation is evaluated
  • ORDER BY inside the window controls the sequence of the calculation
  • when ranking, ties and ordering matter a lot for reproducibility

On this page