LeetCode176——第二高的薪水
2024-09-03 16:09:58
题目描述
编写一个 SQL 查询,获取 Employee
表中第二高的薪水(Salary
) 。
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
例如上述 Employee
表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null
。
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
思路
- 排序,取出排名第二的值
select Salary from Employee
order by Salary desc
limit 1, 1;
- group by 过滤掉相同薪水
select Salary from Employee
group by Salary
order by Salary desc
limit 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;
最新文章
- RedisUtil 工具类
- ORACLE 移动数据文件 控制文件 重做日志文件
- 突袭HTML5之SVG 2D入门1 - SVG综述////////////////zzzzzzzz
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
- 20145129 《Java程序设计》第5周学习总结
- 史上最全github使用方法:github入门到精通--备用
- linux zombie process相关学习
- 借助OpenOffice实现office转pdf(Java)的.exe小程序
- java list<;string>;集合 传递值给js的数组
- CSS组件
- python3获取网页天气预报信息并打印
- tomcat守护相关
- 【原创 深度学习与TensorFlow 动手实践系列 - 3】第三课:卷积神经网络 - 基础篇
- JSP学习笔记(2)-JSP语法
- [Codility] CommonPrimeDivisors
- 140725暑期培训.txt
- Linux 开机、重启和用户登录注销、用户管理、用户组
- windows下安装jmeter
- js图片转换为base64
- node.js 开发命令行工具 发布npm包
热门文章
- RemoveError: &#39;setuptools&#39; is a dependency of conda and cannot be removed from conda&#39;s operating environment.
- DOM是什么
- APIO2019 游记
- navicat提示无法连接解决办法
- SpringBoot上传文件到本服务器 目录与jar包同级问题
- Spring 注解@Value详解
- sql查询条件参数为空
- SpringBoot激活profiles
- dubbo源码分析- 集群容错之Cluster(一)
- Vscode 修改主题颜色