SQL Statement To find the second largest salary from the Employee table
Finding result in 4 ways :
SELECT TOP 1 Salary FROM
(SELECT TOP 2 Salary FROM Employee ORDER BY Salary DESC) Temp
ORDER BY Salary ASC
SELECT TOP 1 Salary FROM Employee
WHERE Salary NOT IN (SELECT TOP 1 Salary FROM Employee ORDER BY Salary DESC)ORDER BY Salary DESC
SELECT Salary FROM Employee A
WHERE 2 = (SELECT count(*) FROM Employee B WHERE A.Salary <= B.Salary)
SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee)
Finding result in 4 ways :
SELECT TOP 1 Salary FROM
(SELECT TOP 2 Salary FROM Employee ORDER BY Salary DESC) Temp
ORDER BY Salary ASC
SELECT TOP 1 Salary FROM Employee
WHERE Salary NOT IN (SELECT TOP 1 Salary FROM Employee ORDER BY Salary DESC)ORDER BY Salary DESC
SELECT Salary FROM Employee A
WHERE 2 = (SELECT count(*) FROM Employee B WHERE A.Salary <= B.Salary)
SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee)
No comments:
Post a Comment