【leetcode Database】176. 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 second highest salary is 200
. If there is no second highest salary, then the query should return null
.
解析:本题采用子查询的方法来做。先取出薪水最高的,然后在所有比最高薪水低的薪水中选取最高的即可。代码如下:
# Write your MySQL query statement below
SELECT MAX(Salary) as SecondHighestSalary FROM Employee WHERE Salary < (SELECT MAX(Salary) FROM Employee);
还没有评论,来说两句吧...