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. RAID 独立磁盘冗余阵列 - redundant array of independent disks

    RAID:  RAID全称是独立磁盘冗余阵列(Redundant Array of Independent Disks),基本思想是把多个磁盘组合起来,组合一个磁盘阵列组,使得性能大幅提高. RAID ...

  2. ubuntu中输入arm-linux-gcc -v出现no such file or directory

    这个问题困扰了我差不多两天时间了,明明已经安装了arm-linux-gcc,且系统变量和用户变量都配置好了 但每次输入arm-linux-gcc -v都会出现如题所示错误.最终经过查到一个帖子有说是因 ...

  3. Ubuntu18.04 安装TensorFlow 和 Keras

    TensorFlow和Keras是当前两款主流的深度学习框架,Keras被采纳为TensorFlow的高级API,平时做深度学习任务,可以使用Keras作为深度学习框架,并用TensorFlow作为后 ...

  4. Java JDK 运行环境安装

    1 Windows系统 1.1 安装包准备 安装包本文档讲解为64位系统,如果是32位系统需要下载32位系统安装包 1.1.1 下载方式 打开jdk官方下载地址,该文档提供jdk版本为1.8 http ...

  5. 全网最新方法:win10下如何安装和配置64位JDK-13

    1.下载JDK-13 下载链接:https://www.oracle.com/technetwork/java/javase/downloads/index.html 2.安装JDK-13 双击下图的 ...

  6. 《京东到家订单中心 Elasticsearch 演进历程》----阅读

    上篇通过阅读文章对京东到家的架构分析有了初步了解,这次对文章(https://mp.weixin.qq.com/s?__biz=MzU1MzE2NzIzMg==&mid=2247486889& ...

  7. Laravel5 --- QQ邮箱发送邮件

    1. 在此之前先确认QQ邮箱是否开启了POP3/SMTP服务,如果未开启则须开启 QQ邮箱->设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalD ...

  8. webrtc笔记(2): 1对1实时视频/语音通讯原理概述

    开始正文之前,先思考1个问题:2个处于不同网络环境的(具备摄像头/麦克风多媒体设备的)浏览器,要实现点对点的实时视频/语音通讯,难点在哪? 至少得先搞定下面2个问题: 1.彼此要了解对方支持的媒体格式 ...

  9. RocketMQ的顺序消费和事务消费

    一.三种消费 :1.普通消费 2. 顺序消费 3.事务消费 1.1  顺序消费:在网购的时候,我们需要下单,那么下单需要假如有三个顺序,第一.创建订单 ,第二:订单付款,第三:订单完成.也就是这个三个 ...

  10. centos 文本文件编码转换

    iconv -f utf-8 -t gbk   shtel_single_utf8.mlf > shtel_single_gbk.mlf