SQL Query To Find the Second Highest Salary

Finding the maximum salary is very easy, you just need to select the field with max function:

SELECT max(salary) FROM employees;

Sometime you need to find the 2nd highest salary or 2nd highest number from a column. Assume following data exist in employees table:

`mysql> select * from employees;+----+--------+-------------------+--------+| id | name   | email             | salary |+----+--------+-------------------+--------+|  1 | Alice  | alice@gmail.com   |  10000 ||  2 | Bob    | bob@gmail         |  20000 ||  3 | Oracle | oracle@gmail.com  |  30000 ||  4 | John   | john@example.com  |  15000 ||  5 | Chris  | chris@example.com |  25000 ||  6 | Moris  | moris@example.com |  35000 |+----+--------+-------------------+--------+6 rows in set (0.00 sec)`

If you need to select the 2nd highest salary, you can use the following query:

SELECT max(salary) FROM employees WHERE salary < (SELECT max(salary) FROM employees);

Lets see the output, when query is executed against above table:
`mysql>SELECT max(salary) FROM employees WHERE salary < (SELECT max(salary) FROM employees);+-------------+| max(salary) |+-------------+|       30000 |+-------------+1 row in set (0.00 sec)`

You see from above data, the highest salary was 3500, so 30000 is the 2nd highest value.