PARTITION BY
PARTITION BY divides rows into independent groups for a window function or an output operation, so calculations reset for each group without collapsing rows the way GROUP BY does.
Overview
PARTITION BY is the sub-clause inside a window function's OVER (...) that splits the input into independent groups. Unlike GROUP BY, which collapses each group into a single output row, PARTITION BY keeps every input row intact — the window function is simply computed independently within each partition.
Copy code
SELECT
region,
salesperson,
revenue,
SUM(revenue) OVER (PARTITION BY region) AS region_total,
revenue / SUM(revenue) OVER (PARTITION BY region) AS pct_of_region
FROM sales;
Here every row keeps its own salesperson and revenue, while region_total restarts for each region.
PARTITION BY vs GROUP BY
GROUP BY regionreturns one row per region with aggregated values.PARTITION BY region(insideOVER) returns one row per input row, annotated with a per-region aggregate.
You can combine both: GROUP BY to aggregate a base query, then window functions with their own PARTITION BY over the aggregated rows.
DuckDB specifics
Beyond window functions, DuckDB reuses the same PARTITION BY keyword for the COPY ... TO statement, which writes Hive-partitioned files to disk or object storage — a genuinely different but related concept (physical partitioning rather than logical grouping):
Copy code
COPY (SELECT * FROM sales)
TO 's3://my-bucket/sales'
(FORMAT parquet, PARTITION_BY (region, year));
This produces a directory structure like sales/region=EMEA/year=2026/*.parquet, which query engines (including DuckDB itself via read_parquet with hive partitioning enabled) can prune on read.
Related terms
The OVER clause turns an aggregate or ranking function into a window function by defining the partition, order, and frame it operates on, without collapsing the result set.
row group →A row group is a fundamental storage concept in columnar databases like DuckDB that represents a horizontal partition of data containing a fixed number of…
window functions →Window functions allow you to perform calculations across a set of table rows that are somehow related to the current row, similar to aggregate functions,…
ROW_NUMBER →ROW_NUMBER() is a SQL window function that assigns a unique, sequential integer to each row within a partition based on a specified order, with no ties.
Partition pruning →Partition pruning is a query optimization that skips reading entire partitions of a dataset when a query's filters make it impossible for those partitions to contain matching rows.
GROUP BY clause →GROUP BY organizes rows into groups for aggregate calculations like SUM, COUNT, and AVG. Learn single and multi-column grouping, HAVING filters, and GROUP BY ALL.
FAQS
GROUP BY collapses rows into one aggregated row per group. PARTITION BY, used inside a window function's OVER clause, keeps every original row and computes the aggregate independently within each group, attaching the result alongside the row's other columns.
Yes. PARTITION BY alone computes the window function (e.g., a sum or count) over the whole partition, treating every row in the partition equally, since there is no ordering to define a frame.
