50條經典(學生,課程,成績,教師)表SQL語句~~值得一看!
/*student(學號#,姓名,性別,年齡)
course(課程號#,課程名,教師號#)
score(學號#,課程號#,成績)
teacher(教師號#,教師名)*/
--1.查詢“001”課程比“002”課程成績高的所有學生的學號
select a.stuNo from score a,score b
where a.cNo='c001' and b.cNo='c002' and a.stuNo=b.stuNo and a.score>b.score
--2.查詢平均成績大於60分的同學的學號和平均成績
select stuNo,avg(score)from score
group by stuNo
having avg(score)>60
--3.查詢所有同學的學號、姓名、選課數、總成績
select a.stuNo,a.stuName,count(cNo),sum(score) from student a,score b
where a.stuNo=b.stuNo
group by a.stuNo,a.stuName
--4.查詢姓“趙”的老師的個數
select count(tName),tName from teacher
where tName like '趙%'
group by tName
--5.查詢沒學過“某某”老師課的同學的學號、姓名
select stuNo,stuName from student
where stuNo not in
(select a.stuNo from student a,score b where a.stuNo=b.stuNo and cNo in
(select d.cNo from teacher c,course d where c.tNo=d.tNo and c.tName='錢市保'))
--6.查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名;
select a.stuNo,a.stuName from student a,score b,score c
where a.stuNo=b.stuNo and b.stuNo=c.stuNo and b.cNo='c001' and c.cNo='c002'
--7.查詢學過“某某”老師所教的所有課的同學的學號、姓名
select stuNo,stuName from student
where stuNo in (select stuNo from score a,course b,teacher c
where a.cNo=b.cNo and b.tNo=c.tNo and c.tName='錢市保'
group by stuNo
having count(a.cNo)>=(select count(cNo) from course d,teacher e
where d.tNo=e.tNo and e.tName='錢市保'))
--老師所教課程為一門課
select stuNo,stuName from student
where stuNo in (select a.stuNo from student a,score b where a.stuNo=b.stuNo and b.cNo in
(select cNo from teacher c,course d where c.tNo=d.tNo and c.tName='錢市保'))
--8.查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名
select stuNo,stuName from student
where stuNo in
(select a.stuNo from score a,score b
where a.cNo='c001' and b.cNo='c002' and a.stuNo=b.stuNo and a.score>b.score)
--9.查詢所有課程成績小於60分的同學的學號、姓名
select stuNo,stuName from student
where stuNo in (select stuNo from score
where score<60
group by stuNo
having count(cNo)=(select count(cNo) from course))
--10.查詢沒有學全所有課的同學的學號、姓名
select b.stuNo,a.stuName,count(b.cNo) from student a,score b
where a.stuNo=b.stuNo
group by b.stuNo,a.stuName
having count(b.cNo)<(select count(cNo) from course)
--11.查詢至少有一門課與學號為“1”的同學所學相同的同學的學號和姓名
select distinct a.stuNo,stuName from student a,score b
where a.stuNo=b.stuNo and cNo in (select cNo from score
where stuNo='001')
--12.查詢至少學過學號為“001”同學一門課的其他同學學號和姓名
&&& select distinct a.stuNo,stuName from student a,score b
where a.stuNo=b.stuNo and cNo all join (select cNo from score
where stuNo='001')
--13.把“SC”表中“某某”老師教的課的成績都更改為此課程的平均成績
update score set score=savg
from score d,(select avg(score) as savg,a.cNo from score a,course b,teacher c
where a.cNo=b.cNo and b.tNo=c.tNo and tName='錢市保'
group by a.cNo) e
where d.cNo=e.cNo
--老師所教課程為一門課
update score
set score=(select avg(score) from score
group by cNo
having cNo=(select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='錢市保'))
where cNo=(select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='錢市保')
select * from score
--14.查詢和“001”號的同學學習的課程完全相同的其他同學學號和姓名
select stuNo from score
where cNo in (select cNo from score where stuNo='005')
group by stuNo
having count(cNo)=(select count(*) from score where stuNo='005')
--15.刪除學習“某某”老師課的SC表記錄
delete from score where cNo=(select cNo from course a,teacher b where a.tNo=b.tNo and b.tName='錢市保')
select * from score
--16.向SC表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號“003”課程的同學學號、2號課的平均成績
--17.按平均成績從高到低顯示所有學生的“C語言”、“sql”、“java”三門的課程成績
--按如下形式顯示: 學生ID,C語言,sql,JAVA,有效課程數,有效平均分
--18.查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分
select cNo,max(score) as 最高分,min(score) as 最低分 from score
group by cNo
--19.按各科平均成績從低到高和及格率的百分數從高到低順序
select avg(c.score),count(a.score)/count(b.score) from score c,(select a.cNo,count(a.score) from score a
where a.score<60
group by a.cNo) d,(select b.cNo,count(b.score) from score b
group by b.cNo) e
where d.cNo=e.cNo
group by c.cNo
order by avg(c.score) desc
(select a.cNo,count(a.score) from score a
where a.score<60
group by a.cNo) d
(select b.cNo,count(b.score) from score b
group by b.cNo) e
--20.查詢如下課程平均成績和及格率的百分數(用"1行"顯示): C語言(001),資料結構(002),JAVA(003),離散數學(004)
--21.查詢不同老師所教不同課程平均分從高到低顯示
select tNo,a.cNo,avg(score) from course a,score b
where a.cNo=b.cNo
group by tNo,a.cNo
order by avg(score) desc
--22.查詢如下課程成績第 3 名到第 6 名的學生成績單:C語言(001),資料結構(002),JAVA(003),離散數學(004)
-- [學生ID],[學生姓名],C語言,資料結構,JAVA,離散數學,平均成績
--23.統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60]
select distinct e.cNo,count(a.stuNo) as '100-85',count(b.stuNo) as '85-70',count(c.stuNo) as '70-60',count(d.stuNo) as '<60' from score a,score b,score c,score d,score e
where a.cNo in (select cNo from course) and a.score between 85 and 100 and b.cNo in (select cNo from course) and b.score between 71 and 84 and c.cNo in (select cNo from course) and c.score between 60 and 70 and d.cNo in (select cNo from course) and d.score<60
group by e.cNo,a.stuNo,b.stuNo,c.stuNo,d.stuNo
having a.stuNo<>b.stuNo and a.stuNo<>c.stuNo and a.stuNo<>d.stuNo and b.stuNo<>c.stuNo and b.stuNo<>d.stuNo and c.stuNo<>d.stuNo
select cNo,count(stuNo) from score
where score between 70 and 100 and cNo='c001'
group by cNo
--24.查詢學生平均成績及其名次
select stuNo,avg(score) from score
group by stuNo
order by avg(score) desc
--25.查詢各科成績前三名的記錄:(不考慮成績並列情況)
select a.stuNo,a.cNo,a.score
from score a
where a.score in (select top 3 score from score b
where a.cNo=b.cNo
order by score)
order by a.cNo
--26.查詢每門課程被選修的學生數
select b.cNo ,count(stuNo) from score a right join course b
on a.cNo=b.cNo
group by b.cNo
--27.查詢出只選修了一門課程的全部學生的學號和姓名
select b.stuNo,a.stuName from student a,score b
where a.stuNo=b.stuNo
group by b.stuNo,a.stuName
having count(b.cNo)=1
--28.查詢男生、女生人數
select stuSex,count(stuSex) from student
group by stuSex
--29.查詢姓‘zhao’的學生名單
select * from student
where stuName like '趙%'
--30.查詢同名同性學生名單,並統計同名人數
select a.stuNo,a.stuName,count(a.stuNo) from student a,student b
where a.stuName=b.stuName and a.stuSex=b.stuSex and a.stuNo<>b.stuNo
group by a.stuNo,a.stuName
--31.1981年出生的學生名單(注:Student表中Sage列的型別是datetime
--32.、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列
select cNo,avg(score) from score
group by cNo
order by avg(score) asc,cNo
--33.查詢平均成績大於70的所有學生的學號、姓名和平均成績
select b.stuNo,a.stuName,avg(score) from student a,score b
where a.stuNo=b.stuNo
group by b.stuNo,a.stuName
having avg(score)>70
--34.查詢課程名稱為“java”,且分數低於70的學生姓名和分數
select a.stuName,b.score from student a,score b
where a.stuNo=b.stuNo and score<70 and b.cNo=(select cNo from course where cName='java')
--35.查詢所有學生的選課情況
select a.stuNo,c.cNo from student a,score b,course c
where a.stuNo=b.stuNo and b.cNo=c.cNo
order by a.stuNo
select a.stuNo,cNo from student a left join (select a.stuNo,c.cNo from student a,score b,course c
where a.stuNo=b.stuNo and b.cNo=c.cNo) d
on a.stuNo=d.stuNo
order by a.stuNo
--36.查詢任何一門課程成績在70分以上的姓名、課程名稱和分數
select a.stuName,b.cNo,score from student a,score b
where score>70 and a.stuNo=b.stuNo
--37.查詢不及格的課程,並按課程號從大到小排列
select cNo,score from score
where score<60
order by cNo
--38.查詢課程編號為003且課程成績在60分以上的學生的學號和姓名
select b.stuNo,a.stuName from student a,score b
where b.cNo='c003' and score>60 and a.stuNo=b.stuNo
--39.求選了課程的學生人數
select count(a.stuNo) from (select distinct stuNo from score) a
--40.查詢選修“趙”老師所授課程的學生中,成績最高的學生姓名及其成績
select b.stuNo,a.stuName,max(score) from student a,score b
where a.stuNo=b.stuNo and b.cNo in (select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='錢市保')
group by b.stuNo,a.stuName,b.cNo
having b.cNo in (select a.cNo from course a,teacher b where a.tNo=b.tNo and b.tName='錢市保')
--41.查詢各個課程及相應的選修人數
select cNo,count(stuNo) from score
group by cNo
select b.cNo ,count(stuNo) from score a right join course b
on a.cNo=b.cNo
group by b.cNo
--42.查詢不同課程成績相同的學生的學號、課程號、學生成績
select a.stuNo,a.cNo,a.score from score a,score b
where a.stuNo=b.stuNo and a.score=b.score and a.cNo<>b.cNo
--43. 查詢每門功成績最好的前兩名
select a.stuNo,a.cNo,a.score
from score a
where score in(select top 2 score from score b
where a.cNo=b.cNo
order by score desc)
order by a.cNo
--44.統計每門課程的學生選修人數(超過2人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列
select cNo,count(stuNo) 課程數 from score
group by cNo
having count(stuNo)>2
order by count(stuNo) desc,cNo
--45.檢索至少選修兩門課程的學生學號
select stuNo from score
group by stuNo
having count(cNo)>=2
--46.查詢全部學生都選修的課程的課程號和課程名
select a.cNo,b.cName from score a,course b
where a.cNo=b.cNo
group by a.cNo,b.cName
having count(a.stuNo)=(select count(stuNo) from student)
select a.cNo,b.cName from score a,course b
group by a.cNo,b.cName,b.cNo
having a.cNo=b.cNo and count(a.stuNo)=(select count(stuNo) from student)
--47.查詢沒學過“錢”老師講授的任一門課程的學生姓名
select stuNo,stuName from student
where stuNo not in (select stuNo from score a,course b,teacher c
where a.cNo=b.cNo and b.tNo=c.tNo and c.tName='錢市保'
group by stuNo
having count(a.cNo)<=(select count(cNo) from course d,teacher e
where d.tNo=e.tNo and e.tName='錢市保'))
select stuNo,stuName from student
where stuNo not in
(select stuNo from score where cNo in
(select cNo from teacher c,course d where c.tNo=d.tNo and c.tName='錢市保'))
--48.查詢兩門以上不及格課程的同學的學號及其平均成績
select stuNo,avg(score) from score
where score<60
group by stuNo
having count(cNo)>2
--49.檢索“004”課程分數小於60,按分數降序排列的同學學號
select stuNo from score
where score<60 and cNo='c004'
order by score desc
--50.刪除“2”同學的“001”課程的成績
delete from score where stuNo='002' and cNo='c001'