Outcomes
- • Use PARTITION BY and ORDER BY with confidence.
- • Build rankings, running totals, and change-over-time metrics.
- • Keep row-level detail while adding analytic calculations.
The most powerful feature in SQL — and the most asked-about in data interviews. Rank rows, run totals, and compare each row to its neighbors without collapsing your result set.
Professional learning blueprint
Outcomes
Real-world use
Fast study plan
Deep learning journey
Use window functions to rank, compare trends, and compute advanced row-level analytics.
Separate base row set from analytic calculation frame.
Compute robust ranks and ordered row positions.
Track progression and deltas over time.
Bonus lesson units
1. FIRST_VALUE and LAST_VALUE
Extract the first and last values in a window partition — useful for comparing each row against group boundaries.
2. RANGE vs ROWS frame mode
Understand when RANGE includes extra peer rows and when ROWS gives precise row offsets.
3. Window functions in production analytics
Patterns for cohort analysis, retention metrics, and session attribution using window functions.
Expanded practice
1. Salary rank per department
easyRank salaries within each department using PARTITION BY.
2. Dense ranking variation
mediumCompare RANK and DENSE_RANK outputs for tied salaries.
3. Running revenue total
mediumCompute running totals by order date and customer.
4. Previous period delta
hardUse LAG to calculate period-over-period change percentage.
5. Top-N per group
hardReturn top 3 earners per department using window filtering.
6. Hire timeline sequence
easyUse ROW_NUMBER to sequence hires by department and date.
7. Moving average trend
hardCompute rolling 3-period average with explicit frame clause.
8. Quartile segmentation
mediumUse NTILE(4) to segment salaries into quartiles.
9. First/last value drill
mediumUse FIRST_VALUE and LAST_VALUE in partitioned windows.
10. Window quality audit
hardValidate partition sizes and ranking consistency in final output.
Study references
Window functions are what separate intermediate SQL writers from advanced ones. They let you compute values that span multiple rows — rankings, running totals, period-over-period comparisons — while keeping every row in the output.
OVER() clause and how window functions differ from aggregatesPARTITION BY — reset the window per group (like GROUP BY, but rows stay intact)ORDER BY inside OVER() — enables running totals and rankingsROW_NUMBER, RANK, DENSE_RANK, NTILELAG and LEAD — read the previous or next row's valueSUM OVER, AVG OVER, MIN/MAX OVERROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROWThe HR database (salary rankings, department running payroll) and the Store database (revenue trends, month-over-month growth). These are the exact query patterns you will see in data analyst and data engineering interviews.
All previous courses. Window functions build on joins, aggregates, and CTEs.