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

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上sql語句練習(xí)題1數(shù)據(jù)庫有如下四個表格:student(sno,sname,sage,ssex,sdpt) 學(xué)生表系表(dptno,dname)course(cno,cname, gradet, tno) 課程表sc(sno,cno,score) 成績表teacher(tno,tname) 教師表 要求:完成以下操作1. 查詢姓"歐陽"且全名為三個漢字的學(xué)生的姓名。select sname from student  where sname like “歐陽_;2. 查詢名字中第

2、2個字為"陽"字的學(xué)生的姓名和學(xué)號。 select sname,sno from student  where sname like '_陽%';3. 查詢所有不姓劉的學(xué)生姓名。select sname,sno,ssex from student  where sname not like “劉%”;4. 查詢db_design課程的課程號和學(xué)分。 select cno,ccred

3、it from course where cname like 'db_design' 5. 查詢以"db_"開頭,且倒數(shù)第3個字符為i的課程的詳細(xì)情況。 select * from course  where cname like 'db%i_ _';6. 某些學(xué)生選修課程后沒有參加考試,所以有選課記錄,但沒有考試成績。查詢?nèi)鄙俪煽兊膶W(xué)生的學(xué)號和相應(yīng)的課程號。select sno,c

4、no from sc where grade is null;7. 查所有有成績的學(xué)生學(xué)號和課程號。select sno,cno from sc where grade is not null;8. 查詢計算機(jī)系年齡在20歲以下的學(xué)生姓名。 select sname from student  where sdept= 'cs' and sage<

5、;20;9. 查詢選修了3號課程的學(xué)生的學(xué)號及其成績,查詢結(jié)果按分?jǐn)?shù)降序排列。 select sno,grade from sc  where cno= ' 3 '  order by grade desc;10. 查詢學(xué)生總?cè)藬?shù)。select count(*) from student;11. 查詢選修了課程的學(xué)生人數(shù)。select count(distinct sno) from

6、0;sc;12. 計算1號課程的學(xué)生平均成績。select avg(grade) from sc  where cno= ' 1 ';13. 查詢選修1號課程的學(xué)生最高分?jǐn)?shù)。select max(grade) from sc  where cno= ' 1 ';14. 查詢學(xué)生選修課程的總學(xué)分?jǐn)?shù)。select sum(grade) from sc,course wh

7、ere sno= '  ' and o=o;15. 查詢選修了3門以上課程的學(xué)生學(xué)號。select sno from sc group by sno  having count(*) >3;  16. 查詢每個學(xué)生及其選修課程的情況。select student.*,sc.*, course.* from student,sc , course where student.sno=sc.sno and o=o;17. 查詢每

8、個學(xué)生及其選修課程的情況包括沒有選修課程的學(xué)生18. 查詢選修2號課程且成績在90分以上的所有學(xué)生的學(xué)號、姓名select student.sno, student.snamefrom student,scwhere student.sno=sc.sno and o=”2and sc.grade>90;19. 查詢每個學(xué)生的學(xué)號、姓名、選修的課程名及成績。select student.sno,sname,ssex,sage,sdept,cno,gradefrom student left outjoin sco on(student.sno=sc.sno);20. 查詢與“劉晨”在同一個

