Before continuing: This entry is part of the window functions guide. If you are new to the topic, start with OVER.
PARTITION BY example
SELECT department,
name,
salary,
ROUND(AVG(salary) OVER (
PARTITION BY department
), 2) AS department_average
FROM employees
WHERE department IN ('Marketing', 'Technology')
ORDER BY department, salary DESC;Each employee keeps their row. The average repeats on every row of the same department because it is calculated over that partition.
Check the result with small data
Each salesperson belongs to a team and records an amount.
WITH sales(team, seller, amount) AS (
VALUES ('A','Amy',10), ('A','Liam',20), ('B','Eva',50)
)
SELECT seller, team,
SUM(amount) OVER (PARTITION BY team) AS team_total
FROM sales ORDER BY team, seller;| seller | team | team_total |
|---|---|---|
| Amy | A | 30 |
| Liam | A | 30 |
| Eva | B | 50 |
PARTITION BY computes a total per team without merging several rows into one. Amy and Liam share the total of 30, and both keep their own row.
Check your understanding: How does this result differ from GROUP BY team?
Show answer
GROUP BY team would return one row per team (A with 30 and B with 50) and lose each salesperson. PARTITION BY keeps all three rows and repeats the team total in each one.
How PARTITION BY is used
PARTITION BY decides where the calculation restarts. The ORDER BY inside OVER decides the order within each partition when the function needs one. The ORDER BY at the end of the query is still what sorts the output.
Common mix-ups with PARTITION BY
Expecting PARTITION BY to sort the result
The ordering inside OVER defines how the function works, but on its own it does not guarantee the final order of the rows. Add an ORDER BY at the end of the query when the display order matters.
You must also tell the partition apart from the window frame: some aggregate functions with an ORDER BY inside OVER compute a running total up to the current row, not the full partition total.
Changing the partition changes the comparison group
The partition answers “which rows is this row compared with?”. In a per-customer total, each order can show the sum of all that customer's orders without losing its own id. Adding the year to the partition changes the measure: each order is then compared only with its customer's orders from the same year.
Unlike GROUP BY, a window does not collapse rows into a single summary row. That lets you show detail and measure side by side, but you must be careful when adding up the repeated total again. If a customer has three orders and each row shows a total of 90, adding that column gives 270, which is not a real additional sale.
Test customers with one row and with several, and, if the key allows nulls, the group of unknown keys. Also check the frame when you add an ordering to a window aggregate: you can go from a partition total to a running total. The partition does not sort the report's output. Make the column name say whether the value is a group total, a running total or a position, so readers do not interpret every window the same way.
Technical questions: SQLite: window functions (opens in a new tab).