
Understanding the RANK() Function in SQL with Practical Examples
This article provides a better description of the SQL RANK() and with a way of executing such as well as its utility. We shall discuss how, when an ordering is specified, RANK () assigns ranks by row to a partition of a result set. It includes some example with sample tables data. The RANK() function assigns a rank to each row within a partition and it is based on the order of the specified column (or columns). It is also here that equal rankings are given to two or more rows sharing the same value of the column or columns used in the ranking. Due to missing the next level in the ranking, it also has elements where there are gaps in the ranking sequence.
Overview of RANK()
The following is the RANK() function's fundamental syntax:
- RANK() OVER (
- [PARTITION BY column1, column2, ...]
- ORDER BY column3 [ASC | DESC], column4 [ASC | DESC], ...
- )
- PARTITION BY (Optional): A clustering of the result set. The ranks of each of the partitions are considered independently. When the PARTITION BY is not provided, the whole result set is considered as one partition.
- ORDER BY: Contains the name of the column(s) which specify the ranking order in each partition. The default order is ASC (ascending) and reverse order can be in descending order.
- OVER(): Defines the window (set of rows) to which the function applies.
Let's consider a table named Employees
with the following structure and data:
- --create objects Employees
- CREATE TABLE Employees (
- EmployeeID INT PRIMARY KEY
- ,Department VARCHAR(50)
- ,Salary DECIMAL(10, 2)
- );
- -- insert in Employees
- INSERT INTO Employees (EmployeeID, Department, Salary)
- VALUES
- (1, 'Sales', 60000.00),
- (2, 'Marketing', 55000.00),
- (3, 'Sales', 70000.00),
- (4, 'IT', 80000.00),
- (5, 'Marketing', 55000.00),
- (6, 'IT', 90000.00),
- (7, 'Sales', 70000.00),
- (8, 'HR', 65000.00),
- (9, 'HR', 75000.00),
- (10, 'IT', 85000.00);
Example 1: Ranking Employees by Salary
the objective was to rank the employees according to the salary paid.
We can tabulate the employees in the terms of salary throughout the company.
- --get data Employees
-
- SELECT EmployeeID, Department, Salary ,RANK() OVER (ORDER BY Salary DESC) AS SalaryRank
- FROM Employees;
- --OutPut
- EmployeeID Department Salary SalaryRank
- ----------- ------------------------------ ------------------------------ --------------------
- 6 IT 90000.00 1
- 10 IT 85000.00 2
- 4 IT 80000.00 3
- 9 HR 75000.00 4
- 3 Sales 70000.00 5
- 7 Sales 70000.00 5
- 8 HR 65000.00 7
- 1 Sales 60000.00 8
- 2 Marketing 55000.00 9
- 5 Marketing 55000.00 9
This table demonstrates the hierarchy of every employee based on the salary.
Example 2: Listing the employees in terms of salary wise in each department.
This is just an illustration of ranking the employees based on salary at the workplace. - SELECT EmployeeID,Department,Salary
- ,RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS DepartmentSalaryRank
- FROM Employees;
- --output
-
- EmployeeID Department Salary DepartmentSalaryRank
- ----------- ------------------------------ ------------------------------ --------------------
- 9 HR 75000.00 1
- 8 HR 65000.00 2
- 6 IT 90000.00 1
- 10 IT 85000.00 2
- 4 IT 80000.00 3
- 5 Marketing 55000.00 1
- 2 Marketing 55000.00 1
- 3 Sales 70000.00 1
- 7 Sales 70000.00 1
- 1 Sales 60000.00 3
The given example illustrates the ranking of employees by salary by department.The column of DepartmentSalaryRank contains the rank of every employee within his/her department. The SELECT statement clause PARTITION BY Department is a command to SQL to rank employees individually in each department. In the instance of the Sales department, the top two earners are employee 3 and employee 7 hence they all have rank 1.
- SELECT EmployeeID,Department,Salary
- ,RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS DepartmentSalaryRank
- FROM Employees;
- --output
- EmployeeID Department Salary DepartmentSalaryRank
- ----------- ------------------------------ ------------------------------ --------------------
- 9 HR 75000.00 1
- 8 HR 65000.00 2
- 6 IT 90000.00 1
- 10 IT 85000.00 2
- 4 IT 80000.00 3
- 5 Marketing 55000.00 1
- 2 Marketing 55000.00 1
- 3 Sales 70000.00 1
- 7 Sales 70000.00 1
- 1 Sales 60000.00 3
Example 3: Using RANK() with a Common Table Expression (CTE)
This example demonstrates how to use RANK() within a CTE to filter the results based on the rank. - WITH RankedEmployees AS (
- SELECT
- EmployeeID,Department,Salary
- ,RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS DepartmentSalaryRank
- FROM Employees
- )
- SELECT
- EmployeeID,Department,Salary
- FROM RankedEmployees
- WHERE DepartmentSalaryRank = 1;
- --output
- EmployeeID Department Salary
- ----------- ------------------------------ ------------------------------
- 9 HR 75000.00
- 6 IT 90000.00
- 5 Marketing 55000.00
- 2 Marketing 55000.00
- 3 Sales 70000.00
- 7 Sales 70000.00
The query initializes all employees arranged by salary in each department using RANK () function in CTE RankedEmployees. The outer query will then only pick those that have DepartmentSalaryRank=1 giving the best-paid employee (or employees) per department.
Conclusion:
RANK() assigns a rank to each row in a result set based on the order in which you specify. You may sort by the best performers, analyze the distribution of data, or even sort by rank. In order to get optimal use of the RANK(), you must know the meaning of PARTITION BY and ORDER BY in the OVER() clause. Remember that RANK() can omit numbers when the ordering values are equal. To enhance your queries, you can apply CTEs which allow you to divide the complicated queries into simpler and more understandable ones and unveil more data about your information.
- WITH RankedEmployees AS (
- SELECT
- EmployeeID,Department,Salary
- ,RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS DepartmentSalaryRank
- FROM Employees
- )
- SELECT
- EmployeeID,Department,Salary
- FROM RankedEmployees
- WHERE DepartmentSalaryRank = 1;
- --output
- EmployeeID Department Salary
- ----------- ------------------------------ ------------------------------
- 9 HR 75000.00
- 6 IT 90000.00
- 5 Marketing 55000.00
- 2 Marketing 55000.00
- 3 Sales 70000.00
- 7 Sales 70000.00
Nikhil Reply
good