9、系學(xué)習(xí)的學(xué)生。selectsno,sname,sdeptfrom studentwhere sdept in(select sdept from student where sname=”劉晨);21. 查詢選修了課程名為“信息系統(tǒng)”的學(xué)生學(xué)號和姓名select sno,sname from student where sno in(select sno from sc where cno in (select cno from course where cname=”信息系統(tǒng));22. 找出每個學(xué)生超過他選修課程平均成績的課程號。select sno,cno from sc x where

10、grade>=(select avg(grade) from sc y where y.sno=x.sno);23. 將一個新學(xué)生記錄(學(xué)號:;姓名:陳冬;性別:男;所在系:is;年齡:18歲)插入到student表中。insert into student values ('','陳冬','男','is',18);24. 將學(xué)生的年齡改為22歲。update student setsage=22 where sno='';25. 將所有學(xué)生的年齡增加1歲。update student setsage=sag

11、e+1;26. 將計算機(jī)科學(xué)系全體學(xué)生的成績置零。update sc set grade=0 where exits(selete * from student where student.sno=sc.sno and sdept=” 計算機(jī)科學(xué)系”);27. 刪除學(xué)號為的學(xué)生記錄delete from student where sno=”';28. 刪除所有的學(xué)生選課記錄。delete from sc;29. 刪除2號課程的所有選課記錄。delete from sc where cno='2';30. 刪除計算機(jī)科學(xué)系所有學(xué)生的選課記錄。delete from sc

12、 where sno in (selete sno from student where sdept=” 計算機(jī)科學(xué)系”);31. 建立信息系學(xué)生的視圖。 create view is_student asselect sno,sname,sage from student where sdept='is';sql語句練習(xí)題2設(shè)教學(xué)數(shù)據(jù)庫education,有三個關(guān)系:  學(xué)生關(guān)系s(sno,sname,age,sex,sdept); 學(xué)習(xí)關(guān)系sc(sno,cno,grade);  課程關(guān)系c(cno,cname,cdept,tname)

13、 查詢問題:  1:查所有年齡在20歲以下的學(xué)生姓名及年齡。select sname,sagefrom swhere sage<20;(not age>=20);2:查考試成績有不及格的學(xué)生的學(xué)號  select distinct snofrom scwhere grade<60;3:查所年齡在20至23歲之間的學(xué)生姓名、系別及年齡。select sname,sdept,sagefrom swhere sage between 20 and 23;4:查計算機(jī)系、數(shù)學(xué)系、信息系的學(xué)生姓名、性別。 select sname,ssex fr

14、om s where sdept in(cs,is,math);5:查既不是計算機(jī)系、數(shù)學(xué)系、又不是信息系的學(xué)生姓名、性別 select sname,ssex from s where sdept not in(cs,is,math);  6:查所有姓“劉”的學(xué)生的姓名、學(xué)號和性別。 select sname,sno,ssex from s where sname like劉%;7:查姓“上官”且全名為3個漢字的學(xué)生姓名。 select sname from s where sname like 上官_;8:查所有不姓“張”的學(xué)生的姓名。 se

15、lect sname,sno,ssex from s where sname not like 張%;9:查db_design課程的課程號。select cno from c where cname like db_design;10:查缺考的學(xué)生的學(xué)號和課程號。 select sno,cno from sc where grade is null;11:查年齡為空值的學(xué)生的學(xué)號和姓名。 select sno,sname from s where sage is null;12:查計算機(jī)系20歲以下的學(xué)生的學(xué)號和姓名。select sno,snamefrom swhere

16、sdept=cs and sage<20;13:查計算機(jī)系、數(shù)學(xué)系、信息系的學(xué)生姓名、性別。select sname,ssexfrom swhere sdept=cs or sdept=is or sdept=math;14:查詢選修了c3課程的學(xué)生的學(xué)號和成績,其結(jié)果按分?jǐn)?shù)的降序排列。select sno,gradefrom scwhere cno=c3order by grade desc;15:查詢?nèi)w學(xué)生的情況,查詢結(jié)果按所在系升序排列,對同一系中的學(xué)生按年齡降序排列。select * from sorder by sdep,sage desc;16:查詢學(xué)生總?cè)藬?shù)。select

17、 count(*) from s;17:查詢選修了課程的學(xué)生人數(shù)。 select count(distinct sno) from sc18:計算選修了c1課程的學(xué)生平均成績。select avg(grade)from scwhere cno=c1;19:查詢學(xué)習(xí)c3課程的學(xué)生最高分?jǐn)?shù)。select max(grade)from scwhere cno=c3;20:查詢各個課程號與相應(yīng)的選課人數(shù)。select cno, count(sno)from scgroup by cno;  21:查詢計算機(jī)系選修了3門以上課程的學(xué)生的學(xué)號。select snofrom scwher

