题目链接: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. [Java]简单计算下一段Java代码段运行了多少秒

    long startTime = System.currentTimeMillis(); ...... long endTime = System.currentTimeMillis(); logge ...

  2. PHP学习之PHP代码的优化

    if代码块的优化 if(1===$orderState){     $status='success'; }else{     $status='error'; } return $status; 简 ...

  3. 异常值检验实战1--风控贷款年龄变量(附python代码)

    python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...

  4. js对象和jQuery对象的区别

    (1)js对象的三种基本定位方式 (A)通过ID属性:document.getElementById() (B)通过NAME属性:document.getElementsByName() (C)通过标 ...

  5. Pattern Evaluation

    对相关性patten质量的常用分析指标有以下这些 其中,X^2 跟 lift不是null-invariant的,也就是说当~A~B项较多时,这两个指标不是很可靠. 据Jiawei Han所言,Kulc ...

  6. Scrapy教程——搭建环境、创建项目、爬取内容、保存文件

    1.创建项目 在开始爬取之前,您必须创建一个新的Scrapy项目.进入您打算存储代码的目录中,运行新建命令. 例如,我需要在D:\00Coding\Python\scrapy目录下存放该项目,打开命令 ...

  7. 004-Spring boot 快速入门-项目搭建与启动、SpringBootApplication、启动图标

    一.官方地址 Spring:http://spring.io/ Spring Project:http://spring.io/projects Spring boot:https://project ...

  8. 2、form 形式,格式,形成

  9. element-ui分页当前在哪一页,刷新页面保留当前分页

  10. 安装k8s-1master多node节点

    卸载比较新的18.3版本,安装17.03版本 删除旧版本 sudo yum remove docker \ docker-client \ docker-client-latest \ docker- ...