WINDOW FUNCTIONS NOTES
1. MAIN PURPOSE OF WINDOW FUNCTIONS
----------------------------------
Window functions perform calculations
WITHOUT collapsing rows.
GROUP BY → collapses rows
WINDOW FUNCTIONS → keep rows intact
2. ORDER BY IN WINDOW FUNCTIONS
--------------------------------
Purpose:
Controls calculation order/ranking order.
Meaning:
“In what order should calculation happen?”
Example:
RANK() OVER(ORDER BY sales DESC)
Meaning:
Rank highest sales first.
3. PARTITION BY IN WINDOW FUNCTIONS
-----------------------------------
Purpose:
Creates separate groups/windows for calculation.
Meaning:
“Inside which groups should calculation happen separately?”
Example:
RANK() OVER(
PARTITION BY region
ORDER BY sales DESC
)
Meaning:
Ranking restarts separately inside each region.
4. SIMPLE DIFFERENCE
--------------------
ORDER BY
→ calculation sequence/order
PARTITION BY
→ separate groups/windows
5. EASY MEMORY TRICK
--------------------
PARTITION BY
= divide into groups
ORDER BY
= arrange order inside groups
6. REAL-LIFE EXAMPLE
--------------------
Classroom Ranking:
ORDER BY marks DESC
→ highest marks gets rank 1
PARTITION BY section
→ each section gets separate ranking