SQL查询选修了全部课程的学生姓名

查询选修了全部课程的学生姓名
:student 是学生表 course 是选课表 sc 是课程表
select sname
from student
where not exists
(select *
from course
where not exists
(select *
from sc
where sno =student.sno
and cno=course.cno)

select *
from course
where not exists
(select *
from sc
where sno =student.sno
and cno=course.cno)
这一段计算为什么就得出 某学生存在没有选的课程?

分析原因如下:

第一问:两个NOT EXISTS表示双重否定:没有一个选了课的学生没有选course表里的课程

select sname from student   where not exists /*没有一个学生满足以下条件*/

(select *   from course  where not exists  /*什么条件呢?没有选过Course表里的课*/

(select *  from sc  where sno =student.sno   /*这里两个=分别指对应的关系,表示选  

过课并且是Course里and cno=course.cno)  的课,只不过用not exists否定掉了*/

第二问:其实和NOT IN 是一个意思 exists只返回true 或false 这里not exists里的内容 其实就                     是指学生选过的课程,再用NOT EXISTS否定了,就变成了没有选

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-11-25
第一问:两个NOT EXISTS表示双重否定:没有一个选了课的学生没有选course表里的课程
select sname
from student
where not exists /*没有一个学生满足以下条件*/
(select * from course
where not exists /*什么条件呢?没有选过Course表里的课*/
(select * from sc
where sno =student.sno /*这里两个=分别指对应的关系,表示选过课并且是Course里and cno=course.cno) 的课,只不过用not exists否定掉了*/

第二问:其实和NOT IN 是一个意思 exists只返回true 或false 这里not exists里的内容 其实就是指学生选过的课程,再用NOT EXISTS否定了,就变成了没有选的本回答被提问者和网友采纳
第2个回答  2012-06-25
exists 是类似于in 效率比in 好的多
not exists 类似于 not in 效率一样比not in 一样好的多
再来看这个sql语句,应该明白了吧