OracleSQL練習(xí)題及答案_第1頁
OracleSQL練習(xí)題及答案_第2頁
OracleSQL練習(xí)題及答案_第3頁
OracleSQL練習(xí)題及答案_第4頁
OracleSQL練習(xí)題及答案_第5頁
已閱讀5頁,還剩6頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、本文由NiCoBee貢獻(xiàn) Oracle SQL 練習(xí)題注:這些查詢題目涵蓋了各種查詢的技術(shù),請大家認(rèn)真做,做好了的查詢語句之后,有興趣的同學(xué)可以再創(chuàng)建相應(yīng)的視圖和存儲過程create table wkj_student(sno varchar2(10) primary key,sname varchar2(20),sage number(2),ssex varchar2(5);create table wkj_teacher(tno varchar2(10) primary key,tname varchar2(20);create table wkj_course(cno varchar2(

2、10),cname varchar2(20),tno varchar2(20),constraint pk_course primary key (cno,tno);create table wkj_sc(sno varchar2(10),cno varchar2(10),score number(4,2),constraint pk_sc primary key (sno,cno);/*初始化學(xué)生表的數(shù)據(jù)*/insert into student values ('s001','張三',23,'男');insert into student v

3、alues ('s002','李四',23,'男');insert into student values ('s003','吳鵬',25,'男');insert into student values ('s004','琴沁',20,'女');insert into student values ('s005','王麗',20,'女');insert into student values ('

4、s006','李波',21,'男');insert into student values ('s007','劉玉',21,'男');insert into student values ('s008','蕭蓉',21,'女');insert into student values ('s009','陳蕭曉',23,'女');insert into student values ('s010',&

5、#39;陳美',22,'女');commit;/*初始化教師表*/insert into teacher values ('t001', '劉陽');insert into teacher values ('t002', '諶燕');insert into teacher values ('t003', '胡明星');commit;/*初始化課程表*/insert into course values ('c001','J2SE','t

6、002');insert into course values ('c002','Java Web','t002');insert into course values ('c003','SSH','t001');insert into course values ('c004','Oracle','t001');insert into course values ('c005','SQL SERVER 2005'

7、;,'t003');insert into course values ('c006','C#','t003');insert into course values ('c007','JavaScript','t002');insert into course values ('c008','DIV+CSS','t001');insert into course values ('c009','PHP',

8、't003');insert into course values ('c010','EJB3.0','t002');commit;/*初始化成績表*/insert into sc values ('s001','c001',78.9);insert into sc values ('s002','c001',80.9);insert into sc values ('s003','c001',81.9);insert into sc

9、values ('s004','c001',60.9);insert into sc values ('s001','c002',82.9);insert into sc values ('s002','c002',72.9);insert into sc values ('s003','c002',81.9);insert into sc values ('s001','c003','59');insert into

10、sc values ('s002','c003','58');commit;練習(xí):注意:以下練習(xí)中的數(shù)據(jù)是根據(jù)初始化到數(shù)據(jù)庫中的數(shù)據(jù)來寫的SQL 語句,請大家務(wù)必注意。1、查詢“c001”課程比“c002”課程成績高的所有學(xué)生的學(xué)號;2、查詢平均成績大于60 分的同學(xué)的學(xué)號和平均成績;3、查詢所有同學(xué)的學(xué)號、姓名、選課數(shù)、總成績;4、查詢姓“劉”的老師的個(gè)數(shù);5、查詢沒學(xué)過“諶燕”老師課的同學(xué)的學(xué)號、姓名;6、查詢學(xué)過“c001”并且也學(xué)過編號“c002”課程的同學(xué)的學(xué)號、姓名;7、查詢學(xué)過“諶燕”老師所教的所有課的同學(xué)的學(xué)號、姓名;8、查詢課程

11、編號“c002”的成績比課程編號“c001”課程低的所有同學(xué)的學(xué)號、姓名;9、查詢所有課程成績小于60 分的同學(xué)的學(xué)號、姓名;10、查詢沒有學(xué)全所有課的同學(xué)的學(xué)號、姓名;11、查詢至少有一門課與學(xué)號為“s001”的同學(xué)所學(xué)相同的同學(xué)的學(xué)號和姓名;12、查詢至少學(xué)過學(xué)號為“s001”同學(xué)所有一門課的其他同學(xué)學(xué)號和姓名;13、把“SC”表中“諶燕”老師教的課的成績都更改為此課程的平均成績;14、查詢和“s001”號的同學(xué)學(xué)習(xí)的課程完全相同的其他同學(xué)學(xué)號和姓名;15、刪除學(xué)習(xí)“諶燕”老師課的SC 表記錄;16、向SC 表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號“c002”課程的同學(xué)學(xué)號

12、、“c002”號課的平均成績;17、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分18、按各科平均成績從低到高和及格率的百分?jǐn)?shù)從高到低順序19、查詢不同老師所教不同課程平均分從高到低顯示20、統(tǒng)計(jì)列印各科成績,各分?jǐn)?shù)段人數(shù):課程ID,課程名稱,100-85,85-70,70-60, <6021、查詢各科成績前三名的記錄:(不考慮成績并列情況)22、查詢每門課程被選修的學(xué)生數(shù)23、查詢出只選修了一門課程的全部學(xué)生的學(xué)號和姓名24、查詢男生、女生人數(shù)25、查詢姓“張”的學(xué)生名單26、查詢同名同性學(xué)生名單,并統(tǒng)計(jì)同名人數(shù)27、1981 年出生的學(xué)生名單(注:Student

13、 表中Sage 列的類型是number)28、查詢每門課程的平均成績,結(jié)果按平均成績升序排列,平均成績相同時(shí),按課程號降序排列29、查詢平均成績大于85 的所有學(xué)生的學(xué)號、姓名和平均成績30、查詢課程名稱為“數(shù)據(jù)庫”,且分?jǐn)?shù)低于60 的學(xué)生姓名和分?jǐn)?shù)31、查詢所有學(xué)生的選課情況;32、查詢?nèi)魏我婚T課程成績在70 分以上的姓名、課程名稱和分?jǐn)?shù);33、查詢不及格的課程,并按課程號從大到小排列34、查詢課程編號為c001 且課程成績在80 分以上的學(xué)生的學(xué)號和姓名;35、求選了課程的學(xué)生人數(shù)36、查詢選修“諶燕”老師所授課程的學(xué)生中,成績最高的學(xué)生姓名及其成績37、查詢各個(gè)課程及相應(yīng)的選修人數(shù)38、

