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 query should return 200 as the second highest salary.
If there is no second highest salary, then the query should return null. +---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+

此题的难点是:

  • 针对可能存在的重复数值,选出唯一的数值(使用distinct);
  • 选取第二个数值,则需要使用limit,offset确定数值;
  • 查询的数值可能为不存在,此时就需要返回NULL(使用IFNULL).

答题如下所示:

# Write your MySQL query statement below
select
ifnull(
(select distinct Salary from Employee order by Salary desc limit 1,1),
NULL
)
as SecondHighestSalary

PS:

如果您觉得我的文章对您有帮助,请关注我的微信公众号,谢谢!

LeeCode——Second Highest Salary的更多相关文章

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

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

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

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

  3. [LeetCode] Second Highest Salary 第二高薪水

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

  4. LeetCode 176 Second Highest Salary mysql,select 嵌套 难度:1

    https://leetcode.com/problems/second-highest-salary/ Write a SQL query to get the second highest sal ...

  5. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  6. TSQL Beginners Challenge 1 - Find the second highest salary for each department

    很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...

  7. Leetcode 176. Second Highest Salary

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

  8. find the Nth highest salary(寻找第N高薪水)

    Suppose that you are given the following simple database table called Employee that has 2 columns na ...

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

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

随机推荐

  1. [20190929]bash使用bc计算的相关问题.txt

    [20190929]bash使用bc计算的相关问题.txt --//快放假没什么事情,使用bash写一些小程序,转化number到oracle number编码,使用bc计算功能,发现一些小问题--/ ...

  2. [20190510]rman备份的疑问7.txt

    [20190510]rman备份的疑问7.txt --//上午测试rman备份时备份文件大小回缩的测试.链接:--//http://blog.itpub.net/267265/viewspace-26 ...

  3. 组装数据- 对象里面是key:value, value里面是数组的形式,如 {key:[aa,bb], key:[cc,dd]}

    组合后 对象里面是key:value,value里面是数组的形式{key:[aa,bb], key:[cc,dd]} var chinaGeoCoordMap = { '无锡市': [121.4648 ...

  4. 【洛谷P2494】 [SDOI2011]保密(分数规划+最小割)

    洛谷 题意: 题意好绕好绕...不想写了. 思路: 首先类似于分数规划做法,二分答案得到到每个点的最小危险度. 然后就是在一个二分图中,两边撤掉最少的点(相应代价为上面算出的危险度)及相应边,使得中间 ...

  5. jmeter beanshell断言接口自动化实例

    一.JMeter介绍 Apache JMeter是一款优秀的开源性能测试工具,在国外无论是在性能测试还是接口测试领域都有着非常高的使用率,但由于本身没有完善的中文文档以及典型开源工具特点(界面不美观) ...

  6. mongodb的数据库,集合,数据可介绍。

    我们知道,在关系型数据库里面有数据库.数据表.表里面是一行一行的数据.而mongodb是非关系型数据库,它有的是数据库.集合.文档,分别对应关系型里面的数据库.数据表.和表里面一行一行的数据.在mon ...

  7. pwn-Stack Overflow

    地址 https://cgctf.nuptsast.com/challenges#Pwn 先观察一下,是一个32位的程序,而且只开了NX保护 用IDA看看伪代码,重点在message和pwnme这两个 ...

  8. web-神盾局的秘密

    题目地址 http://web.jarvisoj.com:32768/ 首页是一张图片 查看源码,看到了一些猫腻,showing.php     c2hpZWxkLmpwZw==是base64编码   ...

  9. [C3] Andrew Ng - Neural Networks and Deep Learning

    About this Course If you want to break into cutting-edge AI, this course will help you do so. Deep l ...

  10. Markdown & LaTex 常用语法

    目录 blog 的目录 博客园自带目录 用 javascript 自定义目录 主标题 副标题 h1,一级标题 h2,二级标题 h3,三级标题 注释 常用的符号及文本形式 如果你想在markdown中文 ...