Window Functions in SQL

Window functions perform calculations across a set of table rows that are somehow related to the current row. This set of rows is called a "window". Window functions are similar to aggregate functions, but they do not cause rows to be collapsed into a single output row. Instead, they return a value for each row from the underlying query.

Window functions can be used in the SELECT list and in the ORDER BY clause.

Syntax

The general syntax for a window function is:

window_function ( expression )
    OVER
    (
        [ partition_by_clause ]
        [ order_by_clause ]
        [ frame_clause ]
    )

Types of Window Functions

1. Ranking Window Functions

These functions assign a rank to each row within its partition based on the ordering specified in the ORDER BY clause.

Example: Using RANK()

Find the rank of each employee within their department based on salary.

SELECT
    employee_name,
    department,
    salary,
    RANK() OVER ( PARTITION BY department ORDER BY salary DESC ) AS salary_rank
FROM employees;

2. Aggregate Window Functions

These are standard aggregate functions (like SUM, AVG, COUNT, MIN, MAX) used as window functions. They compute an aggregate value for each row based on the window defined for that row.

Example: Using SUM()

Calculate the cumulative sum of sales for each product category over time.

SELECT
    sale_date,
    product_category,
    sale_amount,
    SUM(sale_amount) OVER ( PARTITION BY product_category ORDER BY sale_date ) AS cumulative_sales
FROM sales_data;

3. Value Window Functions (Navigation Functions)

These functions access data from other rows in the same result set without the need for self-joins. They are useful for comparing values between rows.

Example: Using LEAD()

Find the difference in salary between an employee and the next highest-paid employee in the same department.

SELECT
    employee_name,
    department,
    salary,
    LEAD(salary, 1, 0) OVER ( PARTITION BY department ORDER BY salary DESC ) AS next_highest_salary
FROM employees;

The FRAME Clause

The FRAME clause specifies which rows within a partition are included in the "frame" for the current row. It refines the window beyond just the partition and order.

Common frame units include:

Common frame boundaries:

If the frame_clause is omitted, the default behavior depends on whether an ORDER BY clause is present in the OVER clause:

Tip: Understanding the FRAME clause is key to precisely controlling the scope of calculations for window functions, especially when dealing with cumulative sums or moving averages.

Common Use Cases

Window functions significantly enhance SQL's analytical capabilities, allowing complex calculations that previously required self-joins or procedural code to be expressed concisely and efficiently.