0
点赞
收藏
分享

微信扫一扫

leetcode数据库中等题目

you的日常 2022-01-04 阅读 31

176. 第二高的薪水

select ifnull((select distinct Salary from Employee order by Salary desc limit 1, 1), null) as SecondHighestSalary 

177. 第N高的薪水

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    set N = N - 1;
  RETURN (
      # Write your MySQL query statement below.
      select ifnull((select distinct Salary from Employee order by Salary desc limit N, 1), null) as getNthHighestSalary
  );
END

178. 分数排名

select a.Score, count(distinct(b.Score)) as 'Rank'
from Scores a join Scores b 
on b.Score >= a.Score 
group by a.Id 
order by a.Score desc;

180. 连续出现的数字

select distinct l.Num as ConsecutiveNums from Logs l join Logs o join Logs g on (l.id = o.id - 1 and l.Num = o.Num) and (o.id = g.id - 1 and o.Num = g.Num) 

184. 部门工资最高的员工

select d.Name as 'Department', e.Name as 'Employee', e.Salary as 'Salary' from Employee e join Department d on e.DepartmentId = d.Id and (e.DepartmentId,e.Salary) in (select DepartmentId, max(Salary) from Employee group by DepartmentId)

626. 换座位

select s1.id as 'id', coalesce(s2.student, s1.student) as 'student' from seat s1 left join seat s2 on (s1.id = s2.id - 1 and s1.id % 2 = 1) or (s2.id = s1.id - 1 and s2.id % 2 = 1)
举报

相关推荐

0 条评论