---
title: "BigQuery Window Functions: OVER, Frames and LAG Examples"
canonical: "https://www.owox.com/blog/articles/bigquery-window-functions"
updated: "2026-10-29"
---

# BigQuery window functions: OVER, PARTITION BY, frames and LAG, with examples

A window function calculates across a set of related rows and keeps every row, unlike GROUP BY. This guide runs the common ones on one small table and shows the result: running totals, share of total, LAG for day-over-day change, moving averages with a frame, LAST\_VALUE with IGNORE NULLS, and QUALIFY to filter on the result. Each comes with the trap that changes the number without an error.

[Google BigQuery](/blog/topics/bigquery) · Updated October 16, 2026 · [Alyona Samovar](/team/alyona-samovar) · [Vadym Kramarenko](/team/vadym-kramarenko) · 6 min read

![BigQuery window functions: OVER, PARTITION BY, frames and LAG, with examples](https://cdn.owox.com/www/content-marketing-article/bigquery-window-functions/thumbnail/1-l43hsm.png/public)

A window function calculates across a set of related rows, the window, and returns a value **for every row**. `GROUP BY` collapses four days into one total; `SUM(revenue) OVER (...)` keeps the four days and puts the running total next to each.

Every window function has the same shape:

```
function(...) OVER (
  PARTITION BY ...   -- which rows belong together
  ORDER BY ...       -- in what order
  ROWS BETWEEN ...   -- which of them, relative to the current row
)
```

| Family | Functions | Guide |
| --- | --- | --- |
| Aggregate | `SUM`, `AVG`, `COUNT`, `MIN`, `MAX` with `OVER` | this page |
| Navigation | `LAG`, `LEAD`, `FIRST_VALUE`, `LAST_VALUE`, `NTH_VALUE` | this page, and [navigation functions](/blog/articles/bigquery-navigation-functions) |
| Numbering | `ROW_NUMBER`, `RANK`, `DENSE_RANK`, `NTILE`, `PERCENT_RANK` | [numbering functions](/blog/articles/bigquery-numbering-functions) |

**_Note:_** _Originally published in April 2024. Fully rewritten in October 2026: every result below was produced by running the query in BigQuery on 16 October 2026._

## The table used in every example

```
WITH sales AS (
  SELECT DATE '2026-09-01' AS day, 'web' AS channel, 100 AS revenue
  UNION ALL SELECT DATE '2026-09-02', 'web', 140
  UNION ALL SELECT DATE '2026-09-03', 'web', NULL
  UNION ALL SELECT DATE '2026-09-04', 'web', 120
  UNION ALL SELECT DATE '2026-09-01', 'ads', 60
  UNION ALL SELECT DATE '2026-09-02', 'ads', 60
  UNION ALL SELECT DATE '2026-09-03', 'ads', 90
  UNION ALL SELECT DATE '2026-09-04', 'ads', 30
)
```

Two channels over four days. Web has no revenue recorded on 3 September.

## Running total and share of total

```
SELECT
  channel, day, revenue,
  SUM(revenue) OVER (
    PARTITION BY channel ORDER BY day) AS running,
  ROUND(revenue / SUM(revenue) OVER (
    PARTITION BY channel), 3) AS share
FROM sales;
```

| channel | day | revenue | running | share |
| --- | --- | --- | --- | --- |
| web | 2026-09-01 | 100 | 100 | 0.278 |
| web | 2026-09-02 | 140 | 240 | 0.389 |
| web | 2026-09-03 | NULL | 240 | NULL |
| web | 2026-09-04 | 120 | 360 | 0.333 |

The difference is `ORDER BY`. With it, the window is “this row and everything before it”, so `SUM` is a running total. Without it, the window is the whole partition, so `SUM` is the channel total and `revenue / total` is the share. `OVER ()` with nothing inside is the whole result: `COUNT(*) OVER ()` returned 8 on every row.

## The Turning Point

OWOX Data Marts

See your first report built in real time. _15 minutes._

1.  Connect your data warehouse
2.  Pick your metrics
3.  Get a live Google Sheets report

In the time it takes to write a ticket. Then imagine never writing that ticket again.

[Book a Demo](/book-a-call)

We'll use your actual use case

## LAG and LEAD: compare with the previous row

```
SELECT
  channel, day, revenue,
  LAG(revenue) OVER (
    PARTITION BY channel ORDER BY day) AS prev_day,
  revenue - LAG(revenue) OVER (
    PARTITION BY channel ORDER BY day) AS change
FROM sales;
```

| channel | day | revenue | prev\_day | change |
| --- | --- | --- | --- | --- |
| ads | 2026-09-01 | 60 | NULL | NULL |
| ads | 2026-09-02 | 60 | 60 | 0 |
| ads | 2026-09-03 | 90 | 60 | 30 |
| ads | 2026-09-04 | 30 | 90 | \-60 |
| web | 2026-09-04 | 120 | NULL | NULL |

Two things to know:

*   **`LAG` takes the previous row, not the previous day.** If a day is missing from the table, `LAG` compares with whatever came before it. For a strict day-over-day change, join to a calendar first so every day has a row.
*   **A NULL in the previous row gives a NULL change.** Web on 4 September has no change because 3 September has no revenue. `LAG(revenue IGNORE NULLS)` fails with “IGNORE NULLS and RESPECT NULLS are not allowed for analytic function LAG”; use `LAST_VALUE(revenue IGNORE NULLS)` over a frame that ends one row back if you want the last known value.

`LEAD` looks forward instead. `LAG(revenue, 7)` looks seven rows back, and `LAG(revenue, 1, 0)` returns 0 instead of NULL on the first row.

## Moving average: the frame

```
SELECT
  channel, day, revenue,
  ROUND(AVG(revenue) OVER (
    PARTITION BY channel ORDER BY day
    ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 1) AS avg3
FROM sales;
```

| channel | day | revenue | avg3 |
| --- | --- | --- | --- |
| web | 2026-09-01 | 100 | 100 |
| web | 2026-09-02 | 140 | 120 |
| web | 2026-09-03 | NULL | 120 |
| web | 2026-09-04 | 120 | 130 |

`ROWS BETWEEN 2 PRECEDING AND CURRENT ROW` is a three-row window. On the first two days it has fewer rows, so the “three-day” average is really one or two days: filter those out or label them. And `AVG` skips the NULL, so 4 September averages two values, not three.

### The default frame catches people out

When there is an `ORDER BY` and no frame, the frame is `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`. Two consequences, both from our test:

| Expression | Result on ads, 1 September |
| --- | --- |
| `LAST_VALUE(revenue) OVER (PARTITION BY channel ORDER BY day)` | 60, the current row |
| `LAST_VALUE(revenue) OVER (... ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)` | 90, the last row |
| `SUM(revenue) OVER (PARTITION BY channel ORDER BY revenue)` on a 60 | 120, both 60s at once |

`LAST_VALUE` without a full frame returns the current row, because the window ends there. And `RANGE` treats rows with the same `ORDER BY` value as one step, so the running total jumps by both tied rows together. Write the frame out whenever the answer depends on it.

## The last known value: LAST\_VALUE IGNORE NULLS

```
SELECT
  channel, day, revenue,
  LAST_VALUE(revenue IGNORE NULLS) OVER (
    PARTITION BY channel ORDER BY day) AS last_known
FROM sales;
```

For web on 3 September this returned 140, the value from the day before, instead of NULL. That fills gaps in a series such as a price list or an exchange rate. Here the default frame is what you want: everything up to the current row.

## Filter on a window function: QUALIFY

A window function cannot go in `WHERE`, because `WHERE` runs before it. `QUALIFY` runs after:

```
SELECT channel, day, revenue
FROM sales
QUALIFY ROW_NUMBER() OVER (
  PARTITION BY channel ORDER BY revenue DESC) <= 2;
```

This returned the two best days per channel: ads on 3 and 1 September, web on 2 and 4 September. The window is still computed over all rows; `QUALIFY` only decides which to keep. The order of clauses is in [WHERE, HAVING and QUALIFY](/blog/articles/bigquery-sql-where-vs-having-vs-qualify).

## Window function or GROUP BY

*   **One row per group:** `GROUP BY` with an aggregate. See [aggregate functions](/blog/articles/bigquery-aggregate-functions).
*   **Every row, with a group value beside it:** a window function.
*   **Both:** aggregate first in a subquery or CTE, then apply the window to the result. The [CTE guide](/blog/articles/bigquery-cte-efficient-queries) shows the pattern.

On a large table, a window over a partition with millions of rows can fail with “Resources exceeded”, because a partition is processed in one place. Partition by something finer, or filter first.

## Where window functions matter in reporting

Day-over-day change, running totals and share of channel are the numbers reports are made of, and they are easy to get subtly wrong: a missing day, a default frame, a tie. Write them once, check them once, and let every report read the same result. In [OWOX Data Marts](https://www.owox.com/app-signup) that query is published as a Data Mart, run as written in your own BigQuery project, and delivered to sheets and [dashboards](/product/dashboards). OWOX Data Marts does not transform the data. All function families are mapped in the [BigQuery functions overview](/blog/articles/google-bigquery-functions-overview).

## FAQ

## Frequently Asked Questions

What are the best practices for window functions in BigQuery?

Write the frame out whenever the result depends on it, because the default frame ends at the current row. Break ties in ORDER BY with a second column. Filter before the window where you can, and use QUALIFY to filter on its result. Partition finely enough that no single partition holds millions of rows.

Which window functions does BigQuery have?

Aggregate functions used with OVER (SUM, AVG, COUNT, MIN, MAX), navigation functions (LAG, LEAD, FIRST\_VALUE, LAST\_VALUE, NTH\_VALUE) and numbering functions (ROW\_NUMBER, RANK, DENSE\_RANK, PERCENT\_RANK, CUME\_DIST, NTILE).

What is the difference between GROUP BY and a window function?

GROUP BY returns one row per group. A window function returns every row and adds a value calculated over related rows, such as a running total or the channel total beside each day.

What is a practical example of a window function in BigQuery?

Day-over-day change: revenue - LAG(revenue) OVER (PARTITION BY channel ORDER BY day). LAG takes the previous row, not the previous calendar day, so join to a calendar first if days can be missing.

How does the OVER clause work in BigQuery?

PARTITION BY groups the rows, ORDER BY sorts them, and the frame (ROWS BETWEEN ...) picks which rows around the current one are used. With ORDER BY and no frame, the default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which makes SUM a running total.

What are window functions in BigQuery?

Functions that calculate across a set of related rows, the window, and return a value for every row instead of collapsing them. They are written with an OVER clause.

## Who wrote this

![Alyona Samovar](https://cdn.owox.com/www/webflow/6799099d337aa8a997b03b56_Alyona-Samovar.png/public)

[Alyona Samovar](/team/alyona-samovar) Senior Digital Analyst

Alyona Samovar is a Senior Digital Analyst who spent years at OWOX helping enterprise clients design analytics systems, build BigQuery reporting pipelines, and optimize marketing measurement. She specializes in SQL-based analytics, data modeling, and turning raw marketing data into actionable insights. Alyona writes about practical analytics workflows, BigQuery best practices, and data-driven decision-making.

![Vadym Kramarenko](https://cdn.owox.com/www/webflow/6798e703afdf787334941cde_Vadym-Kramarenko.png/public)

[Vadym Kramarenko](/team/vadym-kramarenko) Growth Marketing Manager

Vadym Kramarenko is a Growth Marketing Manager at OWOX, where he drives user acquisition and product-led growth strategies. He hosts the OWOX podcast, interviewing analytics professionals about data-driven marketing, attribution, and reporting best practices. Vadym specializes in turning complex analytics concepts into practical, actionable marketing frameworks.

[LinkedIn](https://www.linkedin.com/in/vadim-kramarenko/) · [All articles](/team/vadym-kramarenko)

[Google BigQuery](/blog/topics/bigquery)

## Related articles

[Google BigQuery · How to Load Data into BigQuery: Every Method and When to Use It · October 6, 2026](/blog/articles/loading-data-into-bigquery)

[Google BigQuery · Is BigQuery a Data Warehouse or a Data Lake? A Marketing Guide · October 6, 2026](/blog/articles/why-gbq-perfect-data-lake)

[Google BigQuery · BigQuery Aggregate Functions: COUNT, SUM, STRING\_AGG, ARRAY\_AGG and More · October 16, 2026](/blog/articles/bigquery-aggregate-functions)

[See all articles →](/blog/articles)

## Links

Pages this page links to, on this site and on docs.owox.com. Where the page has a Markdown twin, its address follows the link.

- [Google BigQuery](https://www.owox.com/blog/topics/bigquery) — /blog/topics/bigquery.md
- [Alyona Samovar](https://www.owox.com/team/alyona-samovar) — /team/alyona-samovar.md
- [Vadym Kramarenko](https://www.owox.com/team/vadym-kramarenko) — /team/vadym-kramarenko.md
- [navigation functions](https://www.owox.com/blog/articles/bigquery-navigation-functions) — /blog/articles/bigquery-navigation-functions.md
- [numbering functions](https://www.owox.com/blog/articles/bigquery-numbering-functions) — /blog/articles/bigquery-numbering-functions.md
- [Book a Demo](https://www.owox.com/book-a-call) — /book-a-call.md
- [WHERE, HAVING and QUALIFY](https://www.owox.com/blog/articles/bigquery-sql-where-vs-having-vs-qualify) — /blog/articles/bigquery-sql-where-vs-having-vs-qualify.md
- [aggregate functions](https://www.owox.com/blog/articles/bigquery-aggregate-functions) — /blog/articles/bigquery-aggregate-functions.md
- [CTE guide](https://www.owox.com/blog/articles/bigquery-cte-efficient-queries) — /blog/articles/bigquery-cte-efficient-queries.md
- [OWOX Data Marts](https://www.owox.com/app-signup)
- [dashboards](https://www.owox.com/product/dashboards) — /product/dashboards.md
- [BigQuery functions overview](https://www.owox.com/blog/articles/google-bigquery-functions-overview) — /blog/articles/google-bigquery-functions-overview.md
- [Google BigQuery · How to Load Data into BigQuery: Every Method and When to Use It · October 6, 2026](https://www.owox.com/blog/articles/loading-data-into-bigquery) — /blog/articles/loading-data-into-bigquery.md
- [Google BigQuery · Is BigQuery a Data Warehouse or a Data Lake? A Marketing Guide · October 6, 2026](https://www.owox.com/blog/articles/why-gbq-perfect-data-lake) — /blog/articles/why-gbq-perfect-data-lake.md
- [See all articles →](https://www.owox.com/blog/articles) — /blog/articles.md
