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. configure.ac文件和Makefile.am文件 编译

    在编译安装openvpn 项目时遇到,其编译过程如下:生成 configure 可执行文件 make && make install ; . aclocal . autoconf . ...

  2. CentOS 7 Apache 绑定域名和网站

    CentOS 7 Apache 绑定域名和网站适用场景一台服务器,运行有多个网站,每个网站都希望用户直接通过二级域名来访问,而不是同一个域名通过子目录来访问 配置过程确定自己的 Apache 服务器的 ...

  3. c#_sort排序函数的返回值

    C# List.Sort函数的返回值 值 含义 小于零 left在right的前面 零 位置不变 大于零 right在left的前面 示例: 本测试结果在unity3d 和纯C#环境下执行. List ...

  4. golang数据结构和算法之StackLinkedList链表堆栈

    会了上一个,这个就差不离了. StackLinkedList.go package StackLinkedList type Node struct { data int next *Node } t ...

  5. 19.Java基础_封装概念

  6. 5. Go语言—数据类型

    一.变量作用域 在函数内部声明的变量叫做局部变量,声明周期仅限于函数内部. 在函数外部声明的变量叫做全局变量,声明周期作用于整个包,如果是大写的,则作用于整个程序. 二.类型 1. 类型转换 ​ ty ...

  7. C++学习三 模板类出错总结(Missing template arguments before 'L')

    一.模板类的说明 模板类有一个好处是可以放宽你输入的数据类型. 比如有这样的一个函数: int add(int x, int y) { return x+y; } 这个函数对于int类型的x,y才适合 ...

  8. Pwn-level3(x64)

    题目地址 https://dn.jarvisoj.com/challengefiles/level3_x64.rar.8c74c402b190ac3fbef5a9ae540c40de 跟level3差 ...

  9. CME Futures & Options Order Book

    http://algo-logic.com/futures-options-orderbook Algo-Logic Systems’ Futures & Options (F&O) ...

  10. Object的多种方法

    Object.entries() Object.entries() 方法返回一个给定对象自身可枚举属性的键值对数组,其排列与使用 for...in 循环遍历该对象时返回的顺序一致(区别在于 for-i ...