SQL數(shù)據(jù)庫面試題以及答案_第1頁
SQL數(shù)據(jù)庫面試題以及答案_第2頁
SQL數(shù)據(jù)庫面試題以及答案_第3頁
已閱讀5頁,還剩14頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、公司內(nèi)部編號(hào):(GOOD-TMMT-MMUT-UUPTY-UUYY-DTTI-Student (Sti, Sname, Sage, Ssex)學(xué)生表S#:學(xué)號(hào)Sname:學(xué)生姓夕iSage:學(xué)生年齡Ssex:學(xué)生性別Course (C#> Cname, Ttt)課程表C#:課程編號(hào)Cname:課程夕i稱T#:教師編號(hào)SC (Stt, C#> score)成績表S#:學(xué)號(hào)c#:課程編號(hào)score: 成績Teacher 仃#, Tname)教師表T#:教師編號(hào):Tname:教師名字問題:1、查詢"001"課程比“002”課程成績高的所有學(xué)生的學(xué)號(hào) select #

2、from (select Stt, score from SC where Ctt001')a, (select s#, sc ore from SC where c#二'002')b Where > and # 二#;2、查詢平均成績大于60分的同學(xué)的學(xué)號(hào)和平均成績select S#, avg (score) from sc group by S# having avg (score)>603、查詢所有同學(xué)的學(xué)號(hào)、姓名、選課數(shù)、總成績select #, , countft), sum(score) from student left outer join

3、SC on # 二# group by Sname4、查詢姓'李'的老師的個(gè)數(shù):select count(distinct(Tname)from teacherwhere tname like '李幣;5、查詢沒有學(xué)過“葉平”老師可的同學(xué)的學(xué)號(hào)、姓名:select #,from Studentwhere S# not in (select distinct#) from SC,Course, Teacher where #=# AND tt二# AND葉平');6、查詢學(xué)過“葉平”老師所教的所有課的同學(xué)的學(xué)號(hào)、姓名:select S#, Snamefrom St

4、udentwhere S# in (select Stt from SC , Course , Teacherwhere tt=# and #二#and 二'葉平'group by Stthaving count#)二(select count (Ctt) from Course, Teacherwhere #=# and Tname二'葉平');7、查詢學(xué)過“011”并且也學(xué)過編號(hào)“002”課程的同學(xué)的學(xué)號(hào)、姓名:select #,from Student, SC where #=#and #二'00T andexists ( Select * fro

5、m SC as SC_2 where #=# and it二'002);8、查詢課程編號(hào)“002”的成績比課程編號(hào)“001”課程低的所有同學(xué)的學(xué)號(hào)、姓名:Select S#, Snamefrom (select,score ,(select score from SC SC_2 where ft二# and ft二'002") score2 from Student, SC where #二# and Ctt二'001)S_2where score2 < score;9、查詢所有課程成績小于60的同學(xué)的學(xué)號(hào)、姓名:select S#> snamef

6、rom studentwhere s# not in(select # from student, sc where ft 二# and score>60);10、查詢沒有學(xué)全所有課的同學(xué)的學(xué)號(hào)、姓名:select #,from student, scwhere #=#group by ft,having count (ctt) < (select count (c#) from course);11、查詢至少有一門課與學(xué)號(hào)為“1001”同學(xué)所學(xué)相同的同學(xué)的學(xué)號(hào)和姓名:select s#, Snamefrom Student, SCwhere it = it and ctt in

7、(select c# from SC where s#二T001/ );12、查詢至少學(xué)過學(xué)號(hào)為“001”同學(xué)所有一門課的其他同學(xué)學(xué)號(hào)和姓名;select distinct # , snamefrom student, scwhere #=#and eft in (select C# from sc where s#001);13、把“SC”表中“葉平”老師教的課的成績都更改為此課程的平均成績:Update Sc Set Score= (Select Avg(s2_Score) From sc s2 Where #二#)Where c# IN(Select cU From sc cs INNE

8、R JOIN Teacher tc ON #=# WHERE tname 二'葉平')14、查詢和“1002”號(hào)的同學(xué)學(xué)習(xí)的課程完全相同的其他同學(xué)學(xué)號(hào)和姓名:select s# from sc where ett in(select eft from sc where s#二'1002)group by s# having count (*)=(select count(*) from sc where s#二T002');15、刪除學(xué)習(xí)“葉平”老師課的SC表記錄:delect scfrom course, Teacherand n=n and tname二&#

9、39;葉平';16、向SC表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號(hào)“003”課程的同學(xué)學(xué)號(hào)、002號(hào)課的平均成績:Insert SC select Stt,'002 ,(Select avg(score) from SC where C#二'002)from Student where S# not in (Select S# from SC where C#二'002);17、按平均成績從高到低顯示所有學(xué)生的“數(shù)據(jù)庫”、“企業(yè)管理”、“英語” 三門的課程成績,按如下形式顯示:學(xué)生ID,數(shù)據(jù)庫,企業(yè)管理,英語,有效課 程數(shù),有效平均分:selec

10、t s# as 學(xué)生 ID,(selectscorefromscwhere#=#andctt="004,)as數(shù)據(jù)庫,(selectscorefromscwhere#二#andcti="001)as企業(yè)管理,(selectscorefromscwhere#二#andctt="006)as英iff,count (*) as有效課程數(shù),avg as半局成績 from sc as t group by s# order by avg18、查詢各科成績最高和最低的分:以如下的形式顯示:課程ID,最高分,最 低分select # as課程ID, as最高分,as最低分fro

11、m sc L, sc Rwhere # = #and 二(select maxfrom sc IL, student as IMwhere #二# and #二#group by ft)and 二(select minfrom sc as IRwhere #二#group by #);19、按各科平均成績從低到高和及格率的百分?jǐn)?shù)從高到低順序:SELECT n AS 課程號(hào),maxAS課程夕i, isnull (AVG (score), 0) AS 平均成績,100 * SUM(CASE WHEN isnull(score, 0)>=60 THEN 1 ELSE 0 END)/COUNT(

12、*) AS及格百分?jǐn)?shù)FROM SC T, CoursewhereGROUP BY #ORDER BY 100 * SUM (CASE WHEN isnull (score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC20、查詢?nèi)缦抡n程平均成績和及格率的百分?jǐn)?shù)(用” 1行”顯示):企業(yè)管理(001),馬克思(002) , OO&UML (003),數(shù)據(jù)庫(004):21、查詢不同老師所教不同課程平均分從高到低顯示:SELECT max#) AS 教師 ID,MAX AS教師姓名,# AS課程ID,AVG (Score) AS平均成績FROM SC

13、AS T, Course AS C , Teacher AS Zwhere #=# and #=#GROUP BY ttORDER BY AVG (Score) DESC22、查詢?nèi)缦抡n程成績第3名到第6名的學(xué)生成績單:企業(yè)管理(001),馬克思(002), UML(003),數(shù)據(jù)庫(004):23、統(tǒng)計(jì)下列各科成績,各分?jǐn)?shù)段人數(shù):課程ID,課程名稱,100-85, 85-70, 70-60,小于 60:SELECT # as 課程 ID,Cname as 課程名稱,SUM(CASE WHENscoreBETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS 100-

14、85SUM(CASE WHENscoreBETWEEN 70 AND 85 THEN1 ELSE 0 END)AS85-70,SUM(CASE WHENscoreBETWEEN 60 AND 70 THEN1 ELSE 0 END)AS70-60,SUM(CASE WHENscore< 60 THEN 1 ELSE 0 END)AS 60 -FROM SC, Coursewhere #=#GROUP BY #,Cname;24、查詢學(xué)生平均成績及其名次:SELECT 1+(SELECT COUNT( distinct 平均成績)FROM (SELECT Stt, AVG (score)

15、AS 平均成績FROM SCGROUP BY Stt ) AS T1 WHERE平均成績> T2.平均成績)as名次,Sit as學(xué)生學(xué)號(hào),平均成績FROM (SELECT S#, AVG (score)平均成績FROM SCGROUP BY Stt ) AS T2ORDER BY平均成績desc;25、查詢各科成績前三名的記錄(不考慮成績并列情況):SELECT n as 學(xué)生 ID,# as 課程 ID, Score as 分?jǐn)?shù)FROM SC tlWHERE score IN(SELECT TOP 3 scoreFROM SCWHERE tt= C#ORDER BY score DE

16、SC)26、查詢每門課程被選修的學(xué)生數(shù):select c#> count(s#)from sc group by c#;27、查詢出只選修一門課程的全部學(xué)生的學(xué)號(hào)和姓洛: select #, , count (c#) as 選課數(shù)from sc,studentwhere # 二#group by ft,having count (ctt)=1;28、查詢男生、女生人數(shù):select count (Ssex) as 男生人數(shù)from studentgroup by Ssexhaving Ssex二'男';select count (Ssex) as 女生人數(shù)from stu

