write a query to select second highest salary of the employee from the employee table?

 Posted by Peeyushkumar2 on 3/24/2013 | Category: Sql Server Interview questions | Views: 14784 | Points: 40
Answer:

select * from employee

select top 1 salary from
(select top 2 salary from employee order by(salary) desc )a
order by(salary)


It first select top 2 salary from table and arrange them in descending order now we have the second highest salary in second number then select top 1 by ascending it that is the second highest salary.


Asked In: few interviews | Alert Moderator 

Comments or Responses

Posted by: aswinialuri-19361 on: 3/27/2013 | Points: 10
hi
for second highest salary in employee table
select max(salary) from employee where salary<(select max(salary) from employee)
it is very short and easy to understand for any one

Posted by: Chvrsri on: 3/27/2013 | Points: 10
Hi Aswinialuri & Jitendrasoft09,

Thank you so much for the responses. This place is dedicated only to post the responses of the above mentioned question. If you find a better way of answerring it kindly get back to us by posting a new question then we shall review it and approve.

Thank you so much for your co-operation

Moderator Interview Section
Posted by: Pkanwar on: 3/30/2013 | Points: 10
Hi,

For getting Second Highest Salary:

Select min(salary) from Employee where salary in (select top 2 salary from Employee order by salary descending )

First In subquery will be select top 2 salary order by descending, then in Top 2 salary will be select the minimum salary, which would be second highest salary.

Thanks

Login to post response

More Interview Questions by Peeyushkumar2