Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have table with fields
1. SalaryID
2.Salary Amount

i want to get second highest salary from my table using query.
Posted

select * from salary where salamount not in (select max(salamount) from salary)
 
Share this answer
 
v2
I Don't Have SQL to hand to test by think something like this would work;

Select Top 1 * From ( Select Top 2 * From Salaries Order By Salary Desc) Order By Salary Asc;


Which should in theory give you the 2nd highest salary, by first creating a set of highest 2 salaries and then flipping the order and returning the lowest.
 
Share this answer
 
v2
Comments
Yusuf 22-Jan-11 9:51am    
Proposed as answer
Here is a code to get the N-th highest/lowest salary if you use MS Sql Server 2005/2008

-- N-th highest salary
SELECT  TOP 1
        SalaryAmount
FROM    (
        SELECT  SalaryRank  = ROW_NUMBER() OVER(ORDER BY SalaryAmount DESC),
                SalaryAmount
        FROM    Salary
        ) a
WHERE   SalaryRank = 2 --N

-- N-th lowest salary
SELECT  TOP 1
        SalaryAmount
FROM    (
        SELECT  SalaryRank  = ROW_NUMBER() OVER(ORDER BY SalaryAmount),
                SalaryAmount
        FROM    Salary
        ) a
WHERE   SalaryRank = 2 --N


Hope this help!
 
Share this answer
 
i got the answer

select max(amount) from salary where amount not in (select max(amount) from salary)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900