Second highest salary
Mediumsqlsubquerylimit offsetnull
Return one row with a column second_highest: the second highest distinct salary in employees. If there isn't one (say, only one distinct salary), return NULL.
Tables
- employees (id INTEGER, name TEXT, salary INTEGER, department_id INTEGER)
Example 1
employees
| id | name | salary | department_id |
|---|---|---|---|
| 1 | Mona | 30000 | 1 |
| 2 | Omar | 20000 | 1 |
| 3 | Laila | 30000 | 2 |
Expected result
| second_highest |
|---|
| 20000 |
Example 2
employees
| id | name | salary | department_id |
|---|---|---|---|
| 1 | Mona | 30000 | 1 |
Expected result
| second_highest |
|---|
| NULL |
Only one salary, so the result is NULL.
Constraints
- 0 ≤ rows ≤ 1000
Submit checks your solution against 8 tests, including hidden ones.
SQL
-- Write one SELECT query. Tables: employees SELECT