Posts

Showing posts from July, 2018

Difference between ROW_NUMBER(),RANK() and DENSE_RANK() function in SQL

Create table #tamp ( EmpID   int identity ( 1 , 1 ) primary key ( EmpID ), Name varchar ( 200 ), Salary int ) select * from #tamp --insert into #tamp values('Rahul',10000) --insert into #tamp values('Vishal',15000) --insert into #tamp values('Mehul',20000) --insert into #tamp values('Hiren',15000) --insert into #tamp values('Amit',15000) --insert into #tamp values('Mihir',20000) ROW_NUMBER() Function keeps increasing integer by one and it is not caring duplicate values RANK() and DENSE_RANK() functions are looking for duplicate values RANK() Function The integer value is increasing by one but if the same value (Salary) is present in the table, then the same integer value is given to all the rows having the same value(Salary) Select * , RANK () over ( order by salary desc ) as rank , DENSE_RANK () over ( order by salary desc ) as denserank , ROW_NUMBER () over ( order by sal...