How to find second highest value in table ?

 How to find second highest value in table ?

 SQL Server 
Find the highest employee salary   

SELECT EmpId, Salary
FROM(
Select EmpId, Salary, ROW_NUMBER() OVER(ORDER BY Salary Desc) AS Salary_Order
FROM   Employee
) results
WHERE results. Salary_Order = 1 ;

Find the 2nd highest employee salary   

SELECT EmpId, Salary
FROM(
Select EmpId, Salary, ROW_NUMBER() OVER(ORDER BY Salary Desc) AS Salary_Order
FROM   Employee
) results
WHERE results. Salary_Order = 2 ;

Find the 3rd highest employee salary   

SELECT EmpId, Salary
FROM(
Select EmpId, Salary, ROW_NUMBER() OVER(ORDER BY Salary Desc) AS Salary_Order
FROM   Employee
) results
WHERE results. Salary_Order = 3 ;

  
MySQL Server 
Type 1 : Find the 2nd highest employee salary  

SELECT MAX(salary) 
FROM Employee 
WHERE salary NOT IN ( SELECT Max(salary) FROM Employee);

Type 2 : Find the 2nd highest employee salary  

SELECT MAX(salary) 
From Employee 
WHERE salary < ( SELECT Max(salary) FROM Employee);

Type 3 : Find the 2nd highest employee salary  


SELECT salary 
FROM (
             SELECT salary FROM Employee ORDER BY salary DESC LIMIT 2
       ) AS Emp ORDER BY salary LIMIT 1;

Published by arjunpremier

Software Engineer, Blogger,

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started