• 力扣(LeetCode)176. 第二高的薪水(2022.06.25)


    SQL架构:

    Create table If Not Exists Employee (id int, salary int)
    Truncate table Employee
    insert into Employee (id, salary) values ('1', '100')
    insert into Employee (id, salary) values ('2', '200')
    insert into Employee (id, salary) values ('3', '300')
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Employee 表:
    ±------------±-----+
    | Column Name | Type |
    ±------------±-----+
    | id | int |
    | salary | int |
    ±------------±-----+
    id 是这个表的主键。
    表的每一行包含员工的工资信息。

    编写一个 SQL 查询,获取并返回 Employee 表中第二高的薪水 。如果不存在第二高的薪水,查询应该返回 null 。

    查询结果如下例所示。

    示例 1:

    输入:
    Employee 表:
    ±—±-------+
    | id | salary |
    ±—±-------+
    | 1 | 100 |
    | 2 | 200 |
    | 3 | 300 |
    ±—±-------+
    输出:
    ±--------------------+
    | SecondHighestSalary |
    ±--------------------+
    | 200 |
    ±--------------------+
    示例 2:

    输入:
    Employee 表:
    ±—±-------+
    | id | salary |
    ±—±-------+
    | 1 | 100 |
    ±—±-------+
    输出:
    ±--------------------+
    | SecondHighestSalary |
    ±--------------------+
    | null |
    ±--------------------+

    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/second-highest-salary

    方法一:使用 IFNULL 和 LIMIT 子句

    MySQL提交内容:

    # Write your MySQL query statement below
    SELECT
        IFNULL(
          (SELECT DISTINCT Salary
           FROM Employee
           ORDER BY Salary DESC
            LIMIT 1 OFFSET 1),
        NULL) AS SecondHighestSalary
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    UDS入门至精通系列Service 3D
    图床云存储项目课程随堂笔记
    循环神经网络和自然语言处理一
    手机如何压缩照片?压缩方法分享
    webpack优化策略
    如何在洛谷里面新建自己的题目、配置数据点
    编译无法加载主类的问题
    【数据结构之线性表】
    129页4万字某智慧能源集团数字化管控平台项目 建设方案
    C++:类与对象完结篇
  • 原文地址:https://blog.csdn.net/ChaoYue_miku/article/details/125465553