题目描述

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+

思路

  1. 排序,取出排名第二的值
select Salary from Employee
order by Salary desc
limit 1, 1;
  1. group by 过滤掉相同薪水
select Salary from Employee
group by Salary
order by Salary desc
limit 1, 1;
  1. 当不存在第二高的薪水时,会返回空而不是 null,做个是否为 null 的判断
select
ifnull(
(select Salary from Employee group by Salary order by Salary desc limit 1, 1),
null
) as SecondHighestSalary;

可以简写为

select
(select Salary from Employee group by Salary order by Salary desc limit 1, 1)
as SecondHighestSalary;

最新文章

  1. RedisUtil 工具类
  2. ORACLE 移动数据文件 控制文件 重做日志文件
  3. 突袭HTML5之SVG 2D入门1 - SVG综述////////////////zzzzzzzz
  4. Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
  5. 20145129 《Java程序设计》第5周学习总结
  6. 史上最全github使用方法:github入门到精通--备用
  7. linux zombie process相关学习
  8. 借助OpenOffice实现office转pdf(Java)的.exe小程序
  9. java list<string>集合 传递值给js的数组
  10. CSS组件
  11. python3获取网页天气预报信息并打印
  12. tomcat守护相关
  13. 【原创 深度学习与TensorFlow 动手实践系列 - 3】第三课:卷积神经网络 - 基础篇
  14. JSP学习笔记(2)-JSP语法
  15. [Codility] CommonPrimeDivisors
  16. 140725暑期培训.txt
  17. Linux 开机、重启和用户登录注销、用户管理、用户组
  18. windows下安装jmeter
  19. js图片转换为base64
  20. node.js 开发命令行工具 发布npm包

热门文章

  1. RemoveError: 'setuptools' is a dependency of conda and cannot be removed from conda's operating environment.
  2. DOM是什么
  3. APIO2019 游记
  4. navicat提示无法连接解决办法
  5. SpringBoot上传文件到本服务器 目录与jar包同级问题
  6. Spring 注解@Value详解
  7. sql查询条件参数为空
  8. SpringBoot激活profiles
  9. dubbo源码分析- 集群容错之Cluster(一)
  10. Vscode 修改主题颜色