Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 second highest salary

Author  Topic 

desikankannan
Posting Yak Master

152 Posts

Posted - 2008-01-25 : 04:38:37
Hi,
iam new to this forum i was asked a question in an interview
how we can get second highest salary and person name
How to retrive second highest salary query



Desikankannan

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-01-25 : 05:01:52
SELECT s.Salary
FROM
(SELECT DISTINCT Salary,
DENSE_RANK() OVER (ORDER BY Salary DESC) AS rnk
FROM table1
WHERE Salary IS NOT NULL) AS s
WHERE rnk = 2

Just replace rnk with whatever highest salary you want.

Also, please try to google first for your queries. If you would have put "second highest salary sql server", you would have got your answer and many more ways to achieve it. The reply that I am posting is from following link:
http://www.eggheadcafe.com/software/aspnet/29735786/third-highest-salary.aspx
Go to Top of Page

raaj
Posting Yak Master

129 Posts

Posted - 2008-01-25 : 09:17:40
u can try in this way also :

select min(salary) from table_name
where salary in (select top 2 salary from table_name order by salary desc)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-07 : 11:50:50
quote:
Originally posted by desikankannan

Hi,
iam new to this forum i was asked a question in an interview
how we can get second highest salary and person name
How to retrive second highest salary query



Desikankannan


http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/find-nth-maximum-value.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -