SQL Window Functions — Explained Simply | SQL Interview Series #6
If you have been working with SQL, you may have used GROUP BY to calculate things like SUM(), AVG(), COUNT(), and MAX().
But there is one problem with GROUP BY.
It combines multiple rows into one row.
What if we want to calculate something across multiple rows but still want to see every individual row?
That’s where Window Functions come in. 🚀
What is a Window Function?
A Window Function performs a calculation using a group of related rows, but it does not remove the individual rows.
Let’s understand this with a simple example.
Suppose we have an Employees table:
Now suppose we want to find the average salary of each department.
With GROUP BY:
SELECT
Department,
AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY Department;Result:
This works.
But notice something.
We lost the individual employee details.
What if we want this instead?
This is where a Window Function is useful.
SELECT
Employee,
Department,
Salary,
AVG(Salary) OVER(PARTITION BY Department) AS AvgSalary
FROM Employees;Now we get the average salary while keeping every employee row.
That’s the main idea behind Window Functions.
Why do we use Window Functions?
Window Functions are very useful when we want to:
- Give a number to each row
- Rank employees or products
- Find the previous or next row
- Compare one row with another
- Calculate running totals
- Calculate averages without grouping the rows
Some commonly used Window Functions are:
ROW_NUMBER()
RANK()
DENSE_RANK()
LAG()
LEAD()
SUM()
AVG()Let’s understand them one by one.
1. ROW_NUMBER()
ROW_NUMBER() gives a unique number to each row.
For example:
SELECT
Employee,
Department,
Salary,
ROW_NUMBER() OVER(ORDER BY Salary DESC) AS RowNum
FROM Employees;Result:
The employee with the highest salary gets 1, the next gets 2, and so on.
Want numbering to restart for each department?
Use PARTITION BY.
SELECT
Employee,
Department,
Salary,
ROW_NUMBER() OVER(
PARTITION BY Department
ORDER BY Salary DESC
) AS RowNum
FROM Employees;Result:
Here, numbering starts again from 1 for each department.
2. RANK()
RANK() is used when we want to give a rank to rows.
Let’s add another employee:
Now:
SELECT
Employee,
Salary,
RANK() OVER(ORDER BY Salary DESC) AS SalaryRank
FROM Employees;Result:
Notice something?
Both Priya and Karan have the same salary, so both get Rank 1.
But the next person gets Rank 3.
This is an important point to remember.
3. DENSE_RANK()
DENSE_RANK() is also used for ranking.
The main difference is what happens after a tie.
Using the same example:
SELECT
Employee,
Salary,
DENSE_RANK() OVER(ORDER BY Salary DESC) AS SalaryRank
FROM Employees;Result:
Here, after two people get Rank 1, the next person gets Rank 2.
So what is the difference?
The easiest way to remember it:
ROW_NUMBER()
→ Always gives a different numberRANK()
→ Same rank for same values, but skips the next rankDENSE_RANK()
→ Same rank for same values, but does NOT skip the next rank
For example:
This difference is a very common SQL interview question.
4. LAG()
Now let’s say we want to compare a row with the previous row.
Get Varsha Vishwakarma’s stories in your inbox
Join Medium for free to get updates from this writer.
For example, suppose we have monthly sales:
We want to see the previous month’s sales.
We can use LAG():
SELECT
Month,
Sales,
LAG(Sales) OVER(ORDER BY Month) AS PreviousMonthSales
FROM Sales;Result:
For January, there is no previous month, so we get NULL.
This is useful when we want to compare:
- This month’s sales vs last month’s sales
- Current salary vs previous salary
- Current transaction vs previous transaction
5. LEAD()
LEAD() is almost the opposite of LAG().
LAG() → looks at the previous row
LEAD() → looks at the next row
Example:
SELECT
Month,
Sales,
LEAD(Sales) OVER(ORDER BY Month) AS NextMonthSales
FROM Sales;Result:
For April, there is no next month, so we get NULL.
What is PARTITION BY?
You may have noticed PARTITION BY in our queries.
Don’t let the name scare you. 😄
It simply means:
“Divide the data into groups and perform the calculation separately for each group.”
For example:
ROW_NUMBER() OVER(
PARTITION BY Department
ORDER BY Salary DESC
)This means:
Divide employees by department and give them a row number based on salary within each department.
So IT employees are numbered separately, and HR employees are numbered separately.
Window Function vs GROUP BY
This is another important interview concept.
GROUP BY
GROUP BY combines rows.
SELECT
Department,
AVG(Salary)
FROM Employees
GROUP BY Department;You get one row per department.
Window Function
A Window Function calculates the value but keeps the original rows.
SELECT
Employee,
Department,
Salary,
AVG(Salary) OVER(PARTITION BY Department) AS AvgSalary
FROM Employees;You still get every employee.
Easy way to remember:
GROUP BY reduces rows. Window Functions keep the rows.
A Real Interview Example
Suppose an interviewer asks:
“Find the second-highest salary in each department.”
This is where Window Functions become very useful.
We can use DENSE_RANK():
SELECT *
FROM (
SELECT
Employee,
Department,
Salary,
DENSE_RANK() OVER(
PARTITION BY Department
ORDER BY Salary DESC
) AS SalaryRank
FROM Employees
) AS E
WHERE SalaryRank = 2;First, we rank employees inside each department.
Then we select only Rank 2.
This type of question is very common in SQL interviews.
Common Mistakes
Here are a few things to remember while using Window Functions.
1. Don’t forget ORDER BY
For functions like ROW_NUMBER(), RANK(), and LAG(), the ORDER BY inside OVER() is very important.
ROW_NUMBER() OVER(ORDER BY Salary DESC)2. Understand PARTITION BY
If you want the calculation separately for each group, use PARTITION BY.
RANK() OVER(
PARTITION BY Department
ORDER BY Salary DESC
)3. Don’t confuse RANK and DENSE_RANK
Remember:
RANK → skips numbers after a tie
DENSE_RANK → does not skip numbersQuick Interview Revision
Before your interview, remember these simple points:
Final Takeaway 🚀
Window Functions may look confusing at first, but the basic idea is actually simple:
They allow us to calculate something across multiple rows while still keeping each row in the result.
If you remember only these five functions, start with:
ROW_NUMBER() → RANK() → DENSE_RANK() → LAG() → LEAD()
Practice them with small datasets. Once you understand how they work, many SQL interview questions become much easier.
And remember:
Don’t just learn the syntax. Understand what problem the function is solving.
Coming Up in Part 7 🚀
In Part 7, we’ll explore another important SQL topic with simple explanations and practical examples.
Until then, keep practicing and keep learning.
Happy Learning! 🚀








