SQL FOUNDATIONS:Window Functions
Use window functions for ranking, running totals, and comparisons.
SQL Window Functions Explained with Examples + Practice Questions
What are Window Functions
Window functions perform calculations across a set of rows without grouping them into a single row.
Unlike GROUP BY, they:
- Keep all rows
- Add computed values alongside each row
When to Use Window Functions
- Ranking (top employees, leaderboards)
- Running totals
- Comparing rows (previous/next)
- Interview problems (very common for mid–senior roles)
Example Dataset
Table: employees
Basic Syntax
sqlFUNCTION() OVER (PARTITION BY columnORDER BY column)
- OVER defines window
- PARTITION BY groups rows (like GROUP BY but keeps rows)
- ORDER BY defines order inside partition
ROW_NUMBER()
Assigns unique row number to each row.
sqlSELECT name, salary,ROW_NUMBER() OVER (ORDER BY salary DESC) AS rankFROM employees;
RANK()
Assigns rank (with gaps for ties).
sqlSELECT name, salary,RANK() OVER (ORDER BY salary DESC) AS rankFROM employees;
DENSE_RANK()
Assigns rank without gaps.
sqlSELECT name, salary,DENSE_RANK() OVER (ORDER BY salary DESC) AS rankFROM employees;
PARTITION BY Example
Rank employees within each department:
sqlSELECT name, department, salary,ROW_NUMBER() OVER (PARTITION BY departmentORDER BY salary DESC) AS dept_rankFROM employees;
SUM() with Window (Running Total)
sqlSELECT name, salary,SUM(salary) OVER (ORDER BY id) AS running_totalFROM employees;
AVG() with Window
sqlSELECT name, salary,AVG(salary) OVER () AS avg_salaryFROM employees;
LAG() (Previous Row)
sqlSELECT name, salary,LAG(salary) OVER (ORDER BY salary) AS previous_salaryFROM employees;
LEAD() (Next Row)
sqlSELECT name, salary,LEAD(salary) OVER (ORDER BY salary) AS next_salaryFROM employees;
Key Concepts
- Window functions do not collapse rows
- PARTITION BY groups logically
- ORDER BY defines sequence
- Useful for ranking and comparisons
Window Functions vs GROUP BY
Common Questions (FAQ)
What is a window function
A function that performs calculations across rows without grouping them.
Difference between RANK and DENSE_RANK
RANK skips numbers on ties, DENSE_RANK does not.
When to use PARTITION BY
When you want calculations within groups.
Are window functions used in interviews
Yes, especially for ranking and analytics problems.
Internal Learning Links
- Learn Aggregations
- Learn GROUP BY
- Learn Subqueries
Practice CTA
Practice window function problems on Schoolabe to master advanced SQL queries.
Window Functions Missions
Solve exercises in sequence to unlock the next mission.
Rank by salary
Rank employees by salary using RANK().
Dense rank by salary
Rank employees without gaps using DENSE_RANK().
Rank within department
Rank employees within each department.
Running total
Calculate running total of salaries.
Average salary window
Show average salary for all employees using window function.
Previous salary
Show previous salary using LAG().
Next salary
Show next salary using LEAD().
Top 2 per department
Find top 2 employees in each department.
Top per department
Find employee with highest salary in each department.