/*例1:查詢“c1”課程比“c2”課程高的所有學生*/select a.sno
from (select sno,score from sc where cno='c1') a,(select sno,score from sc where cno='c2') b
where a.sno=b.sno and a.score>b.score;/*例2:查詢平均成績大于的同學的學號和平均成績*/select sno,avg(score)
from sc
groupby sno
havingavg(score)>60;/*例3:查詢所有同學的學號、姓名、選課數、總成績*/select student.sno,sname,count(cno),sum(score)
from student,sc
where student.sno=sc.sno
groupby student.sno,sname;/*例4:查詢姓“李”的老師的個數*/selectcount(distinct(tname))
from teacher
where tname like'李%';/*例5:查詢沒學過“李玉”老師課的同學的學號和姓名*/select sno,sname
from student
where sno notin
( selectdistinct(sc.sno)
from course,sc,teacher
where sc.cno=course.cno and course.tno=teacher.tno and tname='李玉'
);/*例:6:查詢學過“c1”并且也學過“c2”課程的的同學的學號和姓名*/select student.sno,sname
from student,sc a,sc b
where student.sno=a.sno and student.sno=b.sno and a.cno='c1'and b.cno='c2';/*另一種做法*/select student.sno,sname
from student,sc
where student.sno=sc.sno and cno='c1'andexists
(select *
from sc a
where sc.sno=a.sno and a.cno='c2');/*例7:查詢學過“李玉”老師所教的所有課的同學的學號、姓名*/select sno,sname
from student
where sno in
( select sno
from course,sc,teacher
where sc.cno=course.cno and course.tno=teacher.tno and teacher.tname='李玉'groupby sno
havingcount(course.cno)=(selectcount(cno)
from course,teacher
where course.tno=teacher.tno and tname='李玉' ));/*例8:查詢課程編號“c1”的成績比課程編號“c2”課程低的所有同學的學號、姓名*/select sno,sname
from (select student.sno,sname,score,(select score from sc a where a.sno=student.sno and a.cno='c2') score2
from student,sc
where student.sno=sc.sno and cno='c1') b
where score2>score;/*另一種做法*/select student.sno,sname
from student,sc a,sc b
where student.sno=a.sno and student.sno=b.sno and a.cno='c1'and b.cno='c2'and a.score<b.score;/*例9:查詢所有課程成績小于60分的同學的學號、姓名*/select sno,sname
from student
where sno notin
(select a.sno
from student a,sc
where a.sno=sc.sno and score>=60);/*存在一個問題:沒有選課的同學也會被篩選出來*//*例10:查詢沒有學全所有課的同學的姓名、學號*/select student.sno,sname
from student,sc
where student.sno=sc.sno
groupby student.sno,sname
havingcount(cno)<
(selectcount(cno) from course)
--存在問題:沒有篩選出沒有選課的同學#
/*例11:查詢至少有一門課與學號為“1”的同學所學相同的同學的學號和姓名*/
select student.sno,sname
from student,sc
where student.sno=sc.sno and cno in (select cno from sc where sno='1') and student.sno!='1';/*例12:查詢至少學過學號為“1”同學所有一門課的其他同學的學號和姓名*/selectdistinct(student.sno),sname
from student,sc
where student.sno=sc.sno and cno in (select cno from sc where sno='1') and student.sno!='1';/*例13:把“sc”表中“李玉”老師教的課的成績都改為此課程的平均成績*/--與在mysql中的區別update SC
set score=(selectavg(SC_2.score)from SC SC_2 where SC_2.Cno=SC.cno )
from Course,Teacher
where Course.cno=SC.cno and Course.tno=Teacher.tno and Teacher.Tname='李玉';/*例14:查詢和“2”號的同學學習的課程完全相同的其他同學學號和姓名*/select sc.sno,sname
from sc innerjoin student on student.sno=sc.sno
where cno in (select cno from sc where sno='2')
groupby sc.sno,sname
havingcount(*)=(selectcount(*) from sc where sno='2')
/*例15:刪除學習“葉平”老師課的sc表的記錄*/
deletefrom sc
where cno=(select cno
from course,teacher
where course.tno=teacher.tno and tname='李玉');/*例16:向SC表中插入一些記錄,符合以下:沒有上過編號“”課程的同學學號,2號課的平均成績*/insertinto sc(sno,cno,score)
select sno,'c2',(selectavg(score) from sc where cno='c2')
from student
where sno notin (select sno from sc where cno='c2');/*例17:按平均成績從高到低顯示所有學生的“管理學”、“經濟學”、“R語言”三門的課程成績,按如下形式顯示:學生ID,管理學,經濟學,R語言,有效課程數,平均分*/select sno as 學生ID,
(select score from course,sc where t.sno=sc.sno and sc.cno=course.cno and cname='管理學') as 管理學,
(select score from course,sc where t.sno=sc.sno and sc.cno=course.cno and cname='經濟學') as 經濟學,
(select score from course,sc where t.sno=sc.sno and sc.cno=course.cno and cname='R語言') as R語言,
count(*) as 有效課程數,avg(t.score) as 平均成績
from sc t
groupby sno
orderbyavg(t.score);/*例18:查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分*/select cno as 課程ID,max(score) as 最高分,min(score) as 最低分
from sc
groupby cno;/*例19:按各科平均成績從低到高和及格率的百分數從高到低順序*/select sc.cno,cname,avg(score) as 平均數,(100 * SUM(CASEWHEN isnull(score,0)>=60THEN1ELSE0END)/COUNT(*)) as 及格百分數
from sc,course
where sc.cno=course.cno
groupby sc.cno,cname
orderby 平均數 ASC,及格百分數 DESC;/*例20:查詢如下課程平均成績和及格率的百分數(用"1行"顯示):管理學(c1),經濟學(c2),R語言(c3))*/selectsum(casewhen cno='c1'then score else0end)/sum(case cno when'c1'then1else0end) as 管理學平均分,
100 * SUM(CASEWHEN cno='c1'and isnull(score,0)>=60THEN1ELSE0END)/sum(casewhen cno='c1'then1else0end) as 管理學及格百分數,
sum(casewhen cno='c2'then score else0end)/sum(case cno when'c2'then1else0end) as 經濟學平均分,
100 * SUM(CASEWHEN cno='c2'and isnull(score,0)>=60THEN1ELSE0END)/sum(casewhen cno='c1'then1else0end) as 經濟學學及格百分數,
sum(casewhen cno='c3'then score else0end)/sum(case cno when'c3'then1else0end) as R語言平均分,
100 * SUM(CASEWHEN cno='c3'and isnull(score,0)>=60THEN1ELSE0END)/sum(casewhen cno='c1'then1else0end) as R語言及格百分數
from sc;/*例21:查詢不同老師所教不同課程平均分從高到低顯示*/select tno,sc.cno,avg(score)
from sc,course
where sc.cno=course.cno
groupby tno,sc.cno
orderbyavg(score) DESC;/*例22:查詢如下課程成績第名到第6 名的學生成績單:管理學(c1),經濟學(c2),R語言(c3)
[學生ID],[學生姓名],管理學,經濟學,R語言,平均成績*/--例23:統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60]select sc.cno as 課程ID,cname as 課程名稱,
sum(casewhen score between 85and100then1else0end)as [100-85],
sum(casewhen score between 70and85then1else0end)as [85-70],
sum(casewhen score between 60and0100then1else0end)as [70-60],
sum(casewhen score<60then1else0end)as [<60]
from sc,course
where sc.cno=course.cno
groupby sc.cno,cname;--例24:查詢學生平均成績及其名次SELECT1+(SELECTCOUNT( distinct 平均成績)
FROM (SELECT sno,AVG(score) AS 平均成績 FROM SC GROUPBY sno) AS T1
WHERE 平均成績 > T2.平均成績) as 名次, sno as 學生學號,平均成績
FROM (SELECT sno,AVG(score) 平均成績 FROM SC GROUPBY sno) AS T2
ORDERBY 平均成績 desc;--例25:查詢各科成績前三名的記錄:(不考慮成績并列情況)select cno,sno,score
from sc
where score in(select top 3 score from sc x where x.cno=sc.cno orderby score desc)
orderby cno;--例26:查詢每門課程被選修的學生數select cno,COUNT(*)as 人數
from sc
groupby cno;--例27:查詢出只選修了一門課程的全部學生的學號和姓名select sno,sname
from student
where sno in (
select sno
from sc
where sc.sno=student.sno
groupby sno
havingCOUNT(*)=1);--例28:查詢男生、女生人數select 男生人數,女生人數
from (selectCOUNT(*)as 女生人數 from student where ssex='女')as a,
(selectCOUNT(*)as 男生人數 from student where ssex='男') as b;Selectcount(Ssex) as 男生人數 from Student groupby Ssex having Ssex='男';Selectcount(Ssex) as 女生人數 from Student groupby Ssex having Ssex='女';--例29:查詢姓“張”的學生名單select sname
from student
where sname like'張%';--例30:查詢同名同性學生名單,并統計同名人數select sname,COUNT(*)
from student
groupby sname
havingCOUNT(*)>1
--例31:年出生的學生名單(注:Student表中Sage列的類型是datetime)
--修改表的類型
altertable student
altercolumn sage datetime;select Sname, CONVERT(char (11),DATEPART(year,Sage)) as age
from student
where CONVERT(char(11),DATEPART(YYYY,Sage))='1981';--例32:查詢每門課程的平均成績,結果按平均成績升序排列,若相同,則按課程號降序排列select cno,AVG(score)as 平均成績
from sc
groupby cno
orderbyAVG(score) asc,cno desc;--例33:查詢平均成績大于的所有學生的學號、姓名和平均成績select sc.sno,sname,AVG(score)
from student,sc
where student.sno=sc.sno
groupby sc.sno,sname
havingAVG(score)>80;--例34:查詢課程名稱為“管理學”,且分數低于的學生姓名和分數select sname,isnull(score,0)
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno and cname='管理學'and score<60;--例35:查詢所有學生的選課情況select sno,COUNT(*) as 選課數
from sc
groupby sno;SELECT SC.Sno,Sname,SC.cno,Cname
FROM SC,Student,Course
where SC.sno=Student.sno and SC.cno=Course.cno;--例36:查詢任何一門課程成績在分以上的姓名、課程名稱和分數selectdistinct(sname),cname,score
from student,course,sc
where student.sno=sc.sno and sc.cno=course.cno and score>70;--例37:查詢不及格的課程,并按課程號從大到小排列selectdistinct(cno)
from sc
where score<60orderby cno desc;--例38:查詢課程編號為c3且課程成績在分以上的學生的學號和姓名select student.sno,sname
from student,sc
where student.sno=sc.sno and cno='c3'and score>80;--例39:求選了課程的學生人數selectcount(distinct(sno))
from sc;--例40:查詢選修“葉平”老師所授課程的學生中,成績最高的學生姓名及其成績select sname,MAX(score)
from sc,student
where sc.sno=student.sno and cno in
(select cno from teacher,course where teacher.tno=course.tno and tname='李玉')
groupby sname;--另一種做法select Student.Sname,score
from Student,SC,Course C,Teacher
where sc.sno=student.sno and SC.cno=C.cno and C.tno=Teacher.tno and
Teacher.Tname='李玉'and SC.score=
(selectmax(score)from SC where Cno=C.cno);--例41:查詢各個課程及相應的選修人數select cno,COUNT(*)
from sc
groupby cno;--例42:查詢不同課程成績相同的學生的學號、課程號、學生成績selectdistinct A.sno,B.score
from SC A ,SC B
where A.Score=B.Score and A.cno <>B.cno;/*例43:查詢每門功成績最好的前兩名*/select sno as 學生ID,cno as 課程ID,score as 分數
from sc
where score in (select top 2 score from sc y where y.cno=sc.cno orderby score desc) ;/*例44:統計每門課程的學生選修人數。查詢結果按人數降序排列,若人數相同,按課程號升序排列*/select cno as 課程號,count(*) as 人數
from sc
groupby cno
orderbycount(*) desc,cno asc;/*例45:檢索至少選修兩門課程的學生學號*/select sno
from sc
groupby sno
havingcount(*)>=2;/*例46:查詢全部學生都選修的課程的課程號和課程名*//*同理:查詢選修了全部課程的學生姓名*/select cno,cname
from course
wherenotexists
(select * from student
wherenotexists
(select * from sc
where sc.sno=student.sno and sc.cno=course.cno));--下面一種方法更簡單,全部課程都在學生選的課程中select cno,cname
from course
where cno in (select cno from sc groupby cno);/*例47:查詢沒學過“李玉”老師講授的任一門課程的學生姓名*/select sname
from student
where sno notin( select sno
from sc,course,teacher
where sc.cno=course.cno and course.tno=teacher.tno and tname='李玉');/*例48:查詢兩門以上不及格課程的同學的學號及平均成績*/select sno,avg(isnull(score,0))
from sc
where sno in(select sno
from sc
where score<60groupby sno
havingcount(*)>=2)
groupby sno;/*例49:檢索“c2”課程分數小于*,按分數降序排列的同學學號*/select sno
from sc
where cno='c2'and score<60orderby score DESC;/*例50:刪除“”同學的“c1”課程的成績*/deletefrom sc
where sno='2'and cno='c1';