RANK gives you the ranking within your ordered partition.
Ties are assigned the same rank, with the next ranking skipped. So, if you
have 3 items at rank 2, the next rank listed would be ranked 5.
DENSE_RANK
again gives you the ranking within your ordered partition, but no ranks are
skipped if there are ranks with multiple items.
E.g:- Suppose we have a table
called SalaryDetails as shown below
Query:- Select * from
SalaryDetails;
O/P:-
|
Id
|
EmployeeDetailsId
|
Salary
|
|
101
|
1
|
2000.50
|
|
102
|
2
|
3050.50
|
|
103
|
3
|
4000.00
|
|
104
|
4
|
2000.50
|
|
105
|
5
|
2000.50
|
Condition:- Fetch the records
from the table and assign the rank to the EmployeeDetailsId on the basis of
salary ascending to descending order.
Query:- Select
EmployeeDetailsId,Salary,
DENSE_RANK() Over(Order By Salary) as
DenseRank_Salary,
Rank() Over(Order by Salary) as Rank_Salary
from SalaryDetails;
O/P:-
|
EmployeeDetailsId
|
Salary
|
DenseRank_Salary
|
Rank_Salary
|
|
1
|
2000.50
|
1
|
1
|
|
5
|
2000.50
|
1
|
1
|
|
4
|
2000.50
|
1
|
1
|
|
2
|
3050.50
|
2
|
4
|
|
3
|
4000.50
|
3
|
5
|