Second Highest Salary

medium sorting database

Problem

Write a SQL query to report the second highest distinct salary from Employee. If there is no such salary, return NULL.

SQL: SELECT (SELECT DISTINCT salary FROM Employee ORDER BY salary DESC LIMIT 1 OFFSET 1) AS SecondHighestSalary;

Inputsalaries = [100, 200, 300]
Output200
Sorted desc: [300, 200, 100]; second is 200.

SELECT (SELECT DISTINCT salary FROM Employee
        ORDER BY salary DESC LIMIT 1 OFFSET 1) AS SecondHighestSalary;
Time: O(n log n) Space: O(n)