加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > C语言 > 正文

176. Second Highest Salary

发布时间:2020-12-15 01:15:00 所属栏目:C语言 来源:网络整理
导读: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 ret
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 |
+---------------------+

从表里面查询出第二大数值

首先要用distinct去重,因为第二大的数值可能是有重复的,然后从高到低排序,取第二个数据就是。但是要注意的是可能表里面只有一条数据,如果想要返回NULL的话,可以在最外层再加一个select查询

select 
 (
    select distinct salary from Employee order by salary desc limit 1,1
 ) as SecondHighestSalary

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读