T-SQL 面試題(查詢(xún)).docx_第1頁(yè)
T-SQL 面試題(查詢(xún)).docx_第2頁(yè)
T-SQL 面試題(查詢(xún)).docx_第3頁(yè)
T-SQL 面試題(查詢(xún)).docx_第4頁(yè)
T-SQL 面試題(查詢(xún)).docx_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

Student(S#,Sname,Sage,Ssex) 學(xué)生表Course(C#,Cname,T#) 課程表SC(S#,C#,score) 成績(jī)表Teacher(T#,Tname) 教師表問(wèn)題:1、查詢(xún)“001”課程比“002”課程成績(jī)高的所有學(xué)生的學(xué)號(hào);select a.S# from (select s#,score from SC where C#=001) a,(select s#,scorefrom SC where C#=002) bwhere a.scoreb.score and a.s#=b.s#;2、查詢(xún)平均成績(jī)大于60分的同學(xué)的學(xué)號(hào)和平均成績(jī);select S#,avg(score)from scgroup by S# having avg(score) 60;3、查詢(xún)所有同學(xué)的學(xué)號(hào)、姓名、選課數(shù)、總成績(jī);select Student.S#,Student.Sname,count(SC.C#),sum(score)from Student left Outer join SC on Student.S#=SC.S#group by Student.S#,Sname4、查詢(xún)姓“李”的老師的個(gè)數(shù);select count(distinct(Tname)from Teacherwhere Tname like 李%;5、查詢(xún)沒(méi)學(xué)過(guò)“葉平”老師課的同學(xué)的學(xué)號(hào)、姓名;select Student.S#,Student.Snamefrom Studentwhere S# not in (select distinct( SC.S#) from SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname=葉平);6、查詢(xún)學(xué)過(guò)“001”并且也學(xué)過(guò)編號(hào)“002”課程的同學(xué)的學(xué)號(hào)、姓名;select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#=001and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#=002);7、查詢(xún)學(xué)過(guò)“葉平”老師所教的所有課的同學(xué)的學(xué)號(hào)、姓名;select S#,Snamefrom Studentwhere S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname=葉平 group by S# having count(SC.C#)=(select count(C#) from Course,Teacher where Teacher.T#=Course.T# and Tname=葉平);8、查詢(xún)課程編號(hào)“002”的成績(jī)比課程編號(hào)“001”課程低的所有同學(xué)的學(xué)號(hào)、姓名;Select S#,Sname from (select Student.S#,Student.Sname,score ,(select score from SC SC_2 where SC_2.S#=Student.S# and SC_2.C#=002) score2from Student,SC where Student.S#=SC.S# and C#=001) S_2 where score2 60);10、查詢(xún)沒(méi)有學(xué)全所有課的同學(xué)的學(xué)號(hào)、姓名;select Student.S#,Student.Snamefrom Student,SCwhere Student.S#=SC.S# group by Student.S#,Student.Sname having count(C#) =60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分?jǐn)?shù)FROM SC T,Coursewhere t.C#=course.C#GROUP BY t.C#ORDER BY 100 * SUM(CASE WHEN isnull(score,0)=60 THEN 1 ELSE 0 END)/COUNT(*) DESC20、查詢(xún)?nèi)缦抡n程平均成績(jī)和及格率的百分?jǐn)?shù)(用1行顯示): 企業(yè)管理(001),馬克思(002),OO&UML (003),數(shù)據(jù)庫(kù)(004)SELECT SUM(CASE WHEN C# =001 THEN score ELSE 0 END)/SUM(CASE C# WHEN 001 THEN 1 ELSE 0 END) AS 企業(yè)管理平均分,100 * SUM(CASE WHEN C# = 001 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = 001 THEN 1 ELSE 0 END) AS 企業(yè)管理及格百分?jǐn)?shù),SUM(CASE WHEN C# = 002 THEN score ELSE 0 END)/SUM(CASE C# WHEN 002 THEN 1 ELSE 0 END) AS 馬克思平均分,100 * SUM(CASE WHEN C# = 002 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = 002 THEN 1 ELSE 0 END) AS 馬克思及格百分?jǐn)?shù),SUM(CASE WHEN C# = 003 THEN score ELSE 0 END)/SUM(CASE C# WHEN 003 THEN 1 ELSE 0 END) AS UML平均分,100 * SUM(CASE WHEN C# = 003 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = 003 THEN 1 ELSE 0 END) AS UML及格百分?jǐn)?shù),SUM(CASE WHEN C# = 004 THEN score ELSE 0 END)/SUM(CASE C# WHEN 004 THEN 1 ELSE 0 END) AS 數(shù)據(jù)庫(kù)平均分,100 * SUM(CASE WHEN C# = 004 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = 004 THEN 1 ELSE 0 END) AS 數(shù)據(jù)庫(kù)及格百分?jǐn)?shù)FROM SC21、查詢(xún)不同老師所教不同課程平均分從高到低顯示SELECT max(Z.T#) AS 教師ID,MAX(Z.Tname) AS 教師姓名,C.C# AS 課程,MAX(C.Cname) AS 課程名稱(chēng),AVG(Score) AS 平均成績(jī)FROM SC AS T,Course AS C ,Teacher AS Zwhere T.C#=C.C# and C.T#=Z.T#GROUP BY C.C#ORDER BY AVG(Score) DESC22、查詢(xún)?nèi)缦抡n程成績(jī)第 3 名到第 6 名的學(xué)生成績(jī)單:企業(yè)管理(001),馬克思(002),UML (003),數(shù)據(jù)庫(kù)(004)學(xué)生ID,學(xué)生姓名,企業(yè)管理,馬克思,UML,數(shù)據(jù)庫(kù),平均成績(jī)SELECT DISTINCT top 3SC.S# As 學(xué)生學(xué)號(hào),Student.Sname AS 學(xué)生姓名 ,T1.score AS 企業(yè)管理,T2.score AS 馬克思,T3.score AS UML,T4.score AS 數(shù)據(jù)庫(kù),ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) as 總分FROM Student,SC LEFT JOIN SC AS T1ON SC.S# = T1.S# AND T1.C# = 001LEFT JOIN SC AS T2ON SC.S# = T2.S# AND T2.C# = 002LEFT JOIN SC AS T3ON SC.S# = T3.S# AND T3.C# = 003LEFT JOIN SC AS T4ON SC.S# = T4.S# AND T4.C# = 004WHERE student.S#=SC.S# andISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)NOT IN(SELECTDISTINCTTOP 15 WITH TIESISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)FROM scLEFT JOIN sc AS T1ON sc.S# = T1.S# AND T1.C# = k1LEFT JOIN sc AS T2ON sc.S# = T2.S# AND T2.C# = k2LEFT JOIN sc AS T3ON sc.S# = T3.S# AND T3.C# = k3LEFT JOIN sc AS T4ON sc.S# = T4.S# AND T4.C# = k4ORDER BY ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) DESC);23、統(tǒng)計(jì)列印各科成績(jī),各分?jǐn)?shù)段人數(shù):課程ID,課程名稱(chēng),100-85,85-70,70-60, 60SELECT SC.C# as 課程ID, Cname as 課程名稱(chēng),SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS 100 - 85,SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS 85 - 70,SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS 70 - 60,SUM(CASE WHEN score T2.平均成績(jī)) as 名次,S# as 學(xué)生學(xué)號(hào),平均成績(jī)FROM (SELECT S#,AVG(score) 平均成績(jī)FROM SCGROUP BY S#) AS T2ORDER BY 平均成績(jī) desc;25、查詢(xún)各科成績(jī)前三名的記錄:(不考慮成績(jī)并列情況)SELECT t1.S# as 學(xué)生ID,t1.C# as 課程ID,Score as 分?jǐn)?shù)FROM SC t1WHERE score IN (SELECT TOP 3 scoreFROM SCWHERE t1.C#= C#ORDER BY score DESC)ORDER BY t1.C#;26、查詢(xún)每門(mén)課程被選修的學(xué)生數(shù)select c#,count(S#) from sc group by C#;27、查詢(xún)出只選修了一門(mén)課程的全部學(xué)生的學(xué)號(hào)和姓名select SC.S#,Student.Sname,count(C#) AS 選課數(shù)from SC ,Studentwhere SC.S#=Student.S# group by SC.S# ,Student.Sname having count(C#)=1;28、查詢(xún)男生、女生人數(shù)Select count(Ssex) as 男生人數(shù) from Student group by Ssex having Ssex=男;Select count(Ssex) as 女生人數(shù) from Student group by Ssex having Ssex=女;29、查詢(xún)姓“張”的學(xué)生名單SELECT Sname FROM Student WHERE Sname like 張%;30、查詢(xún)同名同性學(xué)生名單,并統(tǒng)計(jì)同名人數(shù)select Sname,count(*) from Student group by Sname having count(*)1;31、1981年出生的學(xué)生名單(注:Student表中Sage列的類(lèi)型是datetime)select Sname, CONVERT(char (11),DATEPART(year,Sage) as agefrom studentwhere CONVERT(char(11),DATEPART(year,Sage)=1981;32、查詢(xún)每門(mén)課程的平均成績(jī),結(jié)果按平均成績(jī)升序排列,平均成績(jī)相同時(shí),按課程號(hào)降序排列Select C#,Avg(score) from SC group by C# order by Avg(score),C# DESC ;33、查詢(xún)平均成績(jī)大于85的所有學(xué)生的學(xué)號(hào)、姓名和平均成績(jī)select Sname,SC.S# ,avg(score)from Student,SCwhere Student.S#=SC.S# group by SC.S#,Sname havingavg(score)85;34、查詢(xún)課程名稱(chēng)為“數(shù)據(jù)庫(kù)”,且分?jǐn)?shù)低于60的學(xué)生姓名和分?jǐn)?shù)Select Sname,isnull(score,0)from Student,SC,Coursewhere SC.S#=Student.S# and SC.C#=Course.C# and Course.Cname=數(shù)據(jù)庫(kù)and score =70 AND SC.S#=student.S#;37、查詢(xún)不及格的課程,并按課程號(hào)從大到小排列select c# from sc where scor e 80 and C#=003;39、求選了課程的學(xué)生人數(shù)select count(*) from sc;40、查詢(xún)選修“葉平”老師所授課程的學(xué)生中,成績(jī)最高的學(xué)生姓名及其成績(jī)select Student.Sname,scorefrom Student,SC,Course C,Teacherwhere Student.S#=SC.S# and SC.C#=C.C# and C.T#=Teacher.T# and Teacher.Tname=葉平 and SC.score=(select max(score)from SC where C#=C.C# );41、查詢(xún)各個(gè)課程及相應(yīng)的選修人數(shù)select count(*) from sc group by C#;42、查詢(xún)不同課程成績(jī)相同的學(xué)生的學(xué)號(hào)、課程號(hào)、學(xué)生成績(jī)select distinct A.S#,B.score from SC A ,SC B where A.Score=B.Score and A.C# B.C# ;43、查詢(xún)每門(mén)功成績(jī)最好的前兩名SELECT t1.S# as 學(xué)生ID,t1.C# as 課程ID,Score as 分?jǐn)?shù)FROM SC t1WHERE score IN (SELECT TOP 2 scoreFROM SCWHERE t1.C#= C#ORDER BY score DESC)ORDER BY t1.C#;44、統(tǒng)計(jì)每門(mén)課程的學(xué)生選修人數(shù)(超過(guò)10人的課程才統(tǒng)計(jì))。要求輸出課程號(hào)和選修

溫馨提示

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

評(píng)論

0/150

提交評(píng)論