超经典SQL练习,当你全部弄透,你的SQL已小成( 四 )


convert(decimal(5,2),(sum(case when score>60 and score<=70 then 1 else null end))*1.00/COUNT(*))*100 所占百分比 from SC group by C#)E on A.C#=E.C#
left join (select C#,sum(case when score>0 and score<=60 then 1 else null end)[60-0],
convert(decimal(5,2),(sum(case when score>0 and score<=60 then 1 else null end))*1.00/COUNT(*))*100 所占百分比 from SC group by C#)F on A.C#=F.C#
--17. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select * from(select *,rank()over (partition by C# order by score desc)A from SC)B where B.A<=3
--18. 查询各科成绩前三名的记录(方法 1)
select a.S#,a.C#,a.score from SC a
left join SC b on a.C#=b.C# and a.score<b.score
group by a.S#,a.C#,a.score
having COUNT(b.S#)<3
order by a.C#,a.score desc
--18. 查询各科成绩前三名的记录(取 a 的最高分与本表比较)(方法 2)
select * from SC a where (select COUNT(*)from SC where C#=a.C# and score>a.score)<3
order by a.C#,a.score desc
--18. 查询各科成绩前三名的记录(取 a)(方法 3)
select C#,COUNT(S#)学生数 from SC group by C#
--19. 查询每门课程被选修的学生数
select S#,Sname from Student
where S# in(select S# from(select S#,COUNT(C#)课程数 from SC group by S#)A where A.课程数=2)
--20. 查询出只选修两门课程的学生学号和姓名
select Ssex,COUNT(Ssex)人数 from Student group by Ssex
--21. 查询男生、女生人数
select * from Student where Sname like '%风%'
--22. 查询名字中含有「风」字的学生信息
select A.*,B.同名人数 from Student A
left join (select Sname,Ssex,COUNT(*)同名人数 from Student group by Sname,Ssex)B
on A.Sname=B.Sname and A.Ssex=B.Ssex
where B.同名人数>1
--23. 查询同名同性学生名单,并统计同名人数
select * from Student where YEAR(Sage)=1990
--24.查询 1990 年出生的学生名单
select C#,AVG(score)平均成绩 from SC group by C# order by 平均成绩 desc,C#
--25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select A.S#,A.Sname,B.平均成绩 from Student A
left join (select S#,AVG(score)平均成绩 from SC group by S#)B on A.S#=B.S#
where B.平均成绩>85
--26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select B.Sname,A.score from(select * from SC where score<60 and C#=(select C# from Course where Cname='数学'))A
left join Student B on A.S#=B.S#
-- 27. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
select A.S#,B.C#,B.score from Student A left join SC B on A.S#=B.S#
-- 28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
select A.Sname,D.Cname,D.score from
(select B.*,C.Cname from(select * from SC where score>70)B left join Course C on B.C#=C.C#)D
left join Student A on D.S#=A.S#
-- 29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select * from SC where score<60
-- 30. 查询不及格的课程
select A.S#,B.Sname from (select * from SC where score>80 and C#=01)A
left join Student B on A.S#=B.S#
--31. 查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
select C#,COUNT(*)学生人数 from SC group by C#
--32. 求每门课程的学生人数
select top 1* from SC
where C#=(select C# from Course where T#=(select T# from Teacher where Tname='张三'))
order by score desc
--33. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select *from(select *,DENSE_RANK()over (order by score desc)A
from SC
where C#=(select C# from Course where T#=(select T# from Teacher where Tname='张三')))B
where B.A=1
--34. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select C.S#,max(C.C#)C#,max(C.score)score from SC C
left join(select S#,avg(score)A from SC group by S#)B
on C.S#=B.S#
where C.score=B.A
group by C.S#
having COUNT(0)=(select COUNT(0)from SC where S#=C.S#)
--35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select * from
(select *,ROW_NUMBER()over(partition by C# order by score desc)A from SC)B
where B.A<3
--36. 查询每门功成绩最好的前两名
select C#,COUNT(S#)选修人数 from SC
group by C#
having COUNT(S#)>5
order by 选修人数 desc,C#
--37.统计每门课程的学生选修人数(超过5人的课程才统计) 。


推荐阅读