second-highest-salary.sql (531B)
1 --Speed: 436ms Beats: 51.92% 2 3 --This DISTINCT part is used in case two employees or more have the same salary but we want to find the next highest. 4 (SELECT DISTINCT E.salary as SecondHighestSalary 5 FROM Employee as E 6 ORDER BY E.salary DESC 7 --The first 1 is the offset and the second 1 is the number of rows to return. By doing this it offsets by 1 and selects one row meaning the second row. 8 LIMIT 1,1) 9 --This is used to return null if there are no records that are found as the second highest salary. 10 UNION 11 (SELECT NULL) 12 LIMIT 1;