17、dentgroup by Ssexhaving Ssex=l女';29、查詢姓“張”的學(xué)生名單:select sname from studentwhere sname like '張,;30、查詢同名同姓的學(xué)生名單,并統(tǒng)計(jì)同名人數(shù):select sanme, count(*)from studentgroup by snamehavang count(*)>1;31、1981年出生的學(xué)生名單(注:student表中sage列的類型是datetime):select sname, convert(char(11), DATEPART (year,sage) as agef

18、rom studentwhere convert (char (11), DATEPART (year, Sage) ='1981'32、查詢平均成績大于85的所有學(xué)生的學(xué)號(hào)、姓名和平均成績:select Sname, # , avg(score)from Student, SCwhere #=#group by ft, Sname having avg (score)>85;33、查詢每門課程的平均成績,結(jié)果按平均成績升序排序,平均成績相同時(shí),按課程號(hào)降序排列:select Ctt, avg(score)from scgroup by c#order by avg (s

19、core), ctt desc;34、查詢課程名稱為“數(shù)據(jù)庫”,且分?jǐn)?shù)低于60的學(xué)生名字和分?jǐn)?shù):select sname, isnull (score, 0)from student, sc ,coursewhere #=# and #=# and 二'數(shù)據(jù)庫'and score<60;35、查詢所有學(xué)生的選課情況:select #,#, sname, cnamefrom sc,student coursewhere #=# and it二#;36、查詢?nèi)魏我婚T課程成績在70分以上的姓名、課程名稱和分?jǐn)?shù): select distinct , #,from student,

