Ai
1 Star 0 Fork 0

逸扬/leetcode-hub-mysql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc-1412.test 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
# $1412. 查找成绩处于中游的学生
# https://leetcode-cn.com/problems/find-the-quiet-students-in-all-exams/
# SQL架构
Create table If Not Exists Student (student_id int, student_name varchar(30));
Create table If Not Exists Exam (exam_id int, student_id int, score int);
Truncate table Student;
insert into Student (student_id, student_name) values ('1', 'Daniel');
insert into Student (student_id, student_name) values ('2', 'Jade');
insert into Student (student_id, student_name) values ('3', 'Stella');
insert into Student (student_id, student_name) values ('4', 'Jonathan');
insert into Student (student_id, student_name) values ('5', 'Will');
Truncate table Exam;
insert into Exam (exam_id, student_id, score) values ('10', '1', '70');
insert into Exam (exam_id, student_id, score) values ('10', '2', '80');
insert into Exam (exam_id, student_id, score) values ('10', '3', '90');
insert into Exam (exam_id, student_id, score) values ('20', '1', '80');
insert into Exam (exam_id, student_id, score) values ('30', '1', '70');
insert into Exam (exam_id, student_id, score) values ('30', '3', '80');
insert into Exam (exam_id, student_id, score) values ('30', '4', '90');
insert into Exam (exam_id, student_id, score) values ('40', '1', '60');
insert into Exam (exam_id, student_id, score) values ('40', '2', '70');
insert into Exam (exam_id, student_id, score) values ('40', '4', '80');
# Write your MySQL query statement below
select
tmp.student_id,
s.student_name
from
(
select
exam_id,
student_id,
score,
if(
dense_rank() over(
partition by exam_id
order by
score desc
) = 1,
1,
0
) as rk_desc,
if(
dense_rank() over(
partition by exam_id
order by
score
) = 1,
1,
0
) as rk_asc
from
Exam
) tmp
left join Student s on tmp.student_id = s.student_id
group by
tmp.student_id,
# sql_mode=only_full_group_by
s.student_name
having
sum(tmp.rk_desc) = 0
and sum(tmp.rk_asc) = 0
order by
tmp.student_id;
# clean-up
drop table Student;
drop table Exam;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gdut_yy/leetcode-hub-mysql.git
git@gitee.com:gdut_yy/leetcode-hub-mysql.git
gdut_yy
leetcode-hub-mysql
leetcode-hub-mysql
master

搜索帮助