14、查詢不同課程成績相同的學(xué)生的學(xué)號、課程號、學(xué)生成績39、查詢每門功課成績最好的前兩名40、統(tǒng)計(jì)每門課程的學(xué)生選修人數(shù)(超過10 人的課程才統(tǒng)計(jì))。要求輸出課程號和選修人數(shù),查詢結(jié)果按人數(shù)降序排列,若人數(shù)相同,按課程號升序排列41、檢索至少選修兩門課程的學(xué)生學(xué)號42、查詢?nèi)繉W(xué)生都選修的課程的課程號和課程名43、查詢沒學(xué)過“諶燕”老師講授的任一門課程的學(xué)生姓名44、查詢兩門以上不及格課程的同學(xué)的學(xué)號及其平均成績45、檢索“c004”課程分?jǐn)?shù)小于60,按分?jǐn)?shù)降序排列的同學(xué)學(xué)號46、刪除“s002”同學(xué)的“c001”課程的成績1. 查詢“c001”課程比“c002”課程成績高的所有學(xué)生的學(xué)號*sel

15、ect a.* from (select * from sc a where o='c001') a,(select * from sc b where o='c002') bwhere a.sno=b.sno and a.score > b.score;*select * from sc awhere o='c001'and exists(select * from sc b where o='c002' and a.score>b.scoreand a.sno = b.sno)*2. 查詢平均成績大于60 分的同學(xué)

16、的學(xué)號和平均成績;*select sno,avg(score) from sc group by sno having avg(score)>60;*3. 3、查詢所有同學(xué)的學(xué)號、姓名、選課數(shù)、總成績;*select a.*,s.sname from (select sno,sum(score),count(cno) from sc group by sno) a ,student s where a.sno=s.sno錯(cuò)錯(cuò)誤示范 select st.*,count(o),sum(sc.score)from student st,scwhere st.sno=sc.snogroup by

