SQL练习题2

作者: 只有三挡的风扇 | 来源:发表于2019-01-20 12:07 被阅读13次

接上第一篇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
)

相关文章

  • SQL练习题(2)

    SQL练习 题目均取自sqlzoo, 在此只写下自己的答案。 4. SELECT within SELECT ...

  • SQL练习题-2

    员工表结构: 部门表结构: 部门员工表结构: 部门经理表结构: 薪资表结构: 1.获取所有员工当前的manager...

  • SQL练习题2

    接上第一篇SQL之练习题,详细数据表见上篇文章。 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 ...

  • Mysql-Sql练习

    Sql练习题 1.表和数据的准备 员工表 部门表 工资等级表 2.练习题目 查询出部门编号为30的所有员工的编号和...

  • 视图与子查询

    《SQL基础教程第2版》Chap.5练习题 5-2 向视图 ViewPractice5_1 插入数据. ❓为什么S...

  • MySQL 练习题

    01第一天20180703 【SQL练习】经典SQL练习题 02第二天20180704 sql语句练习50题(My...

  • bWAPP学习笔记 - A1 Injection (二)

    SQL Injection (GET/Search) 手注练习题 (^_^) Level: Low 先输入单引号'...

  • 2018-12-01

    练习题1 练习题2 练习题3

  • LeetCode-SQL-nine

    Leetcode-sql-nine 本文中主要是介绍LeetCode中关于SQL的练习题,从易到难,循序渐进。文中...

  • LeetCode-SQL-five

    LeetCode-SQL-five 本文中主要是介绍LeetCode中关于SQL的练习题,从易到难,循序渐进。文中...

网友评论

    本文标题:SQL练习题2

    本文链接:https://www.haomeiwen.com/subject/ddhxjqtx.html