LeetCode:176.第二高的薪水
题目链接:https://leetcode-cn.com/problems/second-highest-salary/
题目
编写一个 SQL 查询,获取 Employee
表中第二高的薪水(Salary)
。
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
例如上述 Employee
表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null
。
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
解答
第一次解答报错。。。
---- oracle ----
/* Write your PL/SQL query statement below */
select Salary as SecondHighestSalary
from
(
select Salary,
rownum as rn
from
(
select Salary
from Employee
order by Salary desc
) b
) c
where c.rn = 2 ---- 执行报错 当数据只有1行时 执行为空 而不是null
第二次解答。。。依旧报错
---- oracle ----
/* Write your PL/SQL query statement below */
select Salary as SecondHighestSalary
from
(
select Salary,
row_number() over(order by Salary desc) as rn
from Employee
) c
where c.rn = 2 ---- 依旧报错
参考评论之后,再改进。。
解答一
---- oracle ----
/* Write your PL/SQL query statement below */
select max(Salary) as SecondHighestSalary
from Empolyee
where Salary <> (select max(Salary) from Employee) ---- 672ms
解答二
修改第一次出错的版本,添加判断后再次尝试。。。
---- oracle ----
select Salary as SecondHighestSalary
from
(
select Salary
from
(
select Salary,
rownum as rn
from
(
select distinct(Salary)
from Employee
order by Salary desc
) b
) c
where c.rn = 2
union all
select null from dual
)
where rownum = 1 -- 未针对薪水进行去重操作 增加distinct
-- 不添加distinct还执行报错 添加之后通过
---- 861ms
解答三
好久没用过MySQL
,用法都忘光了。。
使用子查询和LIMIT
子句
---- MySQL ----
select
(
select distinct Salary
from Employee
order by Salary desc
limit 1 offset 1
) as SecondHighestSalary; ---- 112ms 好快
解答四
为了解决NULL
的问题,可以使用IFNULL
函数
---- MySQL ----
select
ifnull(
(
select distinct Salary
from Employee
order by Salary desc
limit 1 offset 1
),
NULL) as SecondHighestSalary ---- 108ms
思考
去重、排序、获取第二个、返回为NULL
时设置返回NULL
注意
oracle中rownum
的使用必须要包含第一条记录,也就是类似rownum <= 10
,所以不能使用rownum = 2
提取第2行数据,必须利用嵌套查询实现。
group by
的速度比distinct
速度要快。
LeetCode:176.第二高的薪水的更多相关文章
- SQL Server实现 LeetCode 176 第二高的薪水
176. 第二高的薪水 SQL架构 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+- ...
- LeetCode 176. 第二高的薪水(MySQL版)
0.前言 最近刷LeetCode 刷数据库题目 由于数据库课上的是SQL,而MySQL有许多自己的函数的,怕把刚学会的函数忘记 特在此记录! 1.题目 编写一个 SQL 查询,获取 Employee ...
- [Leetcode] 176.第二高薪水
题目: 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 | ...
- [SQL]LeetCode176. 第二高的薪水 | Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
- MYSQL查询第二高的薪水
编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+| Id | Salary |+----+--------+| 1 | 100 || ...
- LeetCode176——第二高的薪水
题目描述 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 ...
- Mysql训练:第二高的薪水(IFNULL,OFFSET,LIMIT)
编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 ...
- MySql_176. 第二高的薪水 + limit + distinct + null
MySql_176. 第二高的薪水 LeetCode_MySql_176 题目描述 题解分析 代码实现 # Write your MySQL query statement below select( ...
- LeetCode 176. Second Highest Salary (第二高的薪水)
题目标签: 题目给了我们一个工资表,让我们返回第二高的工资. 利用Max,把第一高的工资找到,然后利用 NOT IN,去找到第二高的工资. Java Solution: Runtime: 153ms ...
随机推荐
- PCD(点云数据)文件格式
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=54 为什么用一种新的文件格式? PCD文件格式并非白费力气地做重复工作,现有 ...
- centos7最小安装怎么安装防火墙
CentOS 7.0默认使用的是firewall作为防火墙,需要事先关闭. 关闭firewall: 1 2 3 systemctl stop firewalld.service systemctl d ...
- [CDH] Process data: integrate Spark with Spring Boot
c 一.Spark 统计计算 简单统计后写入Redis. /** * 订单统计和乘车人数统计 */ object OrderStreamingProcessor { def main(args: Ar ...
- Kafka消息流处理
- redis开启持久化、redis 数据备份与恢复
redis持久化介绍 https://segmentfault.com/a/1190000015897415 1. 开启aof持久化.以守护进程启动.远程访问先把配置文件拷贝一份到/etc/redi ...
- composer install与composer update的区别
1.composer install install 命令从当前目录读取 composer.json 文件,处理了依赖关系,并把其安装到 vendor 目录下. php composer.phar i ...
- 禁用composer update命令
composer update 命令会直接把依赖的包更新到最新,会导致整个项目代码都是没有经过测试的不稳定代码 所以一定要禁止使用该命令. 正确的做法是: 更新指定的包到指定的版本(高版本的包也可以更 ...
- AES 加密算法的原理详解
AES 加密算法的原理详解 本教程摘选自 https://blog.csdn.net/qq_28205153/article/details/55798628 的原理部分. AES简介 高级加密标准( ...
- k8s 网络模型解析之实践
一. 实践说明 首先我们先创建一组资源,包括一个deployment和一个service apiVersion: apps/v1 kind: Deployment metadata: name: ng ...
- Exchange2010---反垃圾邮件配置
Exchange2010---反垃圾邮件配置 Exchange2010---反垃圾邮件配置 本文以Exchange Server 2010作为反垃圾邮件配置实例为例. 其实,在微软发布的Exc ...