SQL练习题2

接上第一篇SQL之练习题,详细数据表见上篇文章。

11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

12.检索" 01 "课程分数小于 60,按分数降序排列的学生信息

13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

14.查询各科成绩最高分、最低分和平均分:

以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。其中及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

15.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺

16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺

16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比

18.查询各科成绩前三名的记录

19.查询每门课程被选修的学生数

20.出只选修两门课程的学生学号和姓名

-------------------------------以上为此次练习题,下面正式开始----------------------------------------

11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select student.SId,student.Sname,avg(sc.score) from student,sc
where
     student.SId = sc.SId and sc.score < 60
group by sc.SId
having count(*) > 1

12.检索" 01 "课程分数小于 60,按分数降序排列的学生信息

select student.*,score1 from student right join
(select sc.SId,sc.score as score1 from sc 
where CId = '01' and sc.score < 60) as t1
on student.SId = t1.SId
order by score1 

13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select * from sc
left join
(select SId,avg(sc.score) as avgscore from sc
 group by SId)as t1
 on t1.SId = sc.SId
order by avgscore desc

14.查询各科成绩最高分、最低分和平均分:

以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率。其中及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select 
sc.CId ,
max(sc.score)as 最高分,
min(sc.score)as 最低分,
AVG(sc.score)as 平均分,
count(*)as 选修人数,
sum(case when sc.score>=60 then 1 else 0 end )/count(*)as 及格率,
sum(case when sc.score>=70 and sc.score<80 then 1 else 0 end )/count(*)as 中等率,
sum(case when sc.score>=80 and sc.score<90 then 1 else 0 end )/count(*)as 优良率,
sum(case when sc.score>=90 then 1 else 0 end )/count(*)as 优秀率 
from sc
group by sc.CId
group by count(*) desc,sc.CId Asc

15.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺

select a.cid, a.sid, a.score, count(b.score)+1 as rank
from sc as a 
left join sc as b 
on a.score<b.score and a.cid = b.cid
group by a.cid, a.sid,a.score
order by a.cid, rank ASC

16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺

set @crank=0;
select q.sid, total, @crank := @crank +1 as rank from(
select sc.sid, sum(sc.score) as total from sc
group by sc.sid
order by total desc)q

17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比

select course.cname, course.cid,
sum(case when sc.score<=100 and sc.score>85 then 1 else 0 end) as "[100-85]",
sum(case when sc.score<=85 and sc.score>70 then 1 else 0 end) as "[85-70]",
sum(case when sc.score<=70 and sc.score>60 then 1 else 0 end) as "[70-60]",
sum(case when sc.score<=60 and sc.score>0 then 1 else 0 end) as "[60-0]"
from sc left join course
on sc.cid = course.cid
group by sc.cid

18.查询各科成绩前三名的记录

select * from sc
where (
select count(*) from sc as a 
where sc.cid = a.cid and sc.score<a.score 
)< 3
order by cid asc, sc.score desc

19.查询每门课程被选修的学生数

select cid, count(sid) from sc 
group by cid

20.出只选修两门课程的学生学号和姓名

select student.sid, student.sname from student
where student.sid in
(select sc.sid from sc
group by sc.sid
having count(sc.cid)=2
)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容