17、 st.sno select count(o) sum (sc.score) 不是單數(shù)分組,要先group,不然和 select stu.sno關(guān)聯(lián)不起來*4. 4、查詢姓“劉”的老師的個(gè)數(shù);*select count(*) from teacher where tname like '劉%'*5. 5、查詢沒學(xué)過“諶燕”老師課的同學(xué)的學(xué)號、姓名;*select a.sno,a.sname from student awhere a.sno not in (select distinct s.sno from sc s,(select c.* from course c ,(s

18、elect tno from teacher t where tname='諶燕')t where c.tno=t.tno) bwhere o = o ) *select * from student st where st.sno not in(select distinct sno from sc s join course c on o=ojoin teacher t on c.tno=t.tno where tname='諶燕')*6. 6、查詢學(xué)過“c001”并且也學(xué)過編號“c002”課程的同學(xué)的學(xué)號、姓名;*select st.* from stud

19、ent stjoin sc a on st.sno=a.snojoin sc b on a.sno=b.snowhere o='c001' and o='c002' ;*7. 7、查詢學(xué)過“諶燕”老師所教的所有課的同學(xué)的學(xué)號、姓名;*select st.* from student st join sc s on st.sno=s.snojoin course c on o=ojoin teacher t on c.tno=t.tnowhere t.tname='諶燕'*8. 8、查詢課程編號“c002”的成績比課程編號“c001”課程低的所有同

20、學(xué)的學(xué)號、姓名;*select * from student stjoin sc a on st.sno=a.snojoin sc b on st.sno=b.snowhere o='c002' and o='c001' and a.score < b.score*9. 9、查詢所有課程成績小于60 分的同學(xué)的學(xué)號、姓名*select st.*,s.score from student stjoin sc s on st.sno=s.snojoin course c on o=owhere s.score <60*10. 查詢沒有學(xué)全所有課的同學(xué)的學(xué)

21、號、姓名*select stu.sno,stu.sname,count(o) from student stuleft join sc on stu.sno=sc.snogroup by stu.sno,stu.snamehaving count(o)<(select count(distinct cno)from course)=select * from student where sno in(select sno from (select stu.sno,o from student stucross join course cminusselect sno,cno from s

22、c)=*11. 查詢至少有一門課與學(xué)號為“s001”的同學(xué)所學(xué)相同的同學(xué)的學(xué)號和姓名*select st.* from student st,(select distinct a.sno from (select * from sc) a,(select * from sc where sc.sno='s001') bwhere o=o) hwhere st.sno=h.sno and st.sno<>'s001'*12. 查詢至少學(xué)過學(xué)號為“s001”同學(xué)所有一門課的其他同學(xué)學(xué)號和姓名;*select * from scleft join stud

23、ent ston st.sno=sc.sno where sc.sno<>'s001'and o in(select cno from scwhere sno='s001')*13. 把“SC”表中“諶燕”老師教的課的成績都更改為此課程的平均成績*update sc c set score=(select avg(c.score) from course a,teacher bwhere a.tno=b.tnoand b.tname='諶燕'and o=ogroup by o)where cno in(select cno from

24、course a,teacher bwhere a.tno=b.tnoand b.tname='諶燕')*不會做14.(原來是錯(cuò)的我改過來了) 查詢和“s001”號的同學(xué)學(xué)習(xí)的課程完全相同的其他同學(xué)學(xué)號和姓名*15. 刪除學(xué)習(xí)“諶燕”老師課的SC 表記錄;*delete from scwhere o in(select cno from course cleft join teacher t on c.tno=t.tnowhere t.tname='諶燕' )*16. 16、向SC 表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號“c002”課程的同學(xué)學(xué)

25、號、“c002”號課的平均成績;*insert into sc (sno,cno,score)select distinct st.sno,o,(select avg(score)from sc where cno='c002') from student st,scwhere not exists(select * from sc where cno='c002' and sc.sno=st.sno) and o='c002'*17. 17、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分*select cno ,max(s

26、core),min(score) from sc group by cno;*18. 18、按各科平均成績從低到高和及格率的百分?jǐn)?shù)從高到低順序*select cno,avg(score),round(sum(case when score>=60 then 1 else 0 end)/count(*),2)*100|% as 及格率from sc group by cnoorder by avg(score) , 及格率 desc*19. 19、查詢不同老師所教不同課程平均分從高到低顯示*select max(t.tno),max(t.tname),max(o),max(ame),o,a

