数据库
首页 > 数据库> > leetcode-mysql 2021-06-02

leetcode-mysql 2021-06-02

作者:互联网

注意点: ifnull函数的初衷是将为null的数据行转换为其他值 而不是把其他值转换成null

来源:Leetcode  链接:https://leetcode-cn.com/problems/second-highest-salary 【侵删】
 

ExampleA(第二高的薪水)

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

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


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

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


解题思路(dense_rank over limit 1,1 distinct)
 

# @1
select (select n.salary from (SELECT distinct salary, DENSE_RANK() OVER (ORDER BY salary DESC) rank_num FROM Employee limit 1,1) n )  as SecondHighestSalary;


# @2
select
(
    select distinct salary 
    from employee 
    order by salary desc 
    limit 1,1
) 
as SecondHighestSalary

 

标签:02,salary,06,薪水,+----+--------+,2021,Employee,null,select
来源: https://blog.csdn.net/qq_23688379/article/details/117404942