题目描述

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+

思路

  1. 排序,取出排名第二的值
select Salary from Employee
order by Salary desc
limit 1, 1;
  1. group by 过滤掉相同薪水
select Salary from Employee
group by Salary
order by Salary desc
limit 1, 1;
  1. 当不存在第二高的薪水时,会返回空而不是 null,做个是否为 null 的判断
select
ifnull(
(select Salary from Employee group by Salary order by Salary desc limit 1, 1),
null
) as SecondHighestSalary;

可以简写为

select
(select Salary from Employee group by Salary order by Salary desc limit 1, 1)
as SecondHighestSalary;

LeetCode176——第二高的薪水的更多相关文章

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

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

  2. LeetCode 176. 第二高的薪水(MySQL版)

    0.前言 最近刷LeetCode 刷数据库题目 由于数据库课上的是SQL,而MySQL有许多自己的函数的,怕把刚学会的函数忘记 特在此记录! 1.题目 编写一个 SQL 查询,获取 Employee ...

  3. LeetCode:176.第二高的薪水

    题目链接:https://leetcode-cn.com/problems/second-highest-salary/ 题目 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Sal ...

  4. MYSQL查询第二高的薪水

    编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+| Id | Salary |+----+--------+| 1 | 100 || ...

  5. SQL Server实现 LeetCode 176 第二高的薪水

    176. 第二高的薪水 SQL架构 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+- ...

  6. Mysql训练:第二高的薪水(IFNULL,OFFSET,LIMIT)

    编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 ...

  7. MySql_176. 第二高的薪水 + limit + distinct + null

    MySql_176. 第二高的薪水 LeetCode_MySql_176 题目描述 题解分析 代码实现 # Write your MySQL query statement below select( ...

  8. LeetCode 176. Second Highest Salary (第二高的薪水)

    题目标签: 题目给了我们一个工资表,让我们返回第二高的工资. 利用Max,把第一高的工资找到,然后利用 NOT IN,去找到第二高的工资. Java Solution: Runtime:  153ms ...

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

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

随机推荐

  1. 网络I/O

    贴几个超级不错的博客 1.Linux IO模式及 select.poll.epoll详解 2.网络 I/O 模型 3.同步异步阻塞非阻塞 4.三种模式的区别与联系

  2. What is react-native link?

    What is react-native link? or Should you just use react-native link when linking any dependency or s ...

  3. Dockerfile介绍、Docker制作jdk镜像

    Dockerfile介绍.Docker制作jdk镜像 目标 1.Dockerfile简介 2.Docker制作jdk镜像 Dockerfile简介 dockerfile 是一个文本格式的配置文件, 用 ...

  4. Python I/O编程 -- 序列化

    序列化 pickle模块,json模块 (1)把变量从内存中变成可存储或传输的过程,称之为序列化.Python中叫pickling,其他语言中也被称为serialization,marshalling ...

  5. 单点登录的实践demo

    https://github.com/deadzq/web-sso-server 统一认证中心 https://github.com/deadzq/web-system-client1 用户客户端 结 ...

  6. VMware Tools安装后设置自动挂载解决共享文件夹无法显示的问题

    一.  确保成功安装了VMware Tools 二.  使用如下命令 1.apt-get install open-vm-tools 2.vmhgfs-fuse .host:/  /mnt/hgfs  ...

  7. 洛谷 P1786 帮贡排序 题解

    P1786 帮贡排序 题目背景 在absi2011的帮派里,死号偏多.现在absi2011和帮主等人联合决定,要清除一些死号,加进一些新号,同时还要鼓励帮贡多的人,对帮派进行一番休整. 题目描述 目前 ...

  8. python range 和xrange

    对于这两个好像功能都差不多,这两个经常会被搞混,所以今天一定要把这个完全弄清楚. 首先我们看看range: range([start,] stop[, step]),根据start与stop指定的范围 ...

  9. python 虚拟环境指定python版本

    virtualenv --no-site-packages -p python3.7 testenv source testenv/bin/activate deactivate 参考:https:/ ...

  10. 【CSP膜你赛】大逃亡

    题目描述 魔王撒旦为了建立魔物的乐土,率领亚多拉玛雷克.艾谢尔.路西菲尔.以 及马纳果达这四位恶魔大元帅进攻人类世界.然而此时手持圣剑的勇者艾米莉亚出现了.  战败的魔王逃跑时穿越到了地球,以真奥贞夫 ...