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

2021-08-27 08:18:05 字數 1029 閱讀 7990

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 exists: 它表示若查詢結果為空,則最外層的where子句返回真值(true),否則為假值(false);exists正好與它相反;

對於執行過程我舉個例子:

select sname from student where exists(

select * from sc where sno=student.sno

and cno='1');

它的執行過程是:

首先取外層查詢中student表的第乙個元祖,根據它與內層查詢查詢相關的屬性值(sno值)處理內層查詢,

若where子句的返回值為真,則取外層查詢中該元祖的sname放入結果表;

然後去student表的下乙個元祖;重複這一過程,直至外層student表全部檢查完為止。

所有對於上面兩個not exists ,他們的執行過程是這樣的。

首先我們是要查詢選了全部的課程的學生。那麼跑一遍sql語句,假設有乙個 sno=2012 的學生的選擇了全部課程和乙個sno=2013 的學生只選擇了全部課程的其中幾門; 假設先以sno=2012(也就是選了全部課程的那位),那麼先往最內層看也就是第6行,既然選擇了全部課程那麼第6行最後不就是有結果集,既然有結果集那麼第5行的not exists不就是為false嘛!,也就是第5行得到的結果嘛。 既然第5行為false那不就是第5行的結果集為空,既然第5行的結果集為空那麼第4行的not exists不就是為 true , 那麼最後最外層當前元祖不就加入到最後查詢結果集了。

然後對於sno=2013這個人。自己跑一遍也就知道了。

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...

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

select sname from student where not exists select from course where not exists select from sc where sno student.sno and cno course.cno 我的理解是 這是乙個相關子查詢...

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

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...