Account
Categories

Window Function


A Window Function performs calculations across multiple rows while keeping the individual row data visible. Unlike GROUP BY, it does not merge rows into a single result but applies numeric calculations within a defined window(a subset of rows).

Note:
It is used for aggregation (SUM, AVG), ranking (RANK, ROW_NUMBER), and offset calculations (LAG, LEAD) while maintaining row-wise output.

Syntax:

SELECT column_name,
       AGGREGATE_FUNCTION(column_name) OVER (
           PARTITION BY column_name
           ORDER BY column_name
           ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
       ) AS alias_name
   FROM table_name;