SQL query to find second highest salary? - GeeksforGeeks

02 Nov, 2021
Consider below simple table: 
Name     Salary
---------------
abc     100000
bcd     1000000
efg     40000
ghi     500000
How to find the employee whose salary is second highest. For example, in above table, “ghi” has the second highest salary as 500000. 
Below is simple query to find the employee whose salary is highest. 
  select *from employee where salary=(select Max(salary) from employee);
We can nest the above query to find the second largest salary. 
select *from employee 
group by salary 
order by  salary desc limit 1,1;
There are other ways :
SELECT name, MAX(salary) AS salary 
FROM employee 
WHERE salary IN
(SELECT salary FROM employee MINUS SELECT MAX(salary) 
FROM employee); 
 
SELECT name, MAX(salary) AS salary 
FROM employee 
WHERE salary <> (SELECT MAX(salary) 
FROM employee);
IN SQL Server using Common Table Expression or CTE, we can find the second highest salary: 
WITH T AS
(
SELECT *
   DENSE_RANK() OVER (ORDER BY Salary Desc) AS Rnk
FROM Employees
)
SELECT Name
FROM T
WHERE Rnk=2;
How to find the third largest salary? Simple, we can do one more nesting.  
SELECT name, MAX(salary) AS salary
  FROM employee
 WHERE salary < (SELECT MAX(salary) 
                 FROM employee
                 WHERE salary < (SELECT MAX(salary)
                 FROM employee)
                ); 
Note that instead of nesting for second, third, etc largest salary, we can find nth salary using general query like in MySQL: 
SELECT salary 
FROM employee 
ORDER BY salary desc limit n-1,1
SELECT name, salary
FROM employee A
WHERE n-1 = (SELECT count(1) 
             FROM employee B 
             WHERE B.salary>A.salary)
If multiple employee have same salary. Suppose you have to find 4th highest salary 
SELECT * FROM employee 
WHERE salary= (SELECT DISTINCT(salary) 
FROM employee ORDER BY salary LIMIT 3,1);
Generic query will be 
SELECT * FROM employee 
WHERE salary= (SELECT DISTINCT(salary) 
FROM employee ORDER BY salary DESC LIMIT n-1,1);
 
YouTubeGeeksforGeeks507K subscribers1. 2nd Highest Salary (Top 50 SQL Interview Questions)| GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:42•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=-6v7ctxC7yk" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This Solution is provided by Mohit.
This article is contributed by Kartik. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
p3arl
pradeepthyadi
hespacelti
sj016267
ankitmishrar
darshkaushik
DBMS-SQL
DBMS
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SQL Interview Questions
Introduction of B-Tree
CTE in SQL
SQL Trigger | Student Database
Difference between Clustered and Non-clustered index
Data Preprocessing in Data Mining
Introduction of ER Model
Introduction of DBMS (Database Management System) | Set 1
Difference between SQL and NoSQL
Difference between DDL and DML in DBMS