select
Sname
from
S
where
not exists(select
*
from
C
where
not exists (select
*
from
SC
where
Snum=S.Snum
and
Cnum=C.Cnum))最里曾的子查询用于查找某个学生选修的所有科目;
中间层的子查询结合not exists()则用于查询出该学生没有选修的课程信息;
最外层的查询结合not exists()则用于查询出不存在没有选修课程的学生信息。两个not exists()的嵌套,是一个双重否定的过程。
Sname
from
S
where
not exists(select
*
from
C
where
not exists (select
*
from
SC
where
Snum=S.Snum
and
Cnum=C.Cnum))最里曾的子查询用于查找某个学生选修的所有科目;
中间层的子查询结合not exists()则用于查询出该学生没有选修的课程信息;
最外层的查询结合not exists()则用于查询出不存在没有选修课程的学生信息。两个not exists()的嵌套,是一个双重否定的过程。
from S
where not exists
(select *
from C
where not exists
(select *
from SC
where Snum=S.Snum
and Cnum=C.Cnum))
原SQL语句的意思是:
如果在课程C表中[不]存在这么一个学生,这个学生什么都课都[没]选,那么就把他找出来,显出他的名字去掉两个否定词,可以理解为: 如果在课程C表中存在这么一个学生,这个学生什么都课都选,那么就把他找出来,显出他的名字当然,SQL语句也就转化成了:select Sname
from S
where exists
(select *
from C
where exists
(select *
from SC
where Snum=S.Snum
and Cnum=C.Cnum))显然,结果也是一样的。