0
0
PostgreSQLquery~10 mins

Practical window function patterns in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Practical window function patterns
Start with table data
Define window function
Partition data (optional)
Order data within partition
Calculate window function value
Return rows with window values
End
Window functions process rows with optional partitioning and ordering, returning values alongside original rows.
Execution Sample
PostgreSQL
SELECT emp_id, dept, salary,
       RANK() OVER (PARTITION BY dept ORDER BY salary DESC) AS rank
FROM employees;
Ranks employees by salary within each department.
Execution Table
Stepemp_iddeptsalaryPartition (dept)Order in PartitionRANK() Result
1101Sales7000Sales11
2102Sales6000Sales22
3103Sales6000Sales32
4104HR8000HR11
5105HR7500HR22
6106HR7000HR33
7107IT9000IT11
8108IT8500IT22
ExitAll rows processed, ranks assigned
💡 All rows processed, ranks assigned
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8Final
emp_id101102103104105106107108
deptSalesSalesSalesHRHRHRITIT
salary70006000600080007500700090008500
RANK()12212312
Key Moments - 3 Insights
Why do employees with the same salary get the same rank?
Because RANK() assigns the same rank to ties, as shown in rows 2 and 3 where salary 6000 gets rank 2 for both.
What happens when the partition changes?
The ranking restarts at 1 for each new department partition, as seen when moving from Sales to HR at row 4.
Why is ordering important inside the partition?
Ordering determines the rank order; without ORDER BY salary DESC, ranks would not reflect salary order.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the rank of emp_id 105 at step 5?
A3
B1
C2
D4
💡 Hint
Check the RANK() Result column at step 5 for emp_id 105.
At which step does the partition change from Sales to HR?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look at the Partition (dept) column to see when it changes.
If we remove ORDER BY salary DESC, what would happen to the ranks?
ARanks would all be 1
BRanks would be assigned randomly within partitions
CRanks would be assigned in insertion order
DRanks would be NULL
💡 Hint
Ordering defines how ranks are assigned; without it, order is not guaranteed.
Concept Snapshot
Window functions compute values across rows related to the current row.
Syntax: function() OVER (PARTITION BY col ORDER BY col)
Partition divides data groups; order defines sequence.
Common functions: RANK(), ROW_NUMBER(), SUM(), AVG().
Results appear alongside original rows without grouping.
Full Transcript
This lesson shows how window functions work in PostgreSQL. We start with a table of employees with departments and salaries. The RANK() function assigns ranks to employees within each department based on salary order. The process partitions data by department, orders salaries descending, then calculates ranks. Equal salaries get the same rank, and ranks restart for each department. The execution table traces each row's rank assignment step-by-step. Key points include understanding partition changes, tie handling, and the importance of ordering. The quiz tests understanding of ranks at specific steps, partition changes, and effects of removing ordering. Window functions let you analyze data across rows without losing row details.