SQL window functions are useful when you need to analyze data while keeping individual rows in the result.

In real-world data analytics, you may need to rank employees by salary, compare salaries within departments, or identify the previous and next employee's salary.
Instead of writing separate queries for each requirement, you can use multiple SQL window functions in a single query.
In this practical SQL example, let's see how to use five commonly used window functions together:
- "ROW_NUMBER()"
- "RANK()"
- "DENSE_RANK()"
- "LAG()"
"LEAD()"
Create the Employee Table
Suppose we have an employee table containing employee names, departments, and salaries.
Input Table: employees
| employee_name | department | salary |
|---|---|---|
| Asha | IT | 90000 |
| Ravi | IT | 80000 |
| Neha | IT | 80000 |
| Amit | HR | 75000 |
| Pooja | HR | 65000 |
| Kiran | HR | 65000 |
Notice that some employees have the same salary. This makes the example useful for understanding the difference between ranking functions.
- Understand the SQL Challenge
We want to display the following information:
- A unique row number for every employee within their department.
- Salary ranking within each department.
- Dense salary ranking without skipping rank numbers.
- The previous employee's salary.
- The next employee's salary.
We will use "PARTITION BY department" to perform these calculations separately for IT and HR.
- SQL Query Using Five Window Functions
Here is the complete Oracle SQL query:
SELECT
employee_name,
department,
salary,
ROW_NUMBER() OVER (
PARTITION BY department
ORDER BY salary DESC, employee_name
) AS rn,
RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS rnk,
DENSE_RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS drnk,
LAG(salary) OVER (
PARTITION BY department
ORDER BY salary DESC, employee_name
) AS prev_sal,
LEAD(salary) OVER (
PARTITION BY department
ORDER BY salary DESC, employee_name
) AS next_sal
FROM employees
ORDER BY department, salary DESC, employee_name;
- Expected Output
| employee_name | dept | salary | rn | rnk | drnk | prev_sal | next_sal |
|---|---|---|---|---|---|---|---|
| Amit | HR | 75000 | 1 | 1 | 1 | NULL | 65000 |
| Kiran | HR | 65000 | 2 | 2 | 2 | 75000 | 65000 |
| Pooja | HR | 65000 | 3 | 2 | 2 | 65000 | NULL |
| Asha | IT | 90000 | 1 | 1 | 1 | NULL | 80000 |
| Neha | IT | 80000 | 2 | 2 | 2 | 90000 | 80000 |
| Ravi | IT | 80000 | 3 | 2 | 2 | 80000 | NULL |
- How Do These Five SQL Window Functions Work?
ROW_NUMBER()
"ROW_NUMBER()" assigns a unique sequential number to each row within a partition.
For example, in the IT department:
- Asha gets row number 1.
- Neha gets row number 2.
- Ravi gets row number 3.
Even when two employees have the same salary, they receive different row numbers.
RANK()
"RANK()" assigns the same rank to employees with equal salaries.
For the HR department:
- Amit earns 75,000 and gets rank 1.
- Kiran and Pooja both earn 65,000 and get rank 2.
If another salary level followed these two employees, its rank would be 4 because RANK() skips the next rank after a tie.
DENSE_RANK()
"DENSE_RANK()" also assigns the same rank to equal salaries, but it does not skip rank numbers.
For example:
- Salary 75,000 gets rank 1.
- Salary 65,000 gets rank 2.
The next distinct salary would receive rank 3, not rank 4.
LAG()
"LAG()" retrieves a value from a previous row within the specified window.
In our example, Asha's previous salary is NULL because she is the first employee in the IT department.
Neha's previous salary is 90,000 because Asha appears immediately before her.
LEAD()
"LEAD()" retrieves a value from the next row within the specified window.
For example, Asha's next salary is 80,000, while Ravi's next salary is NULL because he is the last employee in the IT department.
- Why Do We Use PARTITION BY?
This is an important part of the query.
PARTITION BY department
It divides the employees into separate department groups.
The window calculations restart for every department.
Without "PARTITION BY", the ranking functions would calculate ranks across the entire company instead of separately within IT and HR.
Also, notice that "employee_name" is included as a secondary sorting column in "ROW_NUMBER()", "LAG()", and "LEAD()" to make the order consistent when salaries are equal.
For "RANK()" and "DENSE_RANK()", we order only by salary so employees with equal salaries receive the same rank.
- Key Differences Between the Five Functions
Function| Purpose
ROW_NUMBER()| Assigns a unique row number
RANK()| Assigns ranks with gaps after ties
DENSE_RANK()| Assigns ranks without gaps
LAG()| Retrieves the previous row's value
LEAD()| Retrieves the next row's value
- Your Turn: SQL Interview Question
Suppose the employee salaries in a department are:
salary
90000
80000
80000
70000
What will be the results of "RANK()" and "DENSE_RANK()"?
Can you calculate both results without executing the SQL query?
Share your answer in the comments.
Conclusion
SQL window functions make it easier to perform ranking, comparison, and row-by-row analysis.
Understanding "ROW_NUMBER()", "RANK()", "DENSE_RANK()", "LAG()", and "LEAD()" is particularly useful when working with employee reports, salary analysis, business reporting, and data analytics projects.
If you want to learn more about SQL window functions and analytic functions, explore this practical tutorial:
SQL Window Functions and Analytic Functions – Sankalan Data Tech
Top comments (0)