Home > DeveloperSection > Forums > How to find out second height salary from table in sql server?
ben reitman
ben reitman

Total Post:96

Points:676
Posted on    May-16-2013 3:10 AM

 MSSQL Server MSSQL Server 
Ratings:


 1 Reply(s)
 908  View(s)
Rate this:
Hi Expert!

I'm new in database. My problem is that, how to find second height salary from table in sql server. I'm getting height salary as following...

CREATE DATABASE DEMO

USE DEMO

CREATE TABLE tblEmployee
(
EmpID INT PRIMARY KEY,
Salary MONEY
)

INSERT INTO tblEmployee(EmpID,Salary) VALUES (100,10000)
INSERT INTO tblEmployee(EmpID,Salary) VALUES (102,20000)
INSERT INTO tblEmployee(EmpID,Salary) VALUES (103,30000)
INSERT INTO tblEmployee(EmpID,Salary) VALUES (104,15000)
INSERT INTO tblEmployee(EmpID,Salary) VALUES (105,35000)

SELECT MAX(Salary) AS 'Height Salary ' FROM tblEmployee

It return '35000.00' that's max salary, but how to get second max salary '30000'!

Please help me!

Thanks in advance!


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-16-2013 8:13 AM

Hi Ben!

You can try as following

select MAX(Salary) from tblEmployee where Salary not in (select MAX(Salary) from tblEmployee)

OR

select MAX(Salary) from tblEmployee where Salary < (select MAX(Salary) from tblEmployee)

Don't want to miss updates? Please click the below button!

Follow MindStick