# 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

insert

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

insert

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

·

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

# Book Review: Big Red – Voyage of a Trident Submarine

I’ve grown up reading Tom Clancy and probably most of you have at least seen Red October, so this book caught my eye when browsing used books for a recent trip. It’s a fairly human look at what’s involved in sailing on a Trident missile submarine…

Andy Warren

2009-03-10

# 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

# Inserting Markup into a String with SQL

In which Phil illustrates an old trick using STUFF to intert a number of substrings from a table into a string, and explains why the technique might speed up your code…

Phil Factor

2009-02-18

# 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