美文网首页
leetcode中的SQL题(三)

leetcode中的SQL题(三)

作者: 瓜皮小咸鱼 | 来源:发表于2019-04-03 10:14 被阅读0次

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

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

例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null。

+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+

题目分析:
第n高 需要创建一个函数,要求如果存在返回,不存在返回null 需要使用IF()

答案:

create funtion getNthHighestSalary(N int) returns int
begin 
set N=N;
return (
select (IF((select count(*) from (select distinct e.Salary from employee e) 
 e) >= N, select min(e.Salary) from (select distinct e.Salary from order by e.Salary desc limit N) e) , NULL))
);
end

相关文章

  • leetcode中的SQL题(三)

    编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。 例如上述 Employee...

  • LeetCode-SQL-超过5名学生的课程

    LeetCode-596-超过5名学生的课程 今天带来的是LeetCode for SQL的第三题,主要考察的是g...

  • leetcode中的SQL题(十三)

    给定一个 salary 表,如下所示,有 m=男性 和 f=女性 的值 。交换所有的 f 和 m 值(例如,将所有...

  • leetcode中的SQL题(八)

    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id D...

  • leetcode中的SQL题(七)

    某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客...

  • leetcode中的SQL题(五)

    Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id...

  • leetcode中的SQL题(十)

    编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。 在...

  • leetcode中的SQL题(四)

    编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次...

  • leetcode中的SQL题(九)

    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id 。...

  • leetcode中的SQL题(十一)

    给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。 ...

网友评论

      本文标题:leetcode中的SQL题(三)

      本文链接:https://www.haomeiwen.com/subject/maxabqtx.html