题目链接: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.第二高的薪水的更多相关文章

  1. SQL Server实现 LeetCode 176 第二高的薪水

    176. 第二高的薪水 SQL架构 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+- ...

  2. LeetCode 176. 第二高的薪水(MySQL版)

    0.前言 最近刷LeetCode 刷数据库题目 由于数据库课上的是SQL,而MySQL有许多自己的函数的,怕把刚学会的函数忘记 特在此记录! 1.题目 编写一个 SQL 查询,获取 Employee ...

  3. [Leetcode] 176.第二高薪水

    题目: 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 | ...

  4. [SQL]LeetCode176. 第二高的薪水 | Second Highest Salary

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

  5. MYSQL查询第二高的薪水

    编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+| Id | Salary |+----+--------+| 1 | 100 || ...

  6. LeetCode176——第二高的薪水

    题目描述 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 ...

  7. Mysql训练:第二高的薪水(IFNULL,OFFSET,LIMIT)

    编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 ...

  8. MySql_176. 第二高的薪水 + limit + distinct + null

    MySql_176. 第二高的薪水 LeetCode_MySql_176 题目描述 题解分析 代码实现 # Write your MySQL query statement below select( ...

  9. LeetCode 176. Second Highest Salary (第二高的薪水)

    题目标签: 题目给了我们一个工资表,让我们返回第二高的工资. 利用Max,把第一高的工资找到,然后利用 NOT IN,去找到第二高的工资. Java Solution: Runtime:  153ms ...

随机推荐

  1. [Mysql]一对多关系是如何发挥作用的?

    一个孩子只有一个妈妈,而一个妈妈可以有多个孩子,这是典型的一对多的关系,这里采用navicat图形化界面建立二者的关系. 第一步:创建mother表,如下图:  第二步:创建children表,在ch ...

  2. 反射中的 Method 的 getReadMethod 与 getWriteMethod 使用 【获取一个对象的所有属性字段名称和其对应的值】

    转: class反射(一),以及Method 的 getReadMethod 与 getWriteMethod 使用 2018年11月28日 17:27:42 zich77521 阅读数 788   ...

  3. maven settings.xml详解

    setting.xml配置文件 http://blog.csdn.net/u012152619/article/details/51485152 maven的配置文件settings.xml存在于两个 ...

  4. centos6 安装docker

    docker 安装要求内核大于3.10 , 而centos6 机器上内核一般是2.6 , 除了升级内核外, 还可以安装低版本的docker , 本文介绍docker 1.7的安装. 机器 环境 [ro ...

  5. Scala语法04 - 其他

  6. Java并发——volatile

    CPU的内存模型如下:

  7. GitLab - 代码仓库管理工具GitLab简介

    1 - GitLab 基于git的开源的仓库管理系统项目,使用git作为代码管理工具,并在此基础上搭建web服务,拥有与Github类似的功能. 社区版(Community Edition,CE) 企 ...

  8. 架构模式: 服务前端的后端(BFF模式)

    架构模式: 服务前端的后端(BFF模式) 上下文 让我们假设您正在构建一个使用Microservice体系结构模式的在线商店,并且您正在实现产品详细信息页面.您需要开发产品详细信息用户界面的多个版本: ...

  9. java发送邮件(一)--补充添加附件

    今天来记录一下如何使用java来发送邮件 背景 之前项目有个需求,当产品出现故障时会把情况上送给服务器,服务器发送邮件将故障产品的位置以及故障信息等告知维修人员.发送邮件的接口不是我负责的,但是有兴趣 ...

  10. Java 基础篇之lambda

    Lambda 示例 public interface Eatable { void taste(); } public interface Flyable { void fly(String weat ...