<分区>
这是我的表格
Student (sname, sid, gpa, level, deptno)
Course (cno, cname, deptno, units)
Dept (dname, deptno)
Takes (sid, cno)
编写一个 SQL 查询,返回已参加的学生的姓名(即 snames) 他们部门外的类(class)比他们部门内的多。你可以 假设数据库中的所有学生都至少在他们的系内选修了一门类(class)。
我不是在为这个问题寻找任何解决方案,但仍然欢迎任何答案。 但我更希望人们能告诉我如何生成编写这样一个复杂查询的步骤。
我的答案是
Select S.sname
From Student S, Course C, Dept D, Takes T
Where T.cno=C.cno and D.deptno=C.deptno and S.sid = T.sid
Having COUNT(S.deptno=C.deptno) > COUNT( S.deptno != C.deptno)
我不确定我能否以这种方式使用 HAVING 后的计数。 谢谢