Introduction:

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

Description: 

  • The Dense_Rank() Function  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 it will not leave gaps in the ranking.
  •  
  • Dense_Rank() Funcation is similar to Rank() function  with only difference, this will not leave gaps in the ranking.
  •  
  • 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 Dense_RANK() Function Shown below:
  •  
  •  
  • select Name,Subject,Marks,
  • DENSE_RANK() over(partition by name order by Marks desc)Rank
  • From StudentResult order by name,subject

  • Output:


    How to use dense_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