HOW TO FIND OUT SECOND HEIGHT SALARY FROM TABLE IN SQL SERVER?

ben reitman

Total Post:96

Points:676
Posted by  ben reitman
 1201  View(s)
Ratings:
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!
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to find out second height salary from table in sql server?

    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)

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!