The Code Developer

MySQL query to find the second highest salary

Write a MySQL query to find the second highest salary from the employees table. There are many ways to get second highest salary based upon which database you are using as different database provides different feature which can be used to find the second highest salary from the employees table.

Consider an employees table, which is having the following records −

SELECT * FROM employees;
idnamesalarydepartment
1Tom4000Technology
2Sam6000Sales
3Bob3000Technology
4Alen8000Technology
5Jack12000Marketing

For example, in above table, “Alen” has the second maximum salary as 8000.

Second maximum salary using sub query and IN clause

SELECT MAX(salary) FROM employees WHERE salary NOT IN ( SELECT Max(salary) FROM employees);
MAX(salary)
8000

Using sub query and < operator instead of IN clause

SELECT MAX(salary) From employees WHERE salary < ( SELECT Max(salary) FROM employees);
MAX(salary)
8000

Using the LIMIT clause in query

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