Write a SQL query to get the second highest salary from the Employee table.

+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+

For example, given the above Employee table, the second highest salary is 200. If there is no second highest salary, then the query should return null.

这道题让我们找表中某列第二大的数,这道题有很多种解法,先来看一种使用Limit和Offset两个关键字的解法,MySQL中Limit后面的数字限制了我们返回数据的个数,Offset是偏移量,那么如果我们想找第二高薪水,我们首先可以先对薪水进行降序排列,然后我们将Offset设为1,那么就是从第二个开始,也就是第二高薪水,然后我们将Limit设为1,就是只取出第二高薪水,如果将Limit设为2,那么就将第二高和第三高薪水都取出来:

解法一:

SELECT Salary FROM Employee GROUP BY Salary
UNION ALL (SELECT NULL AS Salary)
ORDER BY Salary DESC LIMIT 1 OFFSET 1;

我们也可以使用Max函数来做,这个返回最大值,逻辑是我们取出的不包含最大值的数字中的最大值,即为第二大值:

解法二:

SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN
(SELECT MAX(Salary) FROM Employee);

下面这种方法和上面基本一样,就是用小于号<代替了Not in关键字,效果相同:

解法三:

SELECT MAX(Salary) FROM Employee
Where Salary <
(SELECT MAX(Salary) FROM Employee);

最后来看一种可以扩展到找到第N高的薪水的方法,只要将下面语句中的1改为N-1即可,第二高的薪水带入N-1就是1,下面语句的逻辑是,假如我们要找第二高的薪水,那么我们允许其中一个最大值存在,然后在其余的数字中找出最大的,即为整个的第二大的值;

解法四:

SELECT MAX(Salary) FROM Employee E1
WHERE 1 =
(SELECT COUNT(DISTINCT(E2.Salary)) FROM Employee E2
WHERE E2.Salary > E1.Salary);

参考资料:

https://leetcode.com/discuss/47041/very-very-simple-solution

https://leetcode.com/discuss/42849/general-solution-not-using-max

https://leetcode.com/discuss/21751/simple-query-which-handles-the-null-situation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Second Highest Salary 第二高薪水的更多相关文章

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

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

  2. [LeetCode] Department Highest Salary 系里最高薪水

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  3. mysql 第二高薪水

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

  4. [LeetCode] Nth Highest Salary 第N高薪水

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

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

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

  6. [LeetCode] Department Highest Salary -- 数据库知识(mysql)

    184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...

  7. [LeetCode]-DataBase-Nth Highest Salary

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

  8. LeetCode——Department Highest Salary(花式使用IN以及GROUP BY)

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  9. LeetCode——Nth Highest Salary

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

随机推荐

  1. ldap部署相关,ldap双机\LAM配置管理\ldap备份还原

    前言 接之前我的文章,django+ldap+memcache实现单点登录+统一认证 就单点登录实现过程进行详细记录,ldap是一切的基础,可以把它理解成一个读强写弱的文件类型数据库,统一认证我们通过 ...

  2. Button 模板和样式

    <Style TargetType="{x:Type Button}"> <Setter Property="FontFamily" Valu ...

  3. C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

    我们知道,企业号主要是面向企业需求而生的,因此内部消息的交流显得非常重要,而且发送.回复消息数量应该很可观,对于大企业尤其如此,因此可以结合企业号实现内部消息的交流.企业号具有关注安全.消息无限制等特 ...

  4. STL的std::find和std::find_if

    std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...

  5. CentOS 7 下安装redis步骤

    1. 从redis官网下载redis源码,本例安装在/usr/opt下 [root@localhost opt]# pwd /usr/opt [root@localhost opt]# wget ht ...

  6. 9.2.1 .net framework下的MVC 控件的封装(上)

    在写.net core下mvc控件的编写之前,我先说一下.net framework下我们MVC控件的做法. MVC下控件的写法,主要有如下三种,最后一种是泛型的写法,mvc提供的控件都是基本控件. ...

  7. javascript 表单

    在HTML中,表单是由<form>元素来组成的.在js中,表单对应的则是HTMLFormElement类型.它和其他HTML元素一样具有相同的默认属性.下面是HTMLFormElement ...

  8. CSS3鼠标悬停图片上浮显示描述代码

    效果:http://hovertree.com/texiao/css3/20/ 效果图: 代码如下: <!doctype html> <html lang="zh" ...

  9. css基础

    一. web标准化 (1).内容与样式,行为分离 (2).html用来定义语义内容,以及内容的结构 (xhtml) (3).xhtml标准 a.xhtml 必须强制指定文档类型 DocType,HTM ...

  10. When I see you again(加密原理介绍,代码实现DES、AES、RSA、Base64、MD5)

    关于网络安全的数据加密部分,本来打算总结一篇博客搞定,没想到东西太多,这已是第三篇了,而且这篇写了多次,熬了多次夜,真是again and again.起个名字:数据加密三部曲,前两部链接如下: 整体 ...