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 ?
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.