- Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathSecond_highest_salary.sql
29 lines (25 loc) · 704 Bytes
/
Second_highest_salary.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
*/
SELECT IFNULL(
(
SELECT DISTINCT(Salary)
FROM employee
ORDER BYemployee.SalaryDESC
LIMIT1
OFFSET 1
), NULL) AS"SecondHighestSalary"