easy
SQL
Second highest salary (SQL)
Table Employee(id, name, salary). Write a query that returns the second highest distinct salary. If only one distinct salary exists, return NULL.
Sample
salaries: 100, 200, 200, 300 result: 200
Hint
Use DISTINCT + ORDER BY salary DESC + LIMIT 1 OFFSET 1, or a subquery with MAX excluding the top salary.
Starter code
-- Employee(id INT, name VARCHAR, salary INT)
-- Return the second highest distinct salary
SELECT ...Solution
-- Option A: LIMIT / OFFSET (MySQL / Postgres)
SELECT DISTINCT salary AS SecondHighestSalary
FROM Employee
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
-- Option B: subquery (works widely)
SELECT MAX(salary) AS SecondHighestSalary
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee);