In this Leetcode Second Highest Salary problem solution, we need to write a SQL query to get the second highest salary from the Employee table.

Leetcode Second Highest Salary problem solution


MYSQL solution.

select MAX(Salary) as "SecondHighestSalary"
from Employee
where Salary < (select MAX(Salary) from Employee);



TSQL solution.

SELECT
ISNULL((SELECT DISTINCT Salary as Salary
FROM Employee
ORDER BY Salary DESC OFFSET 1 ROWS FETCH NEXT 1 ROW ONLY), NULL)
AS SecondHighestSalary


Second MYSQL solution.

SELECT MAX(Salary) AS SecondHighestSalary
FROM Employee
WHERE Salary!=(SELECT MAX(Salary)FROM Employee)