LeeCode——Second Highest Salary
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的更多相关文章
- [LeetCode] Department Highest Salary 系里最高薪水
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- [LeetCode] Nth Highest Salary 第N高薪水
Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...
- [LeetCode] Second Highest Salary 第二高薪水
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
- 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 ...
- 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. +----+ ...
- TSQL Beginners Challenge 1 - Find the second highest salary for each department
很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...
- Leetcode 176. Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
- find the Nth highest salary(寻找第N高薪水)
Suppose that you are given the following simple database table called Employee that has 2 columns na ...
- [SQL]LeetCode176. 第二高的薪水 | Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
随机推荐
- configure.ac文件和Makefile.am文件 编译
在编译安装openvpn 项目时遇到,其编译过程如下:生成 configure 可执行文件 make && make install ; . aclocal . autoconf . ...
- CentOS 7 Apache 绑定域名和网站
CentOS 7 Apache 绑定域名和网站适用场景一台服务器,运行有多个网站,每个网站都希望用户直接通过二级域名来访问,而不是同一个域名通过子目录来访问 配置过程确定自己的 Apache 服务器的 ...
- c#_sort排序函数的返回值
C# List.Sort函数的返回值 值 含义 小于零 left在right的前面 零 位置不变 大于零 right在left的前面 示例: 本测试结果在unity3d 和纯C#环境下执行. List ...
- golang数据结构和算法之StackLinkedList链表堆栈
会了上一个,这个就差不离了. StackLinkedList.go package StackLinkedList type Node struct { data int next *Node } t ...
- 19.Java基础_封装概念
- 5. Go语言—数据类型
一.变量作用域 在函数内部声明的变量叫做局部变量,声明周期仅限于函数内部. 在函数外部声明的变量叫做全局变量,声明周期作用于整个包,如果是大写的,则作用于整个程序. 二.类型 1. 类型转换 ty ...
- C++学习三 模板类出错总结(Missing template arguments before 'L')
一.模板类的说明 模板类有一个好处是可以放宽你输入的数据类型. 比如有这样的一个函数: int add(int x, int y) { return x+y; } 这个函数对于int类型的x,y才适合 ...
- Pwn-level3(x64)
题目地址 https://dn.jarvisoj.com/challengefiles/level3_x64.rar.8c74c402b190ac3fbef5a9ae540c40de 跟level3差 ...
- CME Futures & Options Order Book
http://algo-logic.com/futures-options-orderbook Algo-Logic Systems’ Futures & Options (F&O) ...
- Object的多种方法
Object.entries() Object.entries() 方法返回一个给定对象自身可枚举属性的键值对数组,其排列与使用 for...in 循环遍历该对象时返回的顺序一致(区别在于 for-i ...
