0
0
PostgreSQLquery~10 mins

SUM, AVG, COUNT as window functions in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SUM, AVG, COUNT as window functions
Start with table rows
Apply window function
Partition rows if specified
Order rows if specified
Calculate running SUM, AVG, COUNT
Add results as new columns
Return result set with window function values
Window functions compute values across sets of rows related to the current row, adding results as new columns without collapsing rows.
Execution Sample
PostgreSQL
SELECT dept, emp, salary,
       SUM(salary) OVER (PARTITION BY dept) AS dept_sum,
       AVG(salary) OVER (PARTITION BY dept) AS dept_avg,
       COUNT(*) OVER (PARTITION BY dept) AS dept_count
FROM employees;
Calculates sum, average, and count of salaries per department, showing these as new columns for each employee.
Execution Table
StepRow (dept, emp, salary)SUM(salary) OVER (PARTITION BY dept)AVG(salary) OVER (PARTITION BY dept)COUNT(*) OVER (PARTITION BY dept)
1(Sales, Alice, 5000)1500050003
2(Sales, Bob, 7000)1500050003
3(Sales, Carol, 3000)1500050003
4(HR, Dave, 4000)900045002
5(HR, Eve, 5000)900045002
6(IT, Frank, 6000)600060001
ExitAll rows processed---
💡 All rows processed, window functions computed per partition
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5After Row 6Final
dept_sum (Sales)015000150001500015000150001500015000
dept_avg (Sales)05000500050005000500050005000
dept_count (Sales)03333333
dept_sum (HR)00009000900090009000
dept_avg (HR)00004500450045004500
dept_count (HR)00002222
dept_sum (IT)00000060006000
dept_avg (IT)00000060006000
dept_count (IT)00000011
Key Moments - 3 Insights
Why does the SUM value repeat for each row in the same department?
Because window functions calculate over the partition but do not collapse rows, so each row in the partition shows the same SUM value (see execution_table rows 1-3).
How is AVG calculated without grouping rows?
AVG is computed as SUM divided by COUNT over the partition for each row, shown as a new column without changing the number of rows (see execution_table AVG column).
Why does COUNT(*) show the total number of rows in the partition for every row?
COUNT(*) counts all rows in the partition and repeats this count for each row, unlike aggregate functions that collapse rows (see execution_table COUNT column).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the SUM(salary) for Bob in Sales at step 2?
A15000
B7000
C5000
D3000
💡 Hint
Check the SUM(salary) column for step 2 in the execution_table.
At which step does the COUNT(*) for HR become 2?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the COUNT(*) column and find when HR rows start showing 2.
If a new employee with salary 4000 is added to IT, how would the AVG for IT change in variable_tracker?
AIt would stay 6000
BIt would become 4000
CIt would become 5000
DIt would become 10000
💡 Hint
AVG is total salary divided by count; adding 4000 to 6000 total over 2 rows changes average.
Concept Snapshot
SUM, AVG, COUNT as window functions:
- Use OVER() clause to apply without grouping
- PARTITION BY divides rows into groups
- Functions compute values per partition, repeated per row
- Rows stay same count, new columns added
- Useful for running totals, averages, counts per group
Full Transcript
This lesson shows how SUM, AVG, and COUNT work as window functions in PostgreSQL. Window functions calculate values across a set of rows related to the current row, defined by PARTITION BY. Unlike aggregate functions, they do not reduce the number of rows but add new columns with the computed results. For example, SUM(salary) OVER (PARTITION BY dept) calculates the total salary per department and shows it on every row in that department. AVG and COUNT work similarly, computing average salary and number of employees per department. The execution table traces each row's values step-by-step, showing how the window functions produce repeated results per partition. Key moments clarify why values repeat and how averages are computed without grouping. The quiz tests understanding of these repeated values and how changes affect averages. This helps beginners see how window functions add powerful analytics without losing row detail.