LeetCode 高频 SQL 50 题(基础版)之 【子查询】· 上
题目:1978. 上级经理已离职的公司员工
题解:
select employee_id from Employees
where salary<30000 and manager_id is not null and manager_id not in (select distinct employee_id from Employees
)
order by employee_id
题目:626. 换座位
题解:
select if(id%2=0,id-1,if( id=(select count(distinct id) from Seat),id,id+1)) id, student
from Seat
order by id
题目:1341. 电影评分
题解:
(select t2.name resultsfrom MovieRating t1 left join Users t2on t1.user_id=t2.user_idgroup by t1.user_id order by count(*) desc,t2.name asclimit 1
)union all(select t2.title results fromMovieRating t1 left join Movies t2on t1.movie_id =t2.movie_id where t1.created_at>='2020-02-01' and t1.created_at<='2020-02-29'group by t1.movie_id order by avg(t1.rating) desc,t2.title asclimit 1
)