實驗三 資料庫的巢狀查詢實驗

2022-11-22 14:12:06 字數 3126 閱讀 7061

實驗目的:

加深對巢狀查詢語句的理解。

實驗內容:

使用in、比較符、any或all和exists操作符進行巢狀查詢操作。

實驗步驟:

一. 使用帶in謂詞的子查詢

1. 查詢與』劉晨』在同乙個系學習的學生的資訊:

select * from student where sdept in

(select sdept from student where sname='劉晨')

比較: select * from student where sdept =

(select sdept from student where sname='劉晨') 的異同

比較: select * from student where sdept =

(select sdept from student where sname='劉晨') andsname<>'劉晨v

比較: select s1.* from student s1, student s2 where and '劉晨'

2. 查詢選修了課程名為』資訊系統』 的學生的學號和姓名:

sql server中: select sno, sname from student where sno in

(select sno from sc where cno in

(select cno from course where cname='資訊系統'))

vfp中: select sno, sname from student where sno in

(select sno from sc, course where

and cname='資訊系統')

3. 查詢選修了課程』1』和課程』2』的學生的學號:

select sno from student where sno in (selectsnofrom sc where cno='1')

and sno in (select sno from sc where cno='2')

比較: 查詢選修了課程』1』或課程』2』的學生的sno:

select sno from sc where cno='1' or cno='2'

比較連線查詢:

select from sc a, sc b where and '1' and '2'

二. 使用帶比較運算的子查詢

4. 查詢比』劉晨』年齡小的所有學生的資訊:

select * from student where sage<

(select sage from student where sname='劉晨')

三. 使用帶any, all謂詞的子查詢

5. 查詢其他系中比資訊系(is)某一學生年齡小的學生姓名和年齡;

select sname, sage from student where sage (select sage from student where sdept='is')

and sdept<>'is'

6. 查詢其他系中比資訊系(is)學生年齡都小的學生姓名和年齡:

select sname, sage from student where sage (select sage from student where sdept='is')

and sdept<>'is'

7. 查詢與計算機系(cs)系所有學生的年齡均不同的學生學號, 姓名和年齡:

select sno,sname,sage from student where sage<>all

(select sage from student where sdept='cs')

四. 使用帶exists謂詞的子查詢和相關子查詢

8. 查詢與其他所有學生年齡均不同的學生學號, 姓名和年齡:

select sno,sname,sage from student a where not exists

(select * from student b where and

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

select sname from student where exists

(select * from sc where sno= and cno='1')

10. 查詢沒有選修了1號課程的學生姓名:

select sname from student where not exists

(select * from sc where sno= and cno='1')

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

sql server中:

select sname from student where not exists

(select * from course where not

exists

( select * from sc where sno= and cno=

11. 查詢至少選修了學生95002選修的全部課程的學生的學號:

sql server中:

select distinct sno from sc a where not exists

(select * from sc b where sno='95002'and not exists

(select * from sc c where sno= and cno=

12. 求沒有人選修的課程號cno和cnamecname:

select cno,cname from course c where not exists

(select * from sc where )

13*. 查詢滿足條件的(sno,cno)對, 其中該學號的學生沒有選修該課程號cno的課程

sql server中:

select sno,cno from student,course where not exists

(select * from sc where cno= and sno=

14*. 查詢每個學生的課程成績最高的成績資訊(sno,cno,grade):

select * from sc a where grade=

(select max(grade) from sc where sno= )

思考:如何查詢所有學生都選修了的課程的課程號cno?

資料庫實驗三 查詢

南昌大學實驗報告 學生姓名 丁金芝學號 6100511080 專業班級 管理科學與工程類112班 實驗型別 驗證 綜合 設計 創新實驗日期實驗成績 一 實驗目的 1 掌握select語句的基本語法 2 了解select語句中各子句的作用和特點 3 掌握select語句的統計函式的作用和用法 4 掌握...

實驗一資料庫查詢

一 實驗目的 使學生掌握sql server query analyzer的使用方法,加深對sql和t sql語言的查詢語句的理解。熟練掌握表的基本查詢,連線查詢和巢狀查詢,以及掌握資料排序和資料分組的操作方法。二 實驗原理 select all distinct 目標列表示式 目標列表示式 fro...

實驗三資料庫SQL基本表查詢

資料庫原理及應用實驗報告 實驗報告 實驗型別 綜合設計實驗室 軟體實驗室一 sql基本表查詢 1 實驗專案的目的和任務 熟練掌握查詢語句的一般格式,熟練掌握連線 巢狀和集合查詢的使用。2.程式 部分 select from student where s in select s from sc wh...