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. linux 安装PostgreSQL12

    一.安装步骤 1.设置保存安装包的目录 # cd /usr/local/src 2.开始下载源包 # wget https://ftp.postgresql.org/pub/source/v12.1/ ...

  2. python 连接数据库,查询结果写入数据到excel

    使用Python链接数据库查询数据,并将查询结果写入到Excel中,实现方法上主要有两步,第一,查数据,第二,写Excel. 一.导入需要的包 import time import xlwt from ...

  3. Jinkins自动构建

    Jinkins自动构建 1.项目添加 点击左侧操作栏“新建”,填写项目基础信息,如下图: 2. General配置 2.1 丢弃旧的构建 注:此处勾选丢弃旧的构建,默认天数为1,最大个数建议填写3-5 ...

  4. 各版本mysql修改root密码

    今天在安装mysql5.7.8的时候遇到一些问题,首当其冲便的是初始root密码的变更,特分享解决方法如下: 1.mysql5.7会生成一个初始化密码,而在之前的版本首次登陆不需要登录. shell& ...

  5. skkyk:题解 洛谷P2420 【让我们异或吧】lca+xor前缀和

    刚学了LCA,写篇题解巩固一下 首先题目有误: (A是否是男生 )xor( B是否是男生)=A和B是否能够成为情侣,这句话显然是错误的qwq 对于这道题,容易看出,对于待处理的两个点,只要我们找到他的 ...

  6. AtCoder Beginner Contest 147

    A - Blackjack #include <bits/stdc++.h> int main() { int a, b, c; scanf("%d%d%d", &am ...

  7. Luogu P4068 [SDOI2016]数字配对

    反正现在做题那么少就争取做一题写一题博客吧 看到题目发现数字种类不多,而且结合价值的要求可以容易地想到使用费用流 但是我们如果朴素地建图就会遇到一个问题,若\(i,j\)符合要求,那么给\(i,j\) ...

  8. 修改官方发行openstack镜像的cloud-init登录方式为账号密码登录

    openstack使用的镜像多为qcow2格式,各个发行商也开源了针对openstack制作的镜像.但是这些镜像的登录方式都是注入用户名和密码的方式,就是说不能够直接通过账号和密码登录.那么如何将一个 ...

  9. Paper | Dynamic Residual Dense Network for Image Denoising

    目录 1. 故事 2. 动机 3. 做法 3.1 DRDB 3.2 训练方法 4. 实验 发表于2019 Sensors.这篇文章的思想可能来源于2018 ECCV的SkipNet[11]. 没开源, ...

  10. not in和not exists区别

    如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引: 而not extsts 的子查询依然能用到表上的索引. 所以无论那个表大,用not exists都比not in要快. 也就是 ...