LeetCode176——第二高的薪水

题目描述

编写一个 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;

原文地址:https://www.cnblogs.com/chaohangz/p/11050405.html

时间: 2024-07-30 18:36:48

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

数据库专题-leetcode176. 第二高的薪水

题目及分析 题目 题目解析 第二高的薪酬:1.需要排序 order by 2.会有薪酬重复的情况,需要去重 distinct 3.需要取指定行数据 limit 没有第二高的时候设置为null:判断语句,可以使用ifnull 所以:1.先查出按薪酬降序的不重复第二条数据(当不重复数据只有1条或者0条时候,此时结果集为空) 2.使用ifnull函数,将结果输出 结果示例 select ifnull( ( select distinct e.Salary from Employee e order b

176. 第二高的薪水

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 a

力扣——第二高的薪水(数据库的题

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ 例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水.如果不存在第二高的薪水,那么查询应返回 null. +---------------------+ | SecondHighestSal

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

0.前言 最近刷LeetCode 刷数据库题目 由于数据库课上的是SQL,而MySQL有许多自己的函数的,怕把刚学会的函数忘记 特在此记录! 1.题目 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ 例如上述 Employee 表,SQL查询应该返回 200 作

mysql--单表中筛选出第二高的薪水

思路一:(最终的结果如果不存在第二高的薪水的话不会返回null)  无论是是使用哪种方法,最终在select后面都要跟一个distinct做到去重的效果 思路二:(解决不能返回null的问题) 方法一:使用子查询结合limit 方法二:使用IFNULL结合limit 补充: 1.对limit的基本使用规范 2.select 后面如果跟一个为空的结果集,最终输出的结果为null 原文地址:https://www.cnblogs.com/vegetableDD/p/11575104.html

LeetCode:176.第二高的薪水

题目链接:https://leetcode-cn.com/problems/second-highest-salary/ 题目 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ 例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水.如果不存

[LeetCode] 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 second highest salary is 

mysql 第二高薪水

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ 例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水.如果不存在第二高的薪水,那么查询应返回 null. +---------------------+ | SecondHighestSalar

LeetCode--176--第二高的薪水

问题描述: 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ 例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水.如果不存在第二高的薪水,那么查询应返回 null. +---------------------+ | SecondHigh