帶有exists謂詞的子查詢 嵌套查詢

Exists,NOT EXISTS:
1.含義:帶有exists謂詞的子查詢不返回任何實際數據,它只產生邏輯真值true或邏輯假值false。

2.查詢所有選修了c1號課程的學生姓名:

   select sn from s where exists (select * from sc where sno=s.sno and cno='c1')

注:若內查詢結果(select * from sc where sno=s.sno and cno='c1')爲空,則外層的where子句返回真值,否則返回假值!

查詢過程:步驟一:從外層查詢中的s表的第一個元組,根據它,與內層查詢相關的屬性值(sno值)處理內層查詢,若where查詢子句返回值爲真(及內層查詢非空),則取此元組放入結果表;

                 步驟二:  在檢查s表中下一個元組;

                 步驟三:重複這一過程,直至s表全部檢查完畢爲止!

3。查詢所有未修c1課程的學生姓名:

   select sn from s where not exists (select * from sc where sno=s.sno and cno='c1')
 

4.查詢與“王林”在同一系學習的學生的信息

   select * from s s1 where exists (select * from s s2 where s2.dept=s1.dept and s2.sn='王林')

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

   select sn from s where not exists(select * from c where not exists(select * from sc where sno=s.sno and cno=c.cno))

數據庫表s,c,sc截圖請到截圖地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章