In this article, we will look at a popular question **How to find 2nd Highest Salary in SQL** asked in many interviews.
Table of Contents
Problem
Given a Table of Employees with Employee Details we have to find the Employee with the 2nd Highest Salary. We have to print the Employee details of the Employee or we can just print the Salary.Let us Consider a Table Employees with the following attributes and records:
ID | NAME | SALARY |
1 | Amanda Jones | 12000 |
2 | Steve Madden | 15000 |
3 | Robert Henry | 10000 |
4 | Shawn Adams | 10000 |
5 | Luke Shaw | 11000 |
The Employees table contains Attributes ID
, NAME
and SALARY
for each Employee
. In the given relation, the Employee ‘Amanda Jones’ has 2nd Highest Salary with value 12000.
Now let us look at the way to find the Employee with 2nd Highest Salary using SQL query. Before that let us first create the schema for the table above.
1 2 3 4 5 6 7 8 |
CREATE TABLE Employees ( ID INTEGER, NAME VARCHAR(20), SALARY INTEGER ); |
Let us enter some sample records into our Table Employees.
1 2 3 4 5 6 7 |
INSERT INTO Employees VALUES(1,'Amanda Jones',12000); INSERT INTO Employees VALUES(2,'Steve Madden',15000); INSERT INTO Employees VALUES(3,'Robert Henry',10000); INSERT INTO Employees VALUES(4,'Shawn Adams',10000); INSERT INTO Employees values(5,'Luke Shaw',11000); |
Note: If using SQL Server, run GO Command after
CREATE TABLE
Block and all INSERT Command operations . One more point to note if we make theID
as key identity we don’t need to provideID
explicitly in each INSERT Statement; just the name and salary needs to be provided..
We will discuss different approaches to provide solution for the problem statement.
Solution
There are multiple ways to find second highest salary in SQL.
Using Subquery to find second highest salary of Employee
First, we find the employee with highest salary. To do this we run this query:
1 2 3 |
SELECT MAX(SALARY) FROM Employees; |
This will give us the Maximum Salary; we can further nest this query to a subquery to find the Second Highest Salary. This query will work on MYSQL, ORACLE as well as SQL Server :
1 2 3 |
SELECT MAX(SALARY) FROM Employees WHERE SALARY < (SELECT MAX(SALARY) FROM Employees); |
For the table above, this query will give Output:
1 2 3 |
12000 |
If we want to print the Employee Details having the 2nd Highest Salary, we execute this query (only in MYSQL) :
1 2 3 |
SELECT ID,NAME,SALARY FROM Employees WHERE SALARY < (SELECT MAX(salary) FROM Employees) ORDER BY SALARY DESC LIMIT 1; |
Output:
The ORDER BY DESC
clause is used to arranges the employees in Order of Decreasing Salaries and then LIMIT 1
gets us the first value from that arrangement. The LIMIT keyword is used mainly in MYSQL Servers only.
We can also use the NOT IN Clause instead of comparing the salary between two sets. The query is:
1 2 3 |
SELECT MAX(SALARY) FROM Employees WHERE SALARY NOT IN (SELECT MAX(SALARY) FROM Employees); |
Output:
1 2 3 |
12000 |
Just like we used MAX
Function, we can also use the MIN
function in MYSQL Server to find out 2nd Highest Salary. The query would be like this :
1 2 3 |
SELECT MIN(SALARY) FROM (SELECT SALARY FROM Employees ORDER BY SALARY DESC LIMIT 2); |