——一个“相当”的学生,就是至少参加了一门考试,既没得高分也没得低分的人。
- 编写SQL查询以报告学生(student_id,student_name)在所有考试中“安静”。
- 不要退回从未参加过任何考试的学生。返回按student_id排序的结果表。
-- 查询结果格式如以下示例所示。
学生桌:
-- +-------------+---------------+ -- | student_id | student_name | -- +-------------+---------------+ -- | 1 | Daniel | -- | 2 | Jade | -- | 3 | Stella | -- | 4 | Jonathan | -- | 5 | Will | -- +-------------+---------------+
考试表:
-- +------------+--------------+-----------+ -- | 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 | -- +------------+--------------+-----------+
结果表:
-- +-------------+---------------+ -- | student_id | student_name | -- +-------------+---------------+ -- | 2 | Jade | -- +-------------+---------------+
我的解决方案是否正确?
--My Solution Select Student_id, Student_name From ( Select B.Student_id, A.Student_name, Score, Max(Score) Over (Partition by Exam_id) score_max, Max(Score) Over (Partition by Exam_id) score_min From Student A, Exam B Where A.Student_ID = B.Student_ID ) T Where Score != Max_score or Score != Min_Score Group by student_id, student_name Having Count(*) = (Select distinct count(exam_id) from exam) Order by A.student_id
您的结果是正确的,但您需要对查询进行两次更改。
// ...
min(score) over (partition by exam_id) score_min,
max(score) over (partition by exam_id) score_max
// ...
having count(1) =
(select count(distinct exam_id) from exam t2
where t1.student_id = t2.student_id)