zoukankan      html  css  js  c++  java
  • LeeCode——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 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:
    如果您觉得我的文章对您有帮助,请关注我的微信公众号,谢谢!
    程序员打怪之路

  • 相关阅读:
    git 常用命令
    mac 显示隐藏文件
    android 图片缓存
    字符串与枚举相互转换
    ios 消息通知
    ios 真机调试
    ios 宏定义 系统版本 判定
    autolayout autoresizing
    c++对象创建带括号与无括号的区别
    内存对齐
  • 原文地址:https://www.cnblogs.com/jason1990/p/11632706.html
Copyright © 2011-2022 走看看