20、 sc where >=70 and37、查詢不及格的課程,并按課程號(hào)從大到小的排列: select c#from scwhere score<60order by c#;38、查詢課程編號(hào)為“003”且課程成績在80分以上的學(xué)生的學(xué)號(hào)和姓名:select #,from sc,studentwhere #=# and score>80 and c#二'003 ;39、求選了課程的學(xué)生人數(shù):select count(*) from sc;40、査詢選修“葉平”老師所授課程的學(xué)生中,成績最高的學(xué)生姓名及其成績:select ,scorefrom student, sc,

21、course c, teacherwhere #=# and #二#and n=nand二'葉平' and = (select max(score) from sc where c#=#);41、查詢各個(gè)課程及相應(yīng)的選修人數(shù):select count (*) from sc group by ctt;42、查詢不同課程成績相同的學(xué)生和學(xué)號(hào)、課程號(hào)、學(xué)生成績:select distinctfrom sc a , sc bwhere =and #<>#;43、查詢每門課程成績最好的前兩名:select # as 學(xué)生 ID, # 課程 ID, Score as 分?jǐn)?shù)from sc tlwhere score in (select top 2 score from scwhereorder by score desc)order by ft;44、統(tǒng)計(jì)每門課程的學(xué)生選修人數(shù)(超過10人的課程才統(tǒng)計(jì))。要求輸出課程號(hào)和選修人數(shù),查詢結(jié)果按人數(shù)降丿宇

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論