18、e sdept=csgroup by snohaving count(*)>3; 22:求基本表s中男同學(xué)的每一年齡組(超過50人)有多少人?要求查詢結(jié)果按人數(shù)升序排列,人數(shù)相同按年齡降序排列。select sage,count(sno)from swhere ssex='m'group by sagehaving count(*)>50order by 2,sage desc;23:查詢每個學(xué)生及其選修課程的情況。select s.sno, sname, sage, ssex, sdept, cno, gradefrom s, scwhere s.sno

19、=sc.sno;24:查詢選修了c2課程且成績在90分以上的所有學(xué)生。select s.sno,snamefrom s,scwhere s.sno=sc.snoand o=c2 and sc.grade>90; 25:查詢每個學(xué)生選修的課程名及其成績。select s.sno,sname,cname,sc.gradefrom s,sc,cwhere s.sno=sc.sno and o=o 26:統(tǒng)計每一年齡選修課程的學(xué)生人數(shù)。 select sage,count(distinct s.sno)from s,scwhere s.sno=sc.snogroup

20、 by sage;27:查詢選修了c2課程的學(xué)生姓名。select sname from s where sno in(select sno from sc where cno=c2);28:查詢與“張三”在同一個系學(xué)習(xí)的學(xué)生學(xué)號、姓名和系別。select sno,sname,sdept from where sdept=(select sdept from s where sname=張三); 29:查詢選修課程名為“數(shù)據(jù)庫”的學(xué)生學(xué)號和姓名。select sno,sname from s where sno in(select sno from sc where cno in (

21、select cno from c where cname=db); 30:查詢與“張三”在同一個系學(xué)習(xí)的學(xué)生學(xué)號、姓名和系別。select sno,sname,sdept from s where sdept=(select sdept from s where sname=張三); 31:查詢選修課程名為“數(shù)據(jù)庫”的學(xué)生學(xué)號和姓名。select sno,sname from s where sno in (select sno from sc where cno=(select cno from c where cname=db);32:查詢選修了c2課程的學(xué)生姓名。1

22、. select sname from s where sno in(select sno from sc where cno=c2);2. select sname from s where exists(select * from sc where sc.sno=s.sno and cno=c2); 33:查詢選修了全部課程的學(xué)生姓名。select sname from s where not exists(select * from c where not exists(select * from sc where sc.sno=s.sno and o=o);  36

23、:查詢所學(xué)課程包含學(xué)生s3所學(xué)課程的學(xué)生學(xué)號 select distinct sno from sc as x where not exists(select * from sc as y where y.sno=s3 and not exists(select * from sc as z where z.sno=x.sno and o=o);sql語句練習(xí)題 3一、簡單查詢1、列出全部學(xué)生的信息。select * from學(xué)生2、列出軟件專業(yè)全部學(xué)生的學(xué)號及姓名。select學(xué)號,姓名from學(xué)生where專業(yè)="軟件"3、列出所有必修課的課號。select

24、distinct課號from必修課4、求1號課成績大于80分的學(xué)生的學(xué)號及成績,并按成績由高到低列出。select學(xué)號,成績from選課where課號="1"and 成績>80 order by成績desc5、列出非軟件專業(yè)學(xué)生的名單。方法一:select姓名from學(xué)生where專業(yè)<>"軟件"方法二:select姓名from學(xué)生where not專業(yè)="軟件"方法三:select姓名from學(xué)生where專業(yè)!="軟件"6、查詢成績在7080分之間的學(xué)生選課得分情況方法一:select*fro

25、m選課where成績>=70and成績<=80方法二:select*from選課where成績between70and80不在此范圍內(nèi)的查詢:(注意寫出和以下語句等價的語句)select * from 選課 where成績not between70and807、列出選修1號課或3號課的全體學(xué)生的學(xué)號和成績。方法一:select學(xué)號,成績from選課where課號="1"or課號="3"方法二:select學(xué)號,成績from選課where課號in("1","3")相反條件查詢:select學(xué)號,成績fro

26、m選課where課號notin("1","3")8、列出所有98級學(xué)生的學(xué)生成績情況。select*from選課where學(xué)號like"98%"select*from選課where學(xué)號like"98_"相反條件查詢:select*from選課where學(xué)號notlike"98%"9、列出成績?yōu)榭罩?或不為空值)的學(xué)生的學(xué)號和課號。答案一:select學(xué)號,課號from選課where成績isnull答案二:select學(xué)號,課號from選課where成績isnotnull10、求出所有學(xué)生的總成績

27、。select sum(成績) as總成績 from 選課11、列出每個學(xué)生的平均成績。select學(xué)號,avg(成績) as 平均成績from選課group by學(xué)號12、列出各科的平均成績、最高成績、最低成績和選課人數(shù)。select課號,avg(成績)as平均成績,max(成績)as最高分,;min(成績)as最低分,count(學(xué)號) as 選課人數(shù) from 選課 group by 課號二、連接查詢(一)簡單連接1、列出選修1號課的學(xué)生姓名及成績。select 姓名,成績 from學(xué)生,選課 where學(xué)生.學(xué)號=選課.學(xué)號 and 課號="1"2、列出選修1號課的

28、學(xué)生的學(xué)號、姓名及成績。select學(xué)生.學(xué)號,姓名,成績from學(xué)生s,選課xwheres.學(xué)號=x.學(xué)號and課號="1"3、求出總分大于150的學(xué)生的學(xué)號、姓名及總成績。select學(xué)生.學(xué)號,姓名,sum(成績)as總成績from學(xué)生,選課;where學(xué)生.學(xué)號=選課.學(xué)號groupby選課.學(xué)號havingsum(成績)>150(二)自連接查詢1、列出那些專業(yè)相同的學(xué)生相應(yīng)的姓名及專業(yè)信息。select a.姓名,b.姓名,專業(yè)from學(xué)生a,學(xué)生bwherea.學(xué)號<>b.學(xué)號anda.專業(yè)=b.專業(yè)2、求至少選修1號課和2號課的學(xué)生的學(xué)號。s

29、electx.學(xué)號from選課x,選課ywherex.學(xué)號=y.學(xué)號andx.課號="1"andy.課號="2"3、有以下表rate.dbf幣種1代碼c(2)、幣種2代碼c(2)、買入價n(8,4)、賣出價n(8,4)外匯匯率.dbf幣種1c(4)、幣種2c(4)、買入價n(8,4)、賣出價n(8,4)外匯代碼.dbf外匯名稱c(10)、外匯代碼c(10)要求:將所有“外匯匯率”表中的數(shù)據(jù)插入rate表中并且順序不變,由于“外匯匯率”中的幣種1和幣種2存放的是外幣名稱,而rate表中的幣種1代碼和幣種2代碼應(yīng)該存放外幣代碼,所以插入時要做相應(yīng)的改動,外幣

30、名稱與外向代碼的對應(yīng)關(guān)系存儲在“外匯代碼”表中。selecta.外幣代碼as幣種1代碼,b.外幣代碼as幣種2代碼,;買入價,賣出價from外匯代碼a,外匯匯率,外匯代碼b;wherea.外幣名稱=外匯匯率.幣種1andb.外幣名稱=外匯匯率.幣種2intotablerate4、假定有“雇員”表(雇員號c(2),雇員姓名c(6),經(jīng)理號c(2),根據(jù)雇員關(guān)系列出上一級經(jīng)理及其所領(lǐng)導(dǎo)的職員清單。(教案中的例題)select"領(lǐng)導(dǎo)",s.雇員姓名,"雇員",e.雇員姓名from雇員s,雇員ewheres.雇員號=e.經(jīng)理(三)超連接1、列出選修1號課的學(xué)生姓

31、名及成績。方法一:(使用簡單連接查詢格式)select姓名,成績from學(xué)生,選課where學(xué)生.學(xué)號=選課.學(xué)號and課號="1"方法二:(使用內(nèi)部連接格式)select姓名,成績from學(xué)生innerjoin選課on學(xué)生.學(xué)號=選課.學(xué)號where課號="1"方法三:內(nèi)部連接的inner短語可以省略。(與方法二等價)select姓名,成績from學(xué)生join選課on學(xué)生.學(xué)號=選課.學(xué)號where課號="1"2、查詢訂貨管理數(shù)據(jù)庫中數(shù)據(jù)的倉庫號、城市、供應(yīng)商名和地址信息。方法一:使用簡單連接格式。select倉庫.倉庫號,城市,供

32、應(yīng)商名,地址from供應(yīng)商,訂購單,職工,倉庫;where供應(yīng)商.供應(yīng)商號=訂購單.供應(yīng)商號and訂購單.職工號=職工.職工號;and職工.倉庫號=倉庫.倉庫號方法二:使用超連接的內(nèi)部連接格式。(注意連接條件的順序)select倉庫.倉庫號,城市,供應(yīng)商名,地址from供應(yīng)商join訂購單join職工join倉庫;on職工.倉庫號=倉庫.倉庫號on訂購單.職工號=職工.職工號on供應(yīng)商.供應(yīng)商號=訂購單.供應(yīng)商號3、查詢沒有選修任何課程的學(xué)生姓名。方法一:使用嵌套查詢select姓名from學(xué)生where學(xué)號notin(select學(xué)號from選課)方法二:使用超連接的右連接。select姓名

33、from選課rightjoin學(xué)生on選課.學(xué)號=學(xué)生.學(xué)號where選課.學(xué)號<>學(xué)生.學(xué)號方法三:使用超連接的左連接。(注意表名順序和方法二的不同)select姓名from學(xué)生leftjoin選課on選課.學(xué)號=學(xué)生.學(xué)號where選課.學(xué)號<>學(xué)生.學(xué)號三、嵌套查詢(一)普通嵌套與謂詞exists1、列出選修匯編語言課的學(xué)生的學(xué)號。方法一:select學(xué)號from選課where課號=(select課號from課程where課名="匯編語言")方法二:使用謂詞exists。注意和方法一格式上的不同。select學(xué)號from選課whereexist

34、(select*from課程;where課名="匯編語言"and選課.課號=課程.課號)2、求軟件專業(yè)所有必修課的課程信息。方法一:select*from課程where課號in;(select課號from必修課where必修專業(yè)="軟件")方法二:select*from課程whereexist(select*from必修課where必修專業(yè)="軟件"and課程.課號=必修課.課號)(二)量詞any、some、all1、求選修2號課的學(xué)生中,成績比選修1號課的最低成績要高的學(xué)生的學(xué)號和成績。方法一:select學(xué)號,成績from選課wh

35、ere課號="2"and成績>(selectmin(成績)from選課where課號="1")方法二:any等價于some,所以可將any換成some。select學(xué)號,成績from選課where課號="2"and成績>any;(select成績from選課where課號="1")2、求選修2號課的學(xué)生中,成績比選修1號課的任何學(xué)生的成績都要高的那些學(xué)生的學(xué)號和成績。方法一:select學(xué)號,成績from選課where課號="2"and成績>(selectmax(成績)from

36、選課where課號="1")方法二:select學(xué)號,成績from選課where課號="2"and成績>all;(select成績from選課where課號="1")(三)內(nèi)外層互相關(guān)嵌套(外層依賴于內(nèi)層的查詢結(jié)果,內(nèi)層依賴于外層來進(jìn)一步查詢)1、列出每門課程中成績最高的選課信息。select*from選課awhere成績=(selectmax(成績)from選課bwherea.課號=b.課號)2、列出每個學(xué)生中成績低于本人平均成績的選課信息。select*from選課awhere成績<(selectavg(成績)from

37、選課bwherea.學(xué)號=b.學(xué)號)3、列出表“訂購單2.dbf”(其內(nèi)容就是在訂購單表的基礎(chǔ)上增加一個總金額字段)中每個職工經(jīng)手的具有最高總金額的訂購單信息。(教案中例題)selectout.職工號,out.供應(yīng)商號,out.訂貨單號,out.訂貨日期,out.總金額;from訂購單outwhere總金額=(selectmax(總金額)from訂購單inner1;whereout.職工號=inner1.職工號)四、操作功能1、在課程表中插入新的元組(5,大學(xué)英語)。insertinto課程(課號,課名)values("5","大學(xué)英語")2、給學(xué)生表中男生的記錄加上刪除標(biāo)記。deletefrom學(xué)生where性別="男"3、將

溫馨提示

  • 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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論