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| Column | Type |
|---|---|
| id | INT |
| name | TEXT |
| salary | INT |
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:
55000Explanation:
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:
65000Explanation:
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:
NULLExplanation:
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