【leetcode Database】176. Second Highest Salary

叁歲伎倆 2022-09-24 06:25 223阅读 0赞

题目:

Write a SQL query to get the second highest salary from the Employee table.

  1. +----+--------+
  2. | Id | Salary |
  3. +----+--------+
  4. | 1 | 100 |
  5. | 2 | 200 |
  6. | 3 | 300 |
  7. +----+--------+

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.

解析:本题采用子查询的方法来做。先取出薪水最高的,然后在所有比最高薪水低的薪水中选取最高的即可。代码如下:

  1. # Write your MySQL query statement below
  2. SELECT MAX(Salary) as SecondHighestSalary FROM Employee WHERE Salary < (SELECT MAX(Salary) FROM Employee);

发表评论

表情:
评论列表 (有 0 条评论,223人围观)

还没有评论,来说两句吧...

相关阅读