Second Highest Salary
Task: Write a solution to find the second highest salary from the Employee table. If there is no second highest salary, return null.
Table: Employee
+-------------+------+
| Column Name | Type |
+-------------+------+
| id | INT |
| salary | INT |
+-------------+------+
Input: Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
Output:
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+ View Reference Solution & Explanation ▼
This solution uses aggregate functions to compute a single value from the data and a subquery as a derived table or inline value.
SELECT MAX(salary) AS SecondHighestSalary FROM Employee WHERE salary < (SELECT MAX(salary) FROM Employee);