Wednesday, September 10, 2008

Difference Between Rank and DenseRank in Sql

Hi,
Here is the differnce between  Rank() and DenseRank() function
Consider Below  Table Employee Which is ordered by salary
Table Name: Employee
EmpId         Name         Salary
 1  John       100
 2               Jenny           170
 3               Mac             170
 4               Millon 350

Here is the query to rank employee by salary

Select *, Dense_RANK() over(order by Salary) As DenseRank,RANK() over(order by Salary) As Rank From Employee  


Now the tablewill be
EmpId           Name            Salary       Rank     DenseRank
 1                John             100          1             1
 2                 Jenny             170          2             2
 3                 Mac                170         2             2
 4                 Millon  350         4             3


Dense Rank last value will be the total unique value in table


2 comments:

Vinothkumar S said...

hmm.....

Sundaram Murugan said...

Hi Shallini,
I am used this concept in my application today , am very happy about this topic ..Thanks a lot :)