查詢選修了全部課程的學生姓名

2021-09-12 02:20:44 字數 582 閱讀 6239

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查詢的表分別是student和course表,最後乙個是sc表。關鍵子查詢條件在於最內層的where語句。

這是個動態的迴圈的查詢,由最外層student表的sno的屬性值迴圈實現。 

那麼結合起來看,如果sno是001,那麼最內層select語句就是查詢的這個學生的選課表;第二層求的是這個學生沒有選的課程,如果這個學生選了全部的課程那麼第二層就是false的結果,再用not exists否定就是true的結果,那麼返回的就是001這個學生的姓名,否則就是相反的結果,即這個學生沒有選全部的課程,那麼這個學生不滿足件,不會被輸出出來!

最後結果查詢的就是選修了全部課程的學生姓名。

查詢選修了全部課程的學生姓名

1.select 2.sname 3.from s 5.where notexists 6.select from course where notexists 7.select from sc where sno student.sno and cno course.cno where後面的 no...

SQL查詢選修了全部課程的學生姓名

1.select 2.sname 3.from 4.not exists 5.select from course where not exists 6.select from sc where sno student.sno and cno course.cno 對於這個題目我解釋一下 not e...

SQL查詢選修了全部課程的學生姓名解析

查詢選修le全部課程的學生姓名 select sname from student where not exists select from course where not exists select from sc where sno student.sno and cno course.cno...