LeetCode(数据库)- 查找成绩处于中游的学生

Posted 程序员牧码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- 查找成绩处于中游的学生相关的知识,希望对你有一定的参考价值。

题目链接:点击打开链接

题目大意:略。

解题思路:注意 ALL 函数。

AC 代码

-- 解决方案(1)
select student_id, student_name 
from student
where student_id not in
    (select student_id
    from exam e
    where score >= all(select score from exam where exam_id=e.exam_id)
        or score <= all(select score from exam where exam_id=e.exam_id))
and student_id in (select student_id from exam)

-- 解决方案(2)
WITH t AS(SELECT student_id, RANK() OVER(PARTITION BY exam_id ORDER BY score) rk FROM Exam
UNION ALL
SELECT student_id, RANK() OVER(PARTITION BY exam_id ORDER BY score DESC) rk FROM Exam)

SELECT DISTINCT student_id, student_name
FROM Exam JOIN Student USING(student_id)
WHERE student_id NOT IN (SELECT student_id FROM t WHERE rk = 1)
ORDER BY student_id

以上是关于LeetCode(数据库)- 查找成绩处于中游的学生的主要内容,如果未能解决你的问题,请参考以下文章