When working with employee data, you may sometimes need to rank employees based on their salaries.

For example, suppose a company has employees working in IT and HR departments. You want to find the highest-paid employees in each department.
You can use the "RANK()" window function for this.
But there is one important difference between ranking all employees together and ranking employees separately within each department.
This is where the SQL "PARTITION BY" clause becomes useful.
In this tutorial, let's understand how "PARTITION BY" works with a practical example.
- Understanding the Employee Table
Consider an "employees" table with the following records:
employee_name| department| salary
Amit| IT| 90000
Rahul| IT| 75000
Sneha| IT| 65000
Priya| HR| 85000
Neha| HR| 70000
Raj| HR| 60000
Suppose you want to rank employees based on their salaries, starting with the highest salary.
- Ranking Employees Across the Entire Company
First, let's use the "RANK()" window function without "PARTITION BY".
SELECT
employee_name,
department,
salary,
RANK() OVER (
ORDER BY salary DESC
) AS salary_rank
FROM employees;
Expected Output
employee_name| department| salary| salary_rank
Amit| IT| 90000| 1
Priya| HR| 85000| 2
Rahul| IT| 75000| 3
Neha| HR| 70000| 4
Sneha| IT| 65000| 5
Raj| HR| 60000| 6
Here, SQL ranks all employees together based on their salaries.
Amit gets rank 1 because he has the highest salary, while Priya gets rank 2.
But what if your requirement is to start the ranking from 1 separately for each department?
- Using PARTITION BY in SQL
Now let's add "PARTITION BY department" to the same query.
SELECT
employee_name,
department,
salary,
RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS department_rank
FROM employees;
Expected Output
employee_name| department| salary| department_rank
Amit| IT| 90000| 1
Rahul| IT| 75000| 2
Sneha| IT| 65000| 3
Priya| HR| 85000| 1
Neha| HR| 70000| 2
Raj| HR| 60000| 3
Notice the difference.
Amit gets rank 1 in IT, and Priya also gets rank 1 in HR.
The ranking starts again from 1 for each department.
- Comparing RANK() With and Without PARTITION BY
Let's compare both calculations in a single query.
SELECT
employee_name,
department,
salary,
RANK() OVER (
ORDER BY salary DESC
) AS company_rank,
RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS department_rank
FROM employees
ORDER BY department, salary DESC;
Expected Output
employee_name| department| salary| company_rank| department_rank
Amit| IT| 90000| 1| 1
Rahul| IT| 75000| 3| 2
Sneha| IT| 65000| 5| 3
Priya| HR| 85000| 2| 1
Neha| HR| 70000| 4| 2
Raj| HR| 60000| 6| 3
This example makes the difference clear.
- "company_rank" ranks employees across the entire company.
- "department_rank" ranks employees separately within each department.
- How Does PARTITION BY Work?
Let's understand the important parts of the query.
RANK()
Assigns a ranking to each row based on the specified sorting condition.
ORDER BY salary DESC
Arranges salaries from highest to lowest for the ranking calculation.
PARTITION BY department
Divides employees into separate groups based on their departments. The ranking starts again within each group.
Final ORDER BY
Controls how the result rows are displayed. It does not change the ranking calculation.
- Where Can You Use PARTITION BY in Real Projects?
The "PARTITION BY" clause is useful in many SQL reporting and data analysis tasks.
Some practical examples include:
- Finding the highest-paid employee in each department.
- Ranking products within each product category.
- Finding the top-performing salesperson in each region.
- Comparing monthly sales for different customers.
- Identifying the latest order for every customer.
You can also use "PARTITION BY" with other window functions such as "ROW_NUMBER()", "DENSE_RANK()", "SUM()", "AVG()" and "COUNT()".
Conclusion
The main difference is simple.
Without "PARTITION BY", the window function calculates results across the entire result set.
With "PARTITION BY", it calculates results separately within each group.
Understanding this difference is important when working with SQL window functions, employee reports, sales analysis and real-world data analytics projects.
Practice Question
What would happen if you replaced "RANK()" with "DENSE_RANK()" in the above query?
Would the output change if two employees in the same department had exactly the same salary?
Try modifying the sample data and running the query to find out.
For more SQL practice, explore the "SQL and PL/SQL interview questions and tutorials" (https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/subqueries-solve-queries-sql-faq.html) with practical examples and explanations.
Top comments (0)