难度系数: 中等
编写一个 SQL 查询,获取并返回 Employee 表中第二高的薪水 。如果不存在第二高的薪水,查询应该返回 null 。
SQL结构
1 2 3 4 5
| Create table If Not Exists Employee (id int, salary int) Truncate table Employee insert into Employee (id, salary) values ('1', '100') insert into Employee (id, salary) values ('2', '200') insert into Employee (id, salary) values ('3', '300')
|
表: Employee
1 2 3 4 5 6 7 8
| + | Column Name | Type | + | id | int | | salary | int | + id 是这个表的主键。 表的每一行包含员工的工资信息。
|
示例 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 输入: Employee 表: + | id | salary | + | 1 | 100 | | 2 | 200 | | 3 | 300 | + 输出: + | SecondHighestSalary | + | 200 | +
|
示例 2:
1 2 3 4 5 6 7 8 9 10 11 12 13
| 输入: Employee 表: + | id | salary | + | 1 | 100 | + 输出: + | SecondHighestSalary | + | null | +
|
解法:
1 2 3
| SELECT max(Salary) SecondHighestSalary FROM Employee WHERE Salary < (SELECT max(Salary) FROM Employee)
|
原题链接:https://leetcode.cn/problems/second-highest-salary