Windowed
Aggregates
Master OVER(PARTITION BY ...) to compute aggregates across groups of rows — without collapsing your result set.
What Is a Window Function?
A window function performs a calculation across a set of rows that are somehow related to the current row. Unlike a regular aggregate (GROUP BY), it does not collapse the rows into one — every original row stays in the output, each enriched with the computed value.
GROUP BY vs Window Functions
Why Does This Matter?
The Anatomy of OVER()
SELECT column, SUM(amount) OVER ( PARTITION BY group_col ORDER BY sort_col -- optional frame clause ) AS window_result FROM table_name;
PARTITION BY Deep Dive
PARTITION BY divides the result set into partitions — groups of rows that share the same value(s) in the specified column(s). The window function is computed independently for each partition.
GROUP BY vs PARTITION BY
| GROUP BY | PARTITION BY | |
|---|---|---|
| Output rows | One per group | All original rows |
| Used in | Own clause (after WHERE) | SELECT (OVER clause) |
| Non-agg columns | Must be in GROUP BY list | Any column is fine |
| Ordering support | Separate ORDER BY | ORDER BY inside OVER |
| Running totals | Requires subquery | Built-in |
How to Apply Window Aggregates
Example 1: Department Totals
SELECT name, dept, salary, SUM(salary) OVER ( PARTITION BY dept ) AS dept_total FROM employees;
| name | dept | salary | dept_total |
|---|---|---|---|
| Alice | Sales | 5000 | 12000 |
| Bob | Sales | 7000 | 12000 |
| Carol | Eng | 6000 | 14000 |
| Dave | Eng | 8000 | 14000 |
Example 2: Percentage of Group
SELECT name, dept, salary, ROUND( 100.0 * salary / SUM(salary) OVER (PARTITION BY dept), 1 ) AS pct_of_dept FROM employees;
| name | dept | salary | pct_of_dept |
|---|---|---|---|
| Alice | Sales | 5000 | 41.7% |
| Bob | Sales | 7000 | 58.3% |
| Carol | Eng | 6000 | 42.9% |
| Dave | Eng | 8000 | 57.1% |
Example 3: Running Total
SELECT order_date, amount, SUM(amount) OVER ( ORDER BY order_date ) AS running_total FROM orders;
| order_date | amount | running_total |
|---|---|---|
| Jan 1 | 100 | 100 |
| Jan 3 | 250 | 350 |
| Jan 5 | 150 | 500 |
| Jan 8 | 300 | 800 |
Combining PARTITION BY + ORDER BY
SELECT dept, name, salary, SUM(salary) OVER ( PARTITION BY dept ORDER BY salary ) AS running_in_dept FROM employees;
| dept | name | salary | running_in_dept |
|---|---|---|---|
| Eng | Carol | 6000 | 6000 |
| Eng | Dave | 8000 | 14000 |
| Sales | Alice | 5000 | 5000 |
| Sales | Bob | 7000 | 12000 |
Other Aggregate Functions in OVER
SELECT name, dept, salary, AVG(salary) OVER (PARTITION BY dept) AS avg_sal, COUNT(*) OVER (PARTITION BY dept) AS dept_size, MAX(salary) OVER (PARTITION BY dept) AS top_sal FROM employees;
OVER() with No Partition
If you write OVER() with empty parentheses — no PARTITION BY, no ORDER BY — the window covers the entire result set. Every row gets the same grand total.
SELECT name, salary, SUM(salary) OVER () AS grand_total FROM employees;
| name | salary | grand_total |
|---|---|---|
| Alice | 5000 | 26000 |
| Bob | 7000 | 26000 |
| Carol | 6000 | 26000 |
| Dave | 8000 | 26000 |
Common Mistakes
Cheat Sheet: Window Aggregate Patterns
| Pattern | OVER Clause | Result |
|---|---|---|
| Grand total | \(\texttt{OVER}()\) | Same value on every row |
| Group total | \(\texttt{OVER}(\texttt{PARTITION BY col})\) | Same value within each group |
| Running total | \(\texttt{OVER}(\texttt{ORDER BY col})\) | Cumulative across whole table |
| Running total per group | \(\texttt{OVER}(\texttt{PARTITION BY g ORDER BY c})\) | Cumulative, resets per group |
| Percentage of group | \(\texttt{val} / \texttt{SUM}(\texttt{val}) \texttt{ OVER } (\texttt{PARTITION BY g})\) | Each row's share |
Question 1 of 5
What does PARTITION BY do inside an OVER clause?
Question 2 of 5
Given this query, what does dept_avg represent?
SELECT name, dept, salary,
AVG(salary) OVER (PARTITION BY dept)
AS dept_avg
FROM employees;
Question 3 of 5
What happens when you write SUM(amount) OVER (ORDER BY order_date) without PARTITION BY?
Question 4 of 5
A developer wants the total salary per department next to each row, but accidentally writes:
SELECT name, dept, salary,
SUM(salary) OVER (
PARTITION BY dept
ORDER BY salary
) AS dept_total
FROM employees;
What is wrong with this query?
Question 5 of 5
How would you compute each employee's salary as a percentage of the grand total (across all departments)?