这是一个作业问题。我并不是在寻求正确的答案,我只是在寻求帮助我是否朝着正确的方向前进。
提示是我要返回(OR):(a)当前尚未注册(不在Enrollments表中)或(b)在其所有班级中得分最低的学生的名字和姓氏自己的部门。
我想到的SQL查询是:
SELECT FIRSTNAME, LASTNAME
FROM STUDENTS
JOIN ENROLLMENTS ON STUDENTS.NETID = ENROLLMENTS.NETID
JOIN COURSES ON COURSES.CRN = ENROLLMENTS.CRN
WHERE STUDENTS.NETID NOT IN (
SELECT NETID
FROM ENROLLMENTS
) OR
ENROLLMENTS.SCORE IN (
SELECT MIN(SCORE)
FROM ENROLLMENTS
WHERE COURSES.DEPARTMENT = STUDENTS.DEPARTMENT
GROUP BY ENROLLMENTS.CRN
);
我试图使用JOIN子句来组合三个相交的表。 NetId对于每个学生都是不同的,可以在“注册”表中识别他们。每个班级都有一个唯一的CRN,因此我将它们连接在Courses表和Enrollments表之间。
我不知道问题出在哪里,但预期结果是:
Wbixik Yjepuriluwe
Wtoyi Avamijosu
Jheyiresoxo Bsexedoh
Ulerusota Mzuzu
但是我的结果是:
Wbixik Yjepuriluwe
Jropop Vduyumi
Jheyiresoxo Bsexedoh
Ulerusota Mzuzu
正在寻找可以使我重回正轨的指导。
答案 0 :(得分:1)
您可以尝试使用左联接
SELECT FIRSTNAME, LASTNAME
FROM STUDENTS
left JOIN ENROLLMENTS ON STUDENTS.NETID = ENROLLMENTS.NETID
JOIN COURSES ON COURSES.CRN = ENROLLMENTS.CRN
where ENROLLMENTS.NETID is null OR
ENROLLMENTS.SCORE IN
(SELECT MIN(SCORE) FROM ENROLLMENTS
WHERE COURSES.DEPARTMENT = STUDENTS.DEPARTMENT
GROUP BY ENROLLMENTS.CRN
)