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 ...
随机推荐
- nodejs服务端实现post请求
博客之前写过一篇php实现post请求的文章. 今天想到好久没有输出了,重新认识到输出的重要性.百般思索该写些什么?想来想去,想到了两点: 逐步熟练nodejs各种场景知识,针对mysql数据交互和f ...
- sed与awk
sed 格式 sed 选项 控制命令 文件或标准输入 sed 流程: (循环打印) sed是将文件里的每一行读入模式空间进行操作, sed选项 -r 支持正则表达 -n 取消默认打印 清空当前模式空间 ...
- 数据分析 - seaborn 模块
seaborn 模块 简述 对 matplotlib 模块进行了二次封装, 底层依旧使用还是 matplotlib 的, 但是在此基础上增加了很多的易用性模板, 更加方便使用 引用使用 import ...
- Java注解(Annotation)详解
转: Java注解(Annotation)详解 幻海流心 2018.05.23 15:20 字数 1775 阅读 380评论 0喜欢 1 Java注解(Annotation)详解 1.Annotati ...
- redis单机版无法启动java程序解决
1.注释掉 bind 127.0.0.1 2.kill-9 杀死进行 3.redis-server + 配置文件进行重启,
- Python 网络通信协议(互联网协议)
一. 操作系统基础 操作系统(Operatin System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在"裸机"上的最基本的系统软件,任何其他软件都必须在 ...
- 20190926 - macOS 下查看进程路径
首先,从 Activity Monitor 中查看进程 PID,然后使用以下命令查看. ps xuwww -p PID 另一个办法是,使用系统调用 proc_pidpath . #include &l ...
- 数据测试001:利用python连接数据库插入excel数据
数据测试001:利用python连接数据库插入excel数据 最近在做数据测试,主要是做报表系统,需要往数据库插入数据验证服务逻辑,本次介绍如何利用python脚本插入Oracle和Mysql库中: ...
- 【C/C++开发】【Java开发】JNI的替代者—使用JNA访问Java外部功能接口
JNI的替代者-使用JNA访问Java外部功能接口 1. JNA简单介绍 先说JNI(Java Native Interface)吧,有过不同语言间通信经历的一般都知道,它允许Java代码和其他语言( ...
- springboot使用elasticsearch的客户端操作eslaticsearch
一 ES客户端 ES提供多种不同的客户端: 1.TransportClient ES提供的传统客户端,官方计划8.0版本删除此客户端. 2.RestClient RestClient是官方推荐使用的 ...