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…