Find 2nd Highest Salary in MySQL
Method 1: Using LIMIT
This is the simplest and most commonly used method.
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
Note: This returns the second highest unique salary.
Method 2: Using Subquery
This method is easy to understand and works reliably.
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Method 3: Using DENSE_RANK() (MySQL 8+)
This method is useful for advanced queries and interviews.
SELECT salary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk = 2;
Notes
DISTINCTremoves duplicate salaries- If all salaries are the same, the result may be
NULL - Assumes table structure:
employees(salary)