Description

Write a SQL query to find the second highest salary from the employees table. If there is no second highest salary, return NULL. **Output columns (in order):** salary (or NULL if not found)

Table:employees
ColumnType
idINT
nameTEXT
salaryINT

Examples

Input:CREATE TABLE employees (id INT, name TEXT, salary INT); INSERT INTO employees VALUES (1, 'Alice', 50000), (2, 'Bob', 60000), (3, 'Charlie', 55000);
Output:55000
Explanation:

Bob has the highest salary at 60000, Charlie has 55000, and Alice has 50000. The second highest distinct salary is 55000 (Charlie's salary). The query must handle finding the second highest value correctly.

Input:CREATE TABLE employees (id INT, name TEXT, salary INT); INSERT INTO employees VALUES (1, 'David', 75000), (2, 'Emma', 75000), (3, 'Frank', 65000);
Output:65000
Explanation:

When multiple employees share the highest salary (75000), the second highest is the next distinct salary value (65000)

Input:CREATE TABLE employees (id INT, name TEXT, salary INT); INSERT INTO employees VALUES (1, 'Grace', 80000);
Output:NULL
Explanation:

Grace is the only employee with salary 80000. Since there's only one salary value in the table, there is no second highest salary to return, so the result is NULL. This edge case must be handled properly.

Constraints

  • Handle edge cases

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!