Easy understanding solution


  • 1
    S
    SELECT MAX(Salary) AS SecondHighestSalary FROM Employee WHERE Salary < (SELECT MAX(Salary) FROM Employee)
    

    At first I tried this:

    SELECT Salary AS SecondHighestSalary FROM Employee LIMIT 2,1
    

    but not work. Anyone can fix it?


  • 0
    E

    Well when you say LIMIT 2,1 you basically tell SQL that you want 1 row from offset 2, which means you want row number 3 from the results. I don't think that is what you want....

    Here is how I did it:

    SELECT 
        IF( COUNT( A.Salary ) > 1, MIN( A.Salary ), NULL ) as SecondHighestSalary
    FROM
        (SELECT DISTINCT
            Salary
        FROM 
            Employee
        ORDER BY
            Salary DESC
        LIMIT 0,2) A
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.