Solution not returning null but is empty

  • 0

    from Employee order by Salary desc limit 2,1;

    this is my solution ; the output is expected but if there are no rows it returns empty instead of null. how can i improve the query ?

  • 0
    SELECT NULLIF(MIN(e1.Salary), MAX(e1.Salary)) AS SecondHighestSalary
    FROM (SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT 2) e1;

    Never thought of NULLIF function! I modified few parts of your code, and it worked and should be easily understood. Basically it compares the highest value against the lowest value in the new table e1. If MIN = MAX, then return NULL, else return MIN. Let me know if you have any further questions.

Log in to reply

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