Introduction
In this article, we will learn about Rank Functions in SQL Server. Find more about Functions in SQL Server here- Functions in SQL Server
- ROW_NUMBER() OVER ([PARTITION BY CLAUSE] <ORDER BY CLAUSE>) - Returns the sequential number of a row within the partition of a result set at 1 for the first row of each partition.
- RANK() OVER ([PARTITION BY CLAUSE] <ORDER BY CLAUSE>) - Returns rank for rows within the partition of the result set.
- DENSE_RANK() OVER ([PARTITION BY CLAUSE] <ORDER BY CLAUSE>) - Returns rank for rows within the partition of the result set. Without any gaps in the ranking.
- NTILE(INTEGER_EXPRESSION) OVER ([PARTITION BY CLAUSE] <ORDER BY CLAUSE>) - Distributes the rows in an ordered partition into a specified number of groups.
Examples
Let's start with creating a table in SQL Server. We will create an Employee table with a few columns. You can copy and execute the following SQL statement in SSMS.
--create Employee table
create table Employee
(
EmpId int identity(1,1) primary key,
FirstName varchar(100),
LastName varchar(100),
JoinDate datetime ,
Salary int ,
Department varchar(20)
)
The above SQL statement creates a new table in the database called "Employee" with the specified columns and their data types. The table will have a primary key column called "EmpId" that is set as an identity column, meaning that it will automatically be assigned a new value for each new row inserted into the table.
Once the table is created, you can use various SQL statements to insert, update, delete, and query data from the table. For example, you can use the INSERT statement to add new rows to the table by using the below SQL query.
--Insert data to Employee table
insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Rakesh','Kalluri','2012-07-01 10:00:00.000',20000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Shabari','Vempati','2011-05-01 10:00:00.000',25000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Venkatesh','Bodupaly','2013-04-01 10:00:00.000',15000,'Bpo')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Surjan','Peddineni','2011-07-01 10:00:00.000',25000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Nani','Ch','2010-07-01 10:00:00.000',50000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Raju','Chinna','2012-07-01 10:00:00.000',25000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Kiran','Kumar','2011-07-01 10:00:00.000',20000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Raki','Kumar','2012-07-01 10:00:00.000',17000,'Bpo')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Sri','Vidya','2011-07-01 10:00:00.000',30000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Fehad','MD','2013-07-01 10:00:00.000',20000,'Bpo')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Anusha','Kumari','2011-07-01 10:00:00.000',35000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Venky','Naidu','2013-07-01 10:00:00.000',20000,'Bpo')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Radha','Kumari','2012-07-01 10:00:00.000',10000,'Bpo')
Using the above query, insert the data into the Employee table.
--selecting data from Employee table
select * from Employee
The Employee table contains columns, and the data is retrieved using a SELECT statement. All rows of data and columns of data from the table will be returned by the database engine when you execute this statement, as shown in the below snapshot.

--Row_Number() with out using partition clause
select * ,row_number() over (order by Salary desc) as Row_Num from Employee
The row number is determined using the ROW_NUMBER() function, which assigns a unique number to each row based on the order specified in the OVER() clause. In this case, the rows are ordered by the "Salary" column in descending order.









Join the conversation! Your thoughts help the community grow.