LeetCode MySQL 1412. 查找成绩处于中游的学生
发布日期:2021-07-01 03:30:37 浏览次数:2 分类:技术文章

本文共 2557 字,大约阅读时间需要 8 分钟。

文章目录

1. 题目

表: Student

+---------------------+---------+| Column Name         | Type    |+---------------------+---------+| student_id          | int     || student_name        | varchar |+---------------------+---------+student_id 是该表主键.student_name 学生名字.

表: Exam

+---------------+---------+| Column Name   | Type    |+---------------+---------+| exam_id       | int     || student_id    | int     || score         | int     |+---------------+---------+(exam_id, student_id) 是该表主键.学生 student_id 在测验 exam_id 中得分为 score.

成绩处于中游的学生是指至少参加了一次测验, 且得分既不是最高分不是最低分的学生。

写一个 SQL 语句,找出在所有测验中都处于中游的学生 (student_id, student_name)。

不要返回从来没有参加过测验的学生。返回结果表按照 student_id 排序。

查询结果格式如下。

Student 表:+-------------+---------------+| student_id  | student_name  |+-------------+---------------+| 1           | Daniel        || 2           | Jade          || 3           | Stella        || 4           | Jonathan      || 5           | Will          |+-------------+---------------+Exam 表:+------------+--------------+-----------+| exam_id    | student_id   | score     |+------------+--------------+-----------+| 10         |     1        |    70     || 10         |     2        |    80     || 10         |     3        |    90     || 20         |     1        |    80     || 30         |     1        |    70     || 30         |     3        |    80     || 30         |     4        |    90     || 40         |     1        |    60     || 40         |     2        |    70     || 40         |     4        |    80     |+------------+--------------+-----------+Result 表:+-------------+---------------+| student_id  | student_name  |+-------------+---------------+| 2           | Jade          |+-------------+---------------+对于测验 1: 学生 1 和 3 分别获得了最低分和最高分。对于测验 2: 学生 1 既获得了最高分, 也获得了最低分。对于测验 3 和 4: 学生 1 和 4 分别获得了最低分和最高分。学生 2 和 5 没有在任一场测验中获得了最高分或者最低分。因为学生 5 从来没有参加过任何测验, 所以他被排除于结果表。由此, 我们仅仅返回学生 2 的信息。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/find-the-quiet-students-in-all-exams
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

# Write your MySQL query statement belowselect student_id, student_namefrom (    select distinct student_id    from Exam    where student_id not in    (        select distinct student_id        from Exam e left join        (	# 考试最高、最低分             select exam_id, max(score) maxs, min(score) mins            from Exam            group by exam_id        ) t        using(exam_id)        where score = maxs or score = mins         # 分数等于最高的或者最低的学生    )) t left join Studentusing(student_id)order by student_id

我的CSDN

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!

Michael阿明

转载地址:https://michael.blog.csdn.net/article/details/107701297 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode MySQL 618. 学生地理信息报告(row_number)
下一篇:LeetCode MySQL 1479. 周内每天的销售情况(dayname星期几)

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月18日 11时00分50秒