實驗名稱:實驗三 數據高級查詢
實驗目的
掌握複雜數據查詢操作。
實驗步驟與調試過程(請用簡單的文字描述)
對各表中的數據進行不同條件的連接查詢和嵌套查詢;
(1)查詢每個學生及其選課情況;
(2)查詢每門課的間接先修課
(3)將STUDENT,SC進行右連接
(4)查詢既選修了2號課程又選修了3號課程的學生姓名、學號;
(5)查詢和劉晨同一年齡的學生
(6)選修了課程名爲“數據庫”的學生姓名和年齡
(7)查詢其他系比IS系任一學生年齡小的學生名單
(8)查詢其他系中比IS系所有學生年齡都小的學生名單
(9)查詢選修了全部課程的學生姓名
(10)查詢計算機系學生及其性別是男的學生
(11)查詢選修課程1的學生集合和選修2號課程學生集合的差集
(12)查詢李麗同學不學的課程的課程號
(13)查詢選修了3號課程的學生平均年齡
(14)求每門課程學生的平均成績
(15)統計每門課程的學生選修人數(超過3人的才統計)。要求輸出課程號和選修人數,結果按人數降序排列,若人數相同,按課程號升序排列
(16)查詢學號比劉晨大,而年齡比他小的學生姓名。
(17)求年齡大於所有女同學年齡的男同學姓名和年齡
實驗結果(上傳實驗結果截圖或者簡單文字描述)
1.經過實驗可以知道求總數時可以用count()函數。
2.在進行分組操作時,用group by 時要用having來限制條件。
3.order by是排序要求 desc是降序 ,asc是升序。
4.any()函數是任意的意思,all()是所有。
疑難小結(總結個人在實驗中遇到的問題或者心得體會)
1.在進行求總數操作時,由於一開始我不知道有count函數,很是迷茫,後來查閱了資料之後,才得知。
2.在進行將STUDENT,SC進行右連接的操作時,由於少打了一個關鍵字,總是報錯,後來檢查了幾遍之後才找到問題。
3.我對SQL語言還有很多地方不是很熟練,需要勤加練習。
實驗詳細操作步驟或程序清單
(1) 查詢每個學生及其選課情況
select student.sno,sname,ssex,sage,sdept,cno,grade
from student,sc
where student.sno=sc.sno
(2) 查詢每門課的間接先修課
select first.cno,second.cpno
from course first,course second
where first.cpno=second.cno
(3) 將STUDENT,SC進行右連接
select student.sno,sname,ssex,sage,sdept,cno,grade
from student right outer join sc on student.sno=sc.sno
(4) 查詢既選修了2號課程又選修了3號課程的學生姓名、學號
select student.sno,sname
from student inner join sc on student.sno=sc.sno
where cno='3' and sc.sno in
(select sno
from sc
where cno='2')
(5)查詢和劉晨同一年齡的學生
select student.sno,sname
from student
where sname!='劉晨' and sage=
(select sage
from student
where sname='劉晨')
(6)選修了課程名爲“數據庫”的學生姓名和年齡
select sname,sage
from student
where sno in
(select sno
from sc
where cno in
(select cno
from course
where cname='數據庫'))
(7)查詢其他系比IS系任一學生年齡小的學生名單
select student.sno,sname
from student
where sdept<>'IS' and
sage<any
(select sage
from student
where sdept='IS')
(8)查詢其他系中比IS系所有學生年齡都小的學生名單
select student.sno,sname
from student
where sdept<>'IS' and
sage<all
(select sage
from student
where sdept='IS')
(9)查詢選修了全部課程的學生姓名
select sname
from student
where Sno in
(select Sno from SC
group by Sno
having count(*) = (select count(*) from course ))
(10)查詢計算機系學生及其性別是男的學生
select student.sno,sname
from student
where sdept='IS' and ssex='男'
(11)查詢選修課程1的學生集合和選修2號課程學生集合的差集
select sno
from sc
where cno='1' except
select sno
from sc
where cno='2'
(12)查詢李麗同學不學的課程的課程號
select cno
from course
where cno not in
(select cno
from sc
where sno in
(select sno
from student
where sname='李麗'))
(13)查詢選修了3號課程的學生平均年齡
select AVG(sage) as avgsage
from student inner join sc on student.sno=sc.sno
where cno='3'
(14)求每門課程學生的平均成績
select cno,AVG(grade) as avggrade
from sc
group by cno
(15)統計每門課程的學生選修人數(超過3人的才統計)。要求輸出課程號和選修人數,結果按人數降序排列,若人數相同,按課程號升序排列
select course.cno '課程號', count(sc.sno) '人數'
from course,sc
where course.cno=sc.cno
group by course.cno having count(sc.sno)>3 order by COUNT(sc.sno) desc,course.cno asc
(16)查詢學號比劉晨大,而年齡比他小的學生姓名
select sname
from student
where sno>
(select sno from student where sname='劉晨')and
sage<(select sage from student where sname='劉晨')
(17)求年齡大於所有女同學年齡的男同學姓名和年齡
select sname,sage
from student
where ssex='男'and sage>
(select MAX(sage) from student where ssex='女')