Find Nth highest salary using RANKING functions

UmaShankar-Patel, 2012-12-19

Suppose you want to find nth highest salary from a table,Then

you can use DENSE_RANK() function.Here i show some example that show results

and i hope clear concept about ranking functions          

DENSE_RANK(),RANK(),ROW_NUMBER(),NTILE()

in SQL Server:

CREATE TABLE [dbo].[Person1](

     [id] [int] IDENTITY(1,1) NOT NULL,

     [name] [varchar](20) NULL,

     [sal] [int] NULL

) ON [PRIMARY]

Insert Data into table

insert 

into Person1  (name,sal)values(‘Anil’,‘100000’)

insert 

into Person1  (name,sal)values(‘Patel’,‘100000’)

insert 

into Person1  (name,sal)values(‘Chikoti’,‘300000’)

insert 

into Person1  (name,sal)values(‘Nikol’,‘100000’)

insert 

into Person1  (name,sal)values(‘Rajeev’,‘100000’)

insert 

into Person1  (name,sal)values(‘Binod’,‘300000’)

insert 

into Person1  (name,sal)values(‘Pradeep’,‘500000’)

insert 

into Person1  (name,sal)values(‘Dinel’,‘300000’)

insert 

into Person1  (name,sal)values(‘Gemil’,‘400000’)

Brief about These: check MSDN

·        

ROW_NUMBER():Returns

rownumber of each row of result set.

Syntax:

ROW_NUMBER ( )

    OVER ( [ PARTITION BY

value_expression , … [ n ] ] order_by_clause )

·       

DENSE_RANK(): Returns the

rank of rows within the partition of a result set, without any gaps in the

ranking.

Syntax:

DENSE_RANK ( ) OVER

( [ <partition_by_clause> ] < order_by_clause >

)

·        

RANK():

Returns the rank of each row within the partition of a

result set.

Syntax:

RANK ( )

OVER ( [

partition_by_clause ] order_by_clause )

Now see result

 select

id,

name,

sal,

DENSE_RANK() over (order by sal) as [DENSE_RANK],

RANK() over (order by sal) as [RANK()],

ROW_NUMBER() over (order by sal) as [ROW_NUMBER()]

  from Person1

      OUTPUT:

Now if we want to find name that gets 2nd highest salary then execute this

query

DENSE_RANK() can be

used for this purpose because it provide a number for same values.

SELECT

a.Name,a.sal

FROM(SELECT Name,sal,DENSE_RANK() over(ORDER BY sal) AS rk FROM Person1) as a where rk=2












If you want to 3rd highest then put rk=3 and as you wish.

SELECT

a.Name,a.sal

FROM(SELECT Name,sal,DENSE_RANK() over(ORDER BY sal) AS rk FROM Person1) as a where rk=3

Rate

Share

Share

Rate

Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

Robert Davis

2009-02-23

1,567 reads

Networking – Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I’d like to talk about social networking. We’ll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let…

Andy Warren

2009-02-17

1,530 reads

Speaking at Community Events – More Thoughts

Last week I posted Speaking at Community Events – Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I’ve got a few more thoughts on the topic this week, and I look forward to your comments.

Andy Warren

2009-02-13

360 reads