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 maximum salary from the employees table.
MySQL query to find the second highest salary
Consider an employees table, which is having the following records −
SELECT * FROM employees;
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);
Using sub query and < operator instead of IN clause
SELECT MAX(salary) From employees WHERE salary < ( SELECT Max(salary) FROM employees);
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;