Window functions in BigQuery

Window functions are one of the most important SQL features in BigQuery. They allow you to perform calculations across related rows without collapsing rows (unlike GROUP BY).

Basic Syntax (Core Pattern)

window_function(expression)
OVER (
  PARTITION BY column1, column2
  ORDER BY column3
  ROWS | RANGE frame_definition
)

What Each Part Does

If PARTITION BY is not used:

Example: Without Window Function

a) Using GROUP BY (rows collapse)

SQL Query

SELECT
  department,
  AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

Result One row per department

b) Same Logic Using Window Function

SQL Query

SELECT
  employee_id,
  department,
  salary,
  AVG(salary) OVER (PARTITION BY department) AS avg_dept_salary
FROM employees;

Key Difference

Use Case: Comparisons, percentages, rankings, running totals

Important Notes on Window Frames

Equivalent Queries Example

SQL Query

SELECT book,
       LAST_VALUE(book) OVER (ORDER BY year)
FROM Library;

is equivalent to

SQL Query

SELECT book,
       LAST_VALUE(book)
       OVER (
         ORDER BY year
         RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
       )
FROM Library;

Example: Running Total Using Window Function

SQL Query

SELECT
  order_date,
  sales_amount,
  SUM(sales_amount) OVER (
    ORDER BY order_date
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  ) AS running_sales
FROM sales;

Frame Explained

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW