Window Functions
ROW_NUMBER, RANK, DENSE_RANK, LEAD, LAG
✅ What You Will Learn
Window functions perform calculations across a set of rows that are related to the current row — without collapsing the rows the way GROUP BY does. They are one of the most powerful features in SQL and a key skill for senior data analyst roles.
The "window" is the set of rows the function operates on, defined by the OVER() clause. Window functions are used for rankings, running totals, moving averages, and comparing a row to previous or next rows.
📋 Window functions add computed columns without collapsing rows
| order_id | customer_name | product | amount | city |
|---|---|---|---|---|
| 1001 | Rahul Sharma | Laptop | 45000 | Delhi |
| 1002 | Priya Verma | Mobile Phone | 18000 | Noida |
| 1003 | Amit Kumar | Headphones | 3500 | Gurgaon |
| 1004 | Sneha Kapoor | Laptop | 52000 | Delhi |
| 1005 | Vikram Singh | Tablet | 28000 | Noida |
| 1006 | Neha Joshi | Laptop | 48000 | Delhi |
Syntax
Examples
📌 Key Points to Remember
- ✓Window functions do not collapse rows — unlike GROUP BY
- ✓OVER() defines the window — the set of rows to calculate across
- ✓PARTITION BY divides the window into groups (like GROUP BY but without collapsing)
- ✓ORDER BY inside OVER() controls which rows come "before" in the window
- ✓ROW_NUMBER, RANK, DENSE_RANK are the most asked-about in SQL interviews
🏢 Real-World Application
Window functions are what separate intermediate SQL writers from advanced ones. They are used in almost every complex analytics task. "Rank each salesperson within their region by monthly revenue" — RANK() OVER(PARTITION BY region ORDER BY revenue DESC). "Calculate a 7-day rolling average of daily orders" — AVG(orders) OVER(ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW). "Find month-over-month revenue change" — revenue - LAG(revenue, 1) OVER(ORDER BY month). Data analyst roles at top companies like Google, Meta, Flipkart, and HDFC Bank regularly test window function skills in SQL interviews.
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
What are window functions in SQL?
Window functions perform calculations across a set of rows related to the current row — without collapsing the result like GROUP BY does. They add a computed column to each row while keeping all rows visible. Common ones: ROW_NUMBER(), RANK(), SUM() OVER(), LAG(), LEAD().
What is the difference between GROUP BY and window functions?
GROUP BY collapses rows into a single summary row per group. Window functions keep all rows intact and add computed values alongside them. Use GROUP BY for aggregated reports; use window functions when you need both detail and aggregated context in the same row.
What is PARTITION BY in window functions?
PARTITION BY divides rows into groups (partitions) within which the window function is applied independently. ROW_NUMBER() OVER(PARTITION BY product ORDER BY amount DESC) numbers rows within each product group, restarting at 1 for each product.
What is the difference between ROW_NUMBER, RANK, and DENSE_RANK?
All three assign rankings but handle ties differently. ROW_NUMBER gives a unique number to every row even with ties. RANK gives the same rank to ties but skips the next rank(1,2,2,4). DENSE_RANK gives the same rank to ties without skipping (1,2,2,3).
What are LAG and LEAD functions in SQL?
LAG(col, n) returns the value of col from n rows before the current row. LEAD(col, n) returns the value from n rows after. Both are used for period-over-period comparisons: LAG(revenue, 1) gets last month's revenue for month-over-month change calculations.
✏️ Practice Exercise
Write a query to show each order with a running total of revenue. Then add a column showing each order's rank by amount within its product category.
Learn SQL with Live Trainer Guidance
These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.