27、vg(score) from sc , course c,teacher t where o=o and c.tno=t.tnogroup by oorder by avg(score) desc*20. 20、統(tǒng)計(jì)列印各科成績,各分?jǐn)?shù)段人數(shù):課程ID,課程名稱,100-85,85-70,70-60, <60*答案是錯(cuò)的select o,ame,sum(case when score between 100 and 85 then 1 else 0 end) AS "100-85",sum(case when score between 85 and 70 then

28、1 else 0 end) AS "85-70",sum(case when score between 70 and 60 then 1 else 0 end) AS "70-60",sum(case when score <60 then 1 else 0 end) AS "<60"from sc, course cwhere o=o group by o ,ame; 改正后的select o,ame,sum(case when score>85 and score<100 then 1 else 0 en

29、d) 85到100,sum(case when score>70 and score<850 then 1 else 0 end) 70到85,sum(case when score>60 andthen 1 else 0 end) 60到70,sum(case when score <60 then 1 else 0 end) 60以下from sc, course cwhere o=o group by o ,ame;*21. 查詢各科成績前三名的記錄:(不考慮成績并列情況)*select * from (select sno,cno,score,row_numbe

30、r()over(partition by cno order by score desc) rn from sc)where rn<4*22. 22、查詢每門課程被選修的學(xué)生數(shù)*select cno,count(sno)from sc group by cno;*23. 23、查詢出只選修了一門課程的全部學(xué)生的學(xué)號和姓名 *select sc.sno,st.sname,count(cno) from student stleft join sc on sc.sno=st.snogroup by st.sname,sc.sno having count(cno)=1;*24. 4、查詢男生

31、、女生人數(shù)*select ssex,count(*)from student group by ssex;*25. 25、查詢姓“張”的學(xué)生名單*select * from student where sname like '張%'*26. 26、查詢同名同性學(xué)生名單,并統(tǒng)計(jì)同名人數(shù)*select sname,count(*)from student group by sname having count(*)>1;*27. 27、1981 年出生的學(xué)生名單(注:Student 表中Sage 列的類型是number)*select sno,sname,sage,ssex

32、from student t where to_char(sysdate,'yyyy')-sage =1988*28. 28、查詢每門課程的平均成績,結(jié)果按平均成績升序排列,平均成績相同時(shí),按課程號降序排列*select cno,avg(score) from sc group by cno order by avg(score)asc,cno desc;*29. 29、查詢平均成績大于85 的所有學(xué)生的學(xué)號、姓名和平均成績*select st.sno,st.sname,avg(score) from student stleft join sc on sc.sno=st.sn

33、ogroup by st.sno,st.sname having avg(score)>85;*30. 30、查詢課程名稱為“數(shù)據(jù)庫”,且分?jǐn)?shù)低于60 的學(xué)生姓名和分?jǐn)?shù)*select sname,score from student st,sc,course cwhere st.sno=sc.sno and o=o and ame='Oracle' and sc.score<60*31. 查詢所有學(xué)生的選課情況; *select st.sno,st.sname,ame from student st,sc,course cwhere sc.sno=st.sno an

34、d o=o;* *32. 查詢?nèi)魏我婚T課程成績在70 分以上的姓名、課程名稱和分?jǐn)?shù);*select st.sname,ame,sc.score from student st,sc,course cwhere sc.sno=st.sno and o=o and sc.score>70*33. 33、查詢不及格的課程,并按課程號從大到小排列*select sc.sno,ame,sc.score from sc,course cwhere o=o and sc.score<60 order by o desc;*34. 34、查詢課程編號為c001 且課程成績在80 分以上的學(xué)生的學(xué)號

35、和姓名;*select st.sno,st.sname,sc.score from sc,student st where sc.sno=st.sno and cno='c001' and score>80;*35. 35、求選了課程的學(xué)生人數(shù)*select count(distinct sno) from sc;*y有問題36、查詢選修“諶燕”老師所授課程的學(xué)生中,成績最高的學(xué)生姓名及其成績*select st.sname,score from student st,sc ,course c,teacher twherest.sno=sc.sno and o=o and c.tno=t.tnoand t.tname='諶燕' and sc.score=(select max(score)from sc where o=o)*37. 37、查詢各個(gè)課程及相應(yīng)的選修人數(shù)*select cno,count(sno) from sc group by cno;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論