PARTITION BY in BigQuery: Syntax, Use, and Examples

How-to basics · Updated November 22, 2026

PARTITION BY in BigQuery is a clause used inside window functions (also called analytic functions) to split rows into groups, or partitions, before the function calculates a result for each row. It does not collapse rows like GROUP BY; instead, it lets you compute running totals, rankings, moving averages, and other per-group values while keeping every original row in the output.

What PARTITION BY Actually Does

In standard SQL, a window function looks like this: FUNCTION(column) OVER (PARTITION BY column ORDER BY column). The PARTITION BY part tells BigQuery which column values define the groups. For example, if you have sales data with a region column, PARTITION BY region makes the function calculate separately for each region. Rows with the same region value belong to the same partition; rows with different region values are independent.

Unlike GROUP BY, which returns one row per group, PARTITION BY returns all rows. Each row gets a result based on its partition. This is why window functions are often called “analytic” functions—they analyze across a set of rows without reducing the result set.

How to Use PARTITION BY: Syntax and a Real Example

Here is the basic syntax for a window function with PARTITION BY:

SELECT
  employee_id,
  department,
  salary,
  AVG(salary) OVER (PARTITION BY department) AS dept_avg_salary
FROM employee_data;

This query returns every employee, plus the average salary of their department. The AVG is calculated per department because of PARTITION BY department. Without PARTITION BY, the average would be over the entire table.

Another common use is ranking within groups:

SELECT
  product,
  category,
  sales,
  RANK() OVER (PARTITION BY category ORDER BY sales DESC) AS rank_in_category
FROM product_sales;

Here, RANK() ranks products within each category. The ORDER BY inside the OVER clause determines the order within each partition. Note that ORDER BY is optional for some functions (like SUM without a running total), but required for ranking functions.

PARTITION BY vs. GROUP BY: Key Differences

Many people confuse these two. The main difference is the number of rows returned:

  • GROUP BY collapses rows: one output row per group. You can only select the grouping columns and aggregate functions.
  • PARTITION BY preserves rows: every input row appears in the output, with an added calculated column.

For example, if you have 100 sales records across 5 regions, GROUP BY region returns 5 rows (one per region). PARTITION BY region in a window function returns 100 rows, each with the region’s total or average attached.

You can also use both together: first aggregate with GROUP BY, then apply a window function with PARTITION BY on the aggregated result. This is common for calculating shares or percentages.

Common Functions That Use PARTITION BY

BigQuery supports many window functions that work with PARTITION BY. The most frequent ones include:

  • Ranking: ROW_NUMBER(), RANK(), DENSE_RANK(), NTILE() – assign numbers or ranks within each partition.
  • Aggregate: SUM(), AVG(), COUNT(), MIN(), MAX() – compute a value per partition, often with a running total if ORDER BY is added.
  • Navigation: LAG(), LEAD() – access values from previous or next rows within the partition.
  • Distribution: PERCENT_RANK(), CUME_DIST() – relative standing within a partition.

Each function has its own rules about whether ORDER BY is required. For example, ROW_NUMBER() requires an ORDER BY to define a deterministic order; SUM() without ORDER BY gives a static total per partition, while with ORDER BY it gives a cumulative sum.

How to Choose What to Partition By

Choosing the right column for PARTITION BY depends on the question you are answering. Here are practical guidelines:

  • For per-group comparisons, partition by the categorical column that defines the group (e.g., department, region, product category).
  • For time-series analysis, partition by a user or entity ID and order by a timestamp. This gives you running totals or month-over-month changes per user.
  • For deduplication, partition by the columns that define a duplicate (e.g., customer_id, order_id) and use ROW_NUMBER() to keep only the first row.
  • For moving averages, partition by the entity and order by date; then use a frame clause like ROWS BETWEEN 6 PRECEDING AND CURRENT ROW.

You can partition by multiple columns: PARTITION BY year, month creates a partition for each unique combination. This is useful for nested groupings, like sales per product per month.

Common Mistakes and Misconceptions

Here are frequent errors people make with PARTITION BY in BigQuery:

  • Forgetting ORDER BY – For ranking and running totals, ORDER BY is essential. Without it, you may get unpredictable results or an error for functions that require it.
  • Confusing PARTITION BY with table partitioning – BigQuery also has physical table partitioning (by date or integer range) for storage and query cost. That is a different feature; the SQL clause PARTITION BY is only for window functions.
  • Using GROUP BY instead of PARTITION BY – If you need all rows, use a window function. If you need only summary rows, use GROUP BY.
  • Assuming partitions are sorted – PARTITION BY groups rows but does not sort them. You must specify ORDER BY inside the OVER clause to get a meaningful order.
  • Ignoring NULLs – By default, BigQuery treats NULLs as a single group in PARTITION BY. If you want to exclude NULLs, filter them out in a WHERE clause.

Performance and Cost Considerations

Using PARTITION BY in a query does not directly affect billing, but the underlying data scanned does. BigQuery charges per query based on the bytes read. If you have a large table, the window function will process all rows in the partitions you reference. To reduce cost, consider:

  • Filtering with WHERE before the window function to limit rows.
  • Using table partitioning and clustering to prune data (this is the physical partitioning feature, not the SQL clause).
  • Avoiding unnecessary ORDER BY inside OVER if you don’t need a running total, as sorting can be expensive.

BigQuery’s query engine is highly optimized for window functions, so they are generally fast even on large datasets, but scanning less data always helps.

Frequently Asked Questions

Can I use PARTITION BY without a window function?
No. PARTITION BY is only valid inside the OVER clause of a window function. If you want to group rows for aggregation, use GROUP BY instead.

What is the difference between PARTITION BY and GROUP BY in BigQuery?
GROUP BY reduces the number of rows to one per group and is used with aggregate functions like SUM or COUNT. PARTITION BY keeps all rows and calculates a value for each row based on its group. You can combine them, but they serve different purposes.

Does PARTITION BY require ORDER BY?
Not always. For aggregate functions like SUM or AVG, ORDER BY is optional—without it, you get a static value per partition. For ranking functions like RANK or ROW_NUMBER, ORDER BY is required.

Can I partition by multiple columns?
Yes. Just list them separated by commas: PARTITION BY department, year. This creates a partition for each unique combination of those columns.

How do I remove duplicates with PARTITION BY?
Use ROW_NUMBER() OVER (PARTITION BY duplicate_key_columns ORDER BY some_column) and then filter to where the row number equals 1. This keeps one row per partition.

Is PARTITION BY supported in standard SQL or legacy SQL?
Window functions with PARTITION BY are supported in BigQuery’s standard SQL, which is the default. Legacy SQL has limited support and is not recommended for new queries.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.