Introduction:

Here we will explain how to use Rank() Function in SQL Server or what is Rank() Function  in SQL Server with example.

Description:

The Rank() function return a rank based on the partitioned column order when two rows have the same order value. It provides the same rank for the two rows and also increases the same order by clause.

Example:

Here we are going to create StudentResult table  Shown below:

Create table StudentResult(
name varchar(50),
Subject varchar(20),
Marks int
)

Here we are going to insert record in StudentResult table  Shown below:

 

insert into StudentResult values('Aman','Maths',70)
insert into StudentResult values ('Aman','Science',80)
insert into StudentResult values ('Aman','Social',60)
 
insert into StudentResult values('Ankit','Maths',60)
insert into StudentResult values ('Ankit','Science',50)
insert into StudentResult values ('Ankit','Social',70)
 
insert into StudentResult values('Sohel','Maths',90)
insert into StudentResult values ('Sohel','Science',90)
insert into StudentResult values ('Sohel','Social',80)

 

Here we are going to write a select query  with RANK() Function Shown below:

 

 

select Name,Subject,Marks,
RANK() over(partition by name order by Marks desc)Rank
From StudentResult order by name,subject


Output:


How to use  RANK() function in SQL Server


I Hope this article will help to you.

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like