SQL 10 : Window Functions
D
Qubits of DPK
March 31, 2026
Core DBMS
Topics Covered
- CTEs (Common Table Expressions)
- Window Functions — Problem Statement
- Window Functions Intro + OVER()
- OVER + PARTITION BY (Deep Dive)
- ROW_NUMBER()
- RANK() & DENSE_RANK()
- LEAD() and LAG()
Topic 1 — CTEs (Common Table Expressions)
What is a CTE?
A named temporary result set that exists only for the duration of a single query. Defined using the WITH clause.
Basic Syntax
sql
QUBITS OF DPK
Multiple CTEs
sql
QUBITS OF DPK
When You MUST Use CTE
sql
QUBITS OF DPK
CTE vs Subquery vs View
Recursive CTE
sql
QUBITS OF DPK
Interview Traps ️
- CTE is temporary — NOT a permanent object like a View
- CTE can be referenced multiple times — subquery cannot
- Window function results need CTE wrapper before WHERE filter
- Recursive CTE requires UNION ALL not UNION
Topic 2 — Window Functions: The Problem Statement
The Core Problem
sql
QUBITS OF DPK
Categories of Window Functions
sql
QUBITS OF DPK
GROUP BY vs Window Functions
Topic 3 — Window Functions Intro + OVER()
The Magic Keyword: OVER()
sql
QUBITS OF DPK
OVER() with No Arguments
sql
QUBITS OF DPK
OVER(ORDER BY) — Running Total
sql
QUBITS OF DPK
Result:
sql
QUBITS OF DPK
Interview Traps ️
- OVER() alone = entire table as one window
- OVER(ORDER BY) = cumulative calculation
- Window function result CANNOT go in WHERE → must use CTE
- PARTITION BY ≠ GROUP BY → PARTITION BY never collapses rows
Topic 4 — OVER + PARTITION BY (Deep Dive)
What PARTITION BY Does
Divides table into groups, calculates separately per group. All rows preserved.
sql
QUBITS OF DPK
Result:
sql
QUBITS OF DPK
PARTITION BY + ORDER BY = Running Total Per Group
sql
QUBITS OF DPK
Result:
sql
QUBITS OF DPK
The 3 Window Sizes
sql
QUBITS OF DPK
Key Rule
sql
QUBITS OF DPK
Interview Traps ️
- PARTITION BY ≠ GROUP BY → never collapses rows
- Multiple PARTITION BY columns → window per unique combination
- ORDER BY inside OVER changes behavior completely
Topic 5 — ROW_NUMBER()
What is ROW_NUMBER()?
Assigns a unique sequential number to each row within a partition.
sql
QUBITS OF DPK
Basic Example
sql
QUBITS OF DPK
Result:
sql
QUBITS OF DPK
Most Asked Pattern — Latest Record Per Group
sql
QUBITS OF DPK
De-duplication Pattern — LeetCode #196
sql
QUBITS OF DPK
Key Properties
sql
QUBITS OF DPK
Interview Traps ️
- ROW_NUMBER() requires ORDER BY inside OVER()
- ROW_NUMBER() NEVER produces ties — even same values get different numbers
- Cannot filter result in WHERE → wrap in CTE first
- "Latest record per group" = ROW_NUMBER + PARTITION BY + ORDER BY DESC + WHERE row_num = 1
Topic 6 — RANK() & DENSE_RANK()
The Problem with ROW_NUMBER() for Rankings
Two employees with same salary get different numbers arbitrarily. Unfair. RANK and DENSE_RANK handle ties properly.
ROW_NUMBER vs RANK vs DENSE_RANK
sql
QUBITS OF DPK
RANK() Example
sql
QUBITS OF DPK
DENSE_RANK() Example
sql
QUBITS OF DPK
Top N Per Group — Most Asked MAANG Pattern
sql
QUBITS OF DPK
When to Use Which
sql
QUBITS OF DPK
Interview Traps ️
- RANK leaves gaps after ties, DENSE_RANK never does
- For Top-N always use DENSE_RANK — RANK may skip ranks at boundary
- All three require ORDER BY inside OVER()
- All three results need CTE wrapper before WHERE filter
Topic 7 — LEAD() and LAG()
What Are LEAD and LAG?
Access values from other rows relative to current row — without a self join.
sql
QUBITS OF DPK
LAG() — Compare with Previous Row
sql
QUBITS OF DPK
Result:
sql
QUBITS OF DPK
LEAD() — Compare with Next Row
sql
QUBITS OF DPK
LAG with Default Value — Avoid NULL
sql
QUBITS OF DPK
LAG with PARTITION BY — Per Customer
sql
QUBITS OF DPK
Rising Temperature — LeetCode #197 (LAG approach)
sql
QUBITS OF DPK
Interview Traps ️
- LAG/LEAD return NULL for first/last rows by default → provide default value
- LAG with PARTITION BY resets per partition
- ORDER BY inside OVER() is MANDATORY
- LAG/LEAD result cannot be in WHERE → use CTE
Quick Reference — All Window Functions
Window Frame Reference
sql
QUBITS OF DPK