1. Second Highest Salary

Easy
#Subqueries#Aggregations#LIMIT#Window Functions

Problem Description

Write a SQL query to find the second highest distinct salary from the **Employee** table. If there is no second highest salary (for instance, if there is only one distinct salary or the table is empty), the query should report `NULL`. ### Example 1 Given the `Employee` table below: | id | salary | | :--- | :--- | | 1 | 100 | | 2 | 200 | | 3 | 300 | **Expected Output:** | SecondHighestSalary | | :--- | | 200 | ### Example 2 Given the `Employee` table with duplicate or single salaries: | id | salary | | :--- | :--- | | 1 | 100 | **Expected Output:** | SecondHighestSalary | | :--- | | NULL |

Database Schema

undefined

Sample SQL Solution

-- Write your SQL query to find the second highest salary
SELECT MAX(salary) AS SecondHighestSalary
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee);