Oracle-SQL-練習題及答案_第1頁
Oracle-SQL-練習題及答案_第2頁
Oracle-SQL-練習題及答案_第3頁
Oracle-SQL-練習題及答案_第4頁
Oracle-SQL-練習題及答案_第5頁
已閱讀5頁,還剩6頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、本文由NiCoBee奉獻 Oracle SQL 練習題注:這些查詢題目涵蓋了各種查詢的技術,請大家認真做,做好了的查詢語句之后,有興趣的同學可以再創(chuà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);/*初始化學生表的數(shù)據(jù)*/insert into student values (s001,張三,23,男);insert into student values (s002,李四,23,男);insert in

3、to student values (s003,吳鵬,25,男);insert into student values (s004,琴沁,20,女);insert into student values (s005,王麗,20,女);insert into student values (s006,李波,21,男);insert into student values (s007,劉玉,21,男);insert into student values (s008,蕭蓉,21,女);insert into student values (s009,陳蕭曉,23,女);insert into st

4、udent values (s010,陳美,22,女);commit;/*初始化教師表*/insert into teacher values (t001, 劉陽);insert into teacher values (t002, 諶燕);insert into teacher values (t003, 胡明星);commit;/*初始化課程表*/insert into course values (c001,J2SE,t002);insert into course values (c002,Java Web,t002);insert into course values (c003,S

5、SH,t001);insert into course values (c004,Oracle,t001);insert into course values (c005,SQL SERVER 2005,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,t003);insert into co

6、urse 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 values (s004,c001,60.9);insert into sc values (s001,c002,82.9);insert into sc values (s002,c002,72.9);insert into sc val

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

8、、姓名;7、查詢學過“諶燕老師所教的所有課的同學的學號、姓名;8、查詢課程編號“c002”的成績比課程編號“c001”課程低的所有同學的學號、姓名;9、查詢所有課程成績小于60 分的同學的學號、姓名;10、查詢沒有學全所有課的同學的學號、姓名;11、查詢至少有一門課與學號為“s001”的同學所學相同的同學的學號和姓名;12、查詢至少學過學號為“s001”同學所有一門課的其他同學學號和姓名;13、把“SC表中“諶燕老師教的課的成績都更改為此課程的平均成績;14、查詢和“s001”號的同學學習的課程完全相同的其他同學學號和姓名;15、刪除學習“諶燕老師課的SC 表記錄;16、向SC 表中插入一些記

9、錄,這些記錄要求符合以下條件:沒有上過編號“c002”課程的同學學號、“c002”號課的平均成績;17、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分18、按各科平均成績從低到高和及格率的百分數(shù)從高到低順序19、查詢不同老師所教不同課程平均分從高到低顯示20、統(tǒng)計列印各科成績,各分數(shù)段人數(shù):課程ID,課程名稱,100-85,85-70,70-60, b.score;*select * from sc awhere a o=c001and exists(select * from sc b where b o=c002 and a.scoreb.scoreand a.sno

10、 = b.sno)*2. 查詢平均成績大于60 分的同學的學號和平均成績;*select sno,avg(score) from sc group by sno having avg(score)60;*3. 3、查詢所有同學的學號、姓名、選課數(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錯錯誤示范 select st.*,count(sc o),sum(sc.score)from student st,sc

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

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

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

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

15、c o) from student stuleft join sc on stu.sno=sc.snogroup by stu.sno,stu.snamehaving count(sc o)(select count(distinct cno)from course)=select * from student where sno in(select sno from (select stu.sno,c o from student stucross join course cminusselect sno,cno from sc)=*11. 查詢至少有一門課與學號為“s001”的同學所學相同

16、的同學的學號和姓名*select st.* from student st,(select distinct a.sno from (select * from sc) a,(select * from sc where sc.sno=s001) bwhere a o=b o) hwhere st.sno=h.sno and st.snos001*12. 查詢至少學過學號為“s001”同學所有一門課的其他同學學號和姓名;*select * from scleft join student ston st.sno=sc.sno where sc.snos001and sc o in(select

17、 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 a o=c ogroup by c o)where cno in(select cno from course a,teacher bwhere a.tno=b.tnoand b.tname=諶燕)*不會做14.(原來是錯的我改正來了) 查詢和“s001”號的同學學習的課程

18、完全相同的其他同學學號和姓名*15. 刪除學習“諶燕老師課的SC 表記錄;*delete from scwhere sc o in(select cno from course cleft join teacher t on c.tno=t.tnowhere t.tname=諶燕 )*16. 16、向SC 表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號“c002”課程的同學學號、“c002”號課的平均成績;*insert into sc (sno,cno,score)select distinct st.sno,sc o,(select avg(score)from sc where

19、 cno=c002) from student st,scwhere not exists(select * from sc where cno=c002 and sc.sno=st.sno) and sc o=c002;*17. 17、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分*select cno ,max(score),min(score) from sc group by cno;*18. 18、按各科平均成績從低到高和及格率的百分數(shù)從高到低順序*select cno,avg(score),round(sum(case when score=60 then 1

20、 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(c o),max(c ame),c o,avg(score) from sc , course c,teacher t where sc o=c o and c.tno=t.tnogroup by c oorder by avg(score) desc*20. 20、統(tǒng)計列印各科成績,各分數(shù)段人

21、數(shù):課程ID,課程名稱,100-85,85-70,70-60, 60*答案是錯的select sc o,c 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 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)

22、AS 85 and score70 and score60 andthen 1 else 0 end) 60到70,sum(case when score 60 then 1 else 0 end) 60以下from sc, course cwhere sc o=c o group by sc o ,c ame;*21. 查詢各科成績前三名的記錄:(不考慮成績并列情況)*select * from (select sno,cno,score,row_number()over(partition by cno order by score desc) rn from sc)where rn1;*

23、27. 27、1981 年出生的學生名單(注:Student 表中Sage 列的類型是number)*select sno,sname,sage,ssex from student t where to_char(sysdate,yyyy)-sage =1988*28. 28、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列*select cno,avg(score) from sc group by cno order by avg(score)asc,cno desc;*29. 29、查詢平均成績大于85 的所有學生的學號、姓名和平均成績*select st

24、.sno,st.sname,avg(score) from student stleft join sc on sc.sno=st.snogroup by st.sno,st.sname having avg(score)85;*30. 30、查詢課程名稱為“數(shù)據(jù)庫,且分數(shù)低于60 的學生姓名和分數(shù)*select sname,score from student st,sc,course cwhere st.sno=sc.sno and sc o=c o and c ame=Oracle and sc.score70*33. 33、查詢不及格的課程,并按課程號從大到小排列*select sc.

25、sno,c ame,sc.score from sc,course cwhere sc o=c o and sc.score80;*35. 35、求選了課程的學生人數(shù)*select count(distinct sno) from sc;*y有問題36、查詢選修“諶燕老師所授課程的學生中,成績最高的學生姓名及其成績*select st.sname,score from student st,sc ,course c,teacher twherest.sno=sc.sno and sc o=c o and c.tno=t.tnoand t.tname=諶燕 and sc.score=(select max(score)from sc where sc o=c o)*37. 37、查詢各個課程及相應的選修人數(shù)*select cno,count(sno) from sc group by cno;*38. 38、查詢不同課程成績相同的學生的學號、課程號、學生成績*select a.* from sc a ,sc b where a.score=b.score and a ob o*

溫馨提示

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

評論

0/150

提交評論