MySQL经典50题,全部都会做就是大佬了!
创建表结构
建表语句
-- 学⽣表 Student
create table Student(
SId varchar(10),
Sname varchar(10),
Sage datetime,
Ssex varchar(10)
);
-- 科⽬表 Course
create table Course(
CId varchar(10),
Cname nvarchar(10),
TId varchar(10)
);
-- 教师表 Teacher
create table Teacher(
TId varchar(10),
Tname varchar(10)
);
-- 成绩表 SC
create table SC(
SId varchar(10),
CId varchar(10),
score decimal(18,1)
);
添加数据
-- 学⽣表 Student
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙⻛' , '1990-12-20' , '男');
insert into Student values('04' , '李云' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '⼥');
insert into Student values('06' , '吴兰' , '1992-01-01' , '⼥');
insert into Student values('07' , '郑⽵' , '1989-01-01' , '⼥');
insert into Student values('09' , '张三' , '2017-12-20' , '⼥');
insert into Student values('10' , '李四' , '2017-12-25' , '⼥');
insert into Student values('11' , '李四' , '2012-06-06' , '⼥');
insert into Student values('12' , '赵六' , '2013-06-13' , '⼥');
insert into Student values('13' , '孙七' , '2014-06-01' , '⼥');
-- 科⽬表 Course
insert into Course values('01' , '语⽂' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 教师表
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 成绩表
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
表关系
稍微写几道题就熟练了!
题目
1.查询" 01 “课程⽐” 02 "课程成绩⾼的学⽣的信息及课程分数
2.查询同时存在" 01 “课程和” 02 "课程的情况
3.查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )
4.查询不存在" 01 “课程但存在” 02 "课程的情况
5.查询平均成绩⼤于等于 60 分的同学的学⽣编号和学⽣姓名和平均成绩
6.查询在 SC 表存在成绩的学⽣信息
7.查询所有同学的学⽣编号、学⽣姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
8.查询「李」姓⽼师的数量
9.查询学过「张三」⽼师授课的同学的信息
10.查询没有学全所有课程的同学的信息
11.查询⾄少有⼀⻔课与学号为" 01 "的同学所学相同的同学的信息
12.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
13.查询没学过"张三"⽼师讲授的任⼀⻔课程的学⽣姓名
14.查询两⻔及其以上不及格课程的同学的学号,姓名及其平均成绩
15.检索" 01 "课程分数⼩于 60,按分数降序排列的学⽣信息
16.按平均成绩从⾼到低显示所有学⽣的所有课的成绩以及平均成绩
17.查询各科成绩最⾼分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最⾼分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修⼈数,查询结果按⼈数降序排列,若⼈数相同,按课程号升序排列
18.按各科平均成绩进⾏排序,并显示排名, Score 重复时保留名次空缺
19.按各科平均成绩进⾏排序,并显示排名, Score 重复时不保留名次空缺
20.查询学⽣的总成绩,并进⾏排名,总分重复时保留名次空缺
21.查询学⽣的总成绩,并进⾏排名,总分重复时不保留名次空缺
22.统计各科成绩各分数段⼈数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0]及所占百分⽐
23.查询各科成绩前三名的记录
24.查询每⻔课程被选修的学⽣数
25.查询出只选修两⻔课程的学⽣学号和姓名
26.查询男⽣、⼥⽣⼈数27.查询名字中含有「⻛」字的学⽣信息
28.查询同名同性学⽣名单,并统计同名⼈数
29.查询 1990 年出⽣的学⽣名单
30.查询每⻔课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
31.查询平均成绩⼤于等于 85 的所有学⽣的学号、姓名和平均成绩
32.查询课程名称为「数学」,且分数低于 60 的学⽣姓名和分数
33.查询所有学⽣的课程及分数情况(存在学⽣没成绩,没选课的情况)
34.查询任何⼀⻔课程成绩在 70 分以上的姓名、课程名称和分数
35.查询不及格的课程
36.查询课程编号为 01 且课程成绩在 80 分以上的学⽣的学号和姓名
37.求每⻔课程的学⽣⼈数
38.成绩不重复,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信及其成绩
39.成绩有重复的情况下,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信息及其成绩
40.查询不同课程成绩相同的学⽣的学⽣编号、课程编号、学⽣成绩
41.查询每⻔课程成绩最好的前两名
42.统计每⻔课程的学⽣选修⼈数(超过 5 ⼈的课程才统计)。
43.检索⾄少选修两⻔课程的学⽣学号
44.查询选修了全部课程的学⽣信息
45.查询各学⽣的年龄,只按年份来算
46.按照出⽣⽇期来算,当前⽉⽇ < 出⽣年⽉的⽉⽇则,年龄减⼀TIMESTAMPDIFF() 从⽇期时间表达式中减去间隔
47.查询本周过⽣⽇的学⽣
返回⽇期从范围内的数字⽇历星期1到5348.查询下周过⽣⽇的学⽣
49.查询本⽉过⽣⽇的学⽣
50.查询下⽉过⽣⽇的学⽣
参考答案
1.查询" 01 “课程⽐” 02 "课程成绩⾼的学⽣的信息及课程分数
分析:我们这里使用到了学生信息,课程分数,所以我们需要关联三张表。
先分别查询01和02课程的学员的id和分数
select sid,score from sc where cid='01';
select sid,score from sc where cid='02';
+------+-------+
| sid | score |
+------+-------+
| 01 | 80.0 |
| 02 | 70.0 |
| 03 | 80.0 |
| 04 | 50.0 |
| 05 | 76.0 |
| 06 | 31.0 |
+------+-------+
6 rows in set (0.00 sec)
+------+-------+
| sid | score |
+------+-------+
| 01 | 90.0 |
| 02 | 60.0 |
| 03 | 80.0 |
| 04 | 30.0 |
| 05 | 87.0 |
| 07 | 89.0 |
+------+-------+
6 rows in set (0.00 sec)
对比结果发现,两个结果中有些sid是不对应的
因此可以对两个结果做join联结,同时sid作为关联条件
并且01乘积大于02
select s1.sid,s1.score from
(select sid,score from sc where cid='01') as s1
join
(select sid,score from sc where cid='02') as s2
on s1.sid=s2.sid
where s1.score > s2.score;
结果
+------+-------+
| sid | score |
+------+-------+
| 02 | 70.0 |
| 04 | 50.0 |
+------+-------+
题目要求是学生信息,而不是学生id,所以进一步查询
select stu.* ,s.score
from student as stu
right join
(
select s1.sid,s1.score from
(select sid,score from sc where cid='01') as s1
join
(select sid,score from sc where cid='02') as s2
on s1.sid=s2.sid
where s1.score > s2.score
) as s
on stu.sid = s.sid;
结果
+------+--------+---------------------+------+-------+
| SId | Sname | Sage | Ssex | score |
+------+--------+---------------------+------+-------+
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 70.0 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 50.0 |
+------+--------+---------------------+------+-------+
有点复杂我们简化一下
select student.*, s1.score, s2.score
from student
join sc s1 on s1.sid=student.sid and s1.cid='01'
join sc s2 on s2.sid=student.sid and s2.cid='02'
where s1.score>s2.score
结果一致
+------+--------+---------------------+------+-------+
| SId | Sname | Sage | Ssex | score |
+------+--------+---------------------+------+-------+
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 70.0 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 50.0 |
+------+--------+---------------------+------+-------+
2.查询同时存在" 01 “课程和” 02 "课程的情况
select t1.SId ,t1.score '01' , t2.score '02'
from (select * from sc WHERE sc.CId='01')as t1
inner join (select * from sc WHERE sc.CId='02')as t2 ON t1.SId=t2.SId
+------+------+------+
| SId | 01 | 02 |
+------+------+------+
| 01 | 80.0 | 90.0 |
| 02 | 70.0 | 60.0 |
| 03 | 80.0 | 80.0 |
| 04 | 50.0 | 30.0 |
| 05 | 76.0 | 87.0 |
+------+------+------+
3.查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )
select t1.SId ,t1.score '01' , t2.score '02'
from (select * from sc WHERE sc.CId='01')as t1
LEFT JOIN (select * from sc WHERE sc.CId='02')as t2 ON t1.SId=t2.SId
+------+------+------+
| SId | 01 | 02 |
+------+------+------+
| 01 | 80.0 | 90.0 |
| 02 | 70.0 | 60.0 |
| 03 | 80.0 | 80.0 |
| 04 | 50.0 | 30.0 |
| 05 | 76.0 | 87.0 |
| 06 | 31.0 | NULL |
+------+------+------+
4.查询不存在" 01 “课程但存在” 02 "课程的情况
select t1.SId ,t2.score '01' , t1.score '02'
from (select * from sc WHERE sc.CId='02')as t1
LEFT JOIN (select * from sc WHERE sc.CId='01')as t2 ON t1.SId=t2.SId
+------+------+------+
| SId | 01 | 02 |
+------+------+------+
| 01 | 80.0 | 90.0 |
| 02 | 70.0 | 60.0 |
| 03 | 80.0 | 80.0 |
| 04 | 50.0 | 30.0 |
| 05 | 76.0 | 87.0 |
| 07 | NULL | 89.0 |
+------+------+------+
5.查询平均成绩⼤于等于 60 分的同学的学⽣编号和学⽣姓名和平均成绩
select sc.sid,sname,round(avg(score),2) as avg_score
from sc,student
where sc.sid = student.sid
group by sc.sid,sname
having avg_score>60
+------+--------+-----------+
| sid | sname | avg_score |
+------+--------+-----------+
| 01 | 赵雷 | 89.67 |
| 02 | 钱电 | 70.00 |
| 03 | 孙⻛ | 80.00 |
| 05 | 周梅 | 81.50 |
| 07 | 郑⽵ | 93.50 |
+------+--------+-----------+
6.查询在 SC 表存在成绩的学⽣信息
select distinct stu.*
from student as stu
join sc on sc.sid=stu.sid;
7.查询所有同学的学⽣编号、学⽣姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select stu.sid,stu.sname,count(sc.cid) as num,sum(sc.score) as total_score
from student as stu
left join sc on sc.sid=stu.sid
group by stu.sid,stu.sname;
+------+--------+-----+-------------+
| sid | sname | num | total_score |
+------+--------+-----+-------------+
| 01 | 赵雷 | 3 | 269.0 |
| 02 | 钱电 | 3 | 210.0 |
| 03 | 孙⻛ | 3 | 240.0 |
| 04 | 李云 | 3 | 100.0 |
| 05 | 周梅 | 2 | 163.0 |
| 06 | 吴兰 | 2 | 65.0 |
| 07 | 郑⽵ | 2 | 187.0 |
| 09 | 张三 | 0 | NULL |
| 10 | 李四 | 0 | NULL |
| 11 | 李四 | 0 | NULL |
| 12 | 赵六 | 0 | NULL |
| 13 | 孙七 | 0 | NULL |
+------+--------+-----+-------------+
8.查询「李」姓⽼师的数量
select count(*) as '姓李的老师的数量'
from teacher
where tname like '李%'
+--------------------------+
| 姓李的老师的数量 |
+--------------------------+
| 1 |
+--------------------------+
9.查询学过「张三」⽼师授课的同学的信息
select student.* , teacher.tname
from student
join sc on student.sid=sc.sid
join course on course.cid=sc.cid
join teacher on teacher.tid=course.tid
where teacher.tname='张三'
+------+--------+---------------------+------+--------+
| SId | Sname | Sage | Ssex | tname |
+------+--------+---------------------+------+--------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 张三 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 张三 |
| 03 | 孙⻛ | 1990-12-20 00:00:00 | 男 | 张三 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 张三 |
| 05 | 周梅 | 1991-12-01 00:00:00 | ⼥ | 张三 |
| 07 | 郑⽵ | 1989-01-01 00:00:00 | ⼥ | 张三 |
+------+--------+---------------------+------+--------+
10.查询没有学全所有课程的同学的信息
select stu.* ,count(sc.cid) as counts
from student stu
join sc on sc.sid=stu.sid
group by stu.sid,stu.sname,stu.Ssex,stu.Sage
having counts
+------+--------+---------------------+------+--------+
| SId | Sname | Sage | Ssex | counts |
+------+--------+---------------------+------+--------+
| 05 | 周梅 | 1991-12-01 00:00:00 | ⼥ | 2 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | ⼥ | 2 |
| 07 | 郑⽵ | 1989-01-01 00:00:00 | ⼥ | 2 |
+------+--------+---------------------+------+--------+
11.查询⾄少有⼀⻔课与学号为" 01 "的同学所学相同的同学的信息
思路:先查询01同学选过的课,然后判断是否 in 这查询结果中,我们会得到id重复的信息,这里还需要去重 distinct
select distinct student.*
from student
join sc on sc.sid=student.sid
where sc.cid in (select sc.cid from sc where sc.sid='01') and sc.sid<>'01'
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙⻛ | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | ⼥ |
| 06 | 吴兰 | 1992-01-01 00:00:00 | ⼥ |
| 07 | 郑⽵ | 1989-01-01 00:00:00 | ⼥ |
+------+--------+---------------------+------+
12.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
思路:先查询01同学选过的课,然后查询同学的课程等于01同学选过课程的匹配数量,在比较与01同学的课程数是否一致
关键点是两个成绩表分别取别名,一个表查是01同学的另一个表查不是01同学的,还有 group by 匹配到的课程数
select s2.sid,student.sname,student.Sage,student.Ssex
from sc s1
join sc s2
on s1.cid =s2.cid and s1.sid='01' and s2.sid!='01'
join student on student.sid = s2.sid
group by s2.sid,student.sname,student.Sage,student.Ssex
having count(s2.sid) = (select count(*) from sc where sid='01');
+------+--------+---------------------+------+
| sid | sname | Sage | Ssex |
+------+--------+---------------------+------+
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙⻛ | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
+------+--------+---------------------+------+
13.查询没学过"张三"⽼师讲授的任⼀⻔课程的学⽣姓名
这是之前的查询学习过张三老师课程的SQL,这里我们取反
select student.* , teacher.tname
from student
join sc on student.sid=sc.sid
join course on course.cid=sc.cid
join teacher on teacher.tid=course.tid
where teacher.tname='张三'
+------+--------+---------------------+------+--------+
| SId | Sname | Sage | Ssex | tname |
+------+--------+---------------------+------+--------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 张三 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 张三 |
| 03 | 孙⻛ | 1990-12-20 00:00:00 | 男 | 张三 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 张三 |
| 05 | 周梅 | 1991-12-01 00:00:00 | ⼥ | 张三 |
| 07 | 郑⽵ | 1989-01-01 00:00:00 | ⼥ | 张三 |
+------+--------+---------------------+------+--------+
查询不在这个列表即可
select student.*
from student
where student.sid not in(
select student.sid
from student
join sc on student.sid=sc.sid
join course on course.cid=sc.cid
join teacher on teacher.tid=course.tid
where teacher.tname='张三'
)
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 06 | 吴兰 | 1992-01-01 00:00:00 | ⼥ |
| 09 | 张三 | 2017-12-20 00:00:00 | ⼥ |
| 10 | 李四 | 2017-12-25 00:00:00 | ⼥ |
| 11 | 李四 | 2012-06-06 00:00:00 | ⼥ |
| 12 | 赵六 | 2013-06-13 00:00:00 | ⼥ |
| 13 | 孙七 | 2014-06-01 00:00:00 | ⼥ |
+------+--------+---------------------+------+
14.查询两⻔及其以上不及格课程的同学的学号,姓名及其平均成绩
先统计不及格的分数,再进行分组,再计算不及格趁机的数量是否大于等于2
select stu.sid,stu.sname,round(avg(sc.score),2) as avg_score
from student as stu
join sc on stu.sid=sc.sid
where sc.score<60
group by stu.sid,stu.sname
having count(sc.cid)>=2;
+------+--------+-----------+
| sid | sname | avg_score |
+------+--------+-----------+
| 04 | 李云 | 33.33 |
| 06 | 吴兰 | 32.50 |
+------+--------+-----------+
15.检索" 01 "课程分数⼩于 60,按分数降序排列的学⽣信息
select student.sname,student.Sage,student.Ssex,sc.score
from student
join sc on student.sid=sc.sid and sc.cid='01' and sc.score<60
group by sc.score,student.sname,student.Sage,student.Ssex desc
+--------+---------------------+------+-------+
| sname | Sage | Ssex | score |
+--------+---------------------+------+-------+
| 吴兰 | 1992-01-01 00:00:00 | ⼥ | 31.0 |
| 李云 | 1990-12-06 00:00:00 | 男 | 50.0 |
+--------+---------------------+------+-------+
16.按平均成绩从⾼到低显示所有学⽣的所有课程的成绩以及平均成绩
思路:先查询所有学生的平均成绩,再和sc表管理,显示所有课程的成绩
select sid,avg(score) avg_score
from sc
group by sid
+------+-----------+
| sid | avg_score |
+------+-----------+
| 01 | 89.66667 |
| 02 | 70.00000 |
| 03 | 80.00000 |
| 04 | 33.33333 |
| 05 | 81.50000 |
| 06 | 32.50000 |
| 07 | 93.50000 |
+------+-----------+
关联
select sc.*,s2.avg_score
from sc
join (
select sid,avg(score) avg_score
from sc
group by sid
)as s2
on sc.sid=s2.sid
order by s2.avg_score desc,sc.sid;
+------+------+-------+-----------+
| SId | CId | score | avg_score |
+------+------+-------+-----------+
| 07 | 02 | 89.0 | 93.50000 |
| 07 | 03 | 98.0 | 93.50000 |
| 01 | 02 | 90.0 | 89.66667 |
| 01 | 03 | 99.0 | 89.66667 |
| 01 | 01 | 80.0 | 89.66667 |
| 05 | 01 | 76.0 | 81.50000 |
| 05 | 02 | 87.0 | 81.50000 |
| 03 | 01 | 80.0 | 80.00000 |
| 03 | 02 | 80.0 | 80.00000 |
| 03 | 03 | 80.0 | 80.00000 |
| 02 | 02 | 60.0 | 70.00000 |
| 02 | 03 | 80.0 | 70.00000 |
| 02 | 01 | 70.0 | 70.00000 |
| 04 | 01 | 50.0 | 33.33333 |
| 04 | 02 | 30.0 | 33.33333 |
| 04 | 03 | 20.0 | 33.33333 |
| 06 | 03 | 34.0 | 32.50000 |
| 06 | 01 | 31.0 | 32.50000 |
+------+------+-------+-----------+
我们看到每一次都打印了平均成绩,这里不能去重了,去重我们就看不到所有的课程成绩了
思路:分别取三个别名,把三科的成绩都关联起来
select stu.sid,stu.sname ,a.score,b.score,c.score,round(avg(d.score),2) as avg_score
from student as stu
left join sc as a on stu.sid=a.sid and a.cid='01'
left join sc as b on stu.sid=b.sid and b.cid='02'
left join sc as c on stu.sid=c.sid and c.cid='03'
left join sc as d on stu.sid=d.sid
group by stu.sid,stu.sname,a.score,b.score,c.score
order by avg_score desc
+------+--------+-------+-------+-------+-----------+
| sid | sname | score | score | score | avg_score |
+------+--------+-------+-------+-------+-----------+
| 07 | 郑⽵ | NULL | 89.0 | 98.0 | 93.50 |
| 01 | 赵雷 | 80.0 | 90.0 | 99.0 | 89.67 |
| 05 | 周梅 | 76.0 | 87.0 | NULL | 81.50 |
| 03 | 孙⻛ | 80.0 | 80.0 | 80.0 | 80.00 |
| 02 | 钱电 | 70.0 | 60.0 | 80.0 | 70.00 |
| 04 | 李云 | 50.0 | 30.0 | 20.0 | 33.33 |
| 06 | 吴兰 | 31.0 | NULL | 34.0 | 32.50 |
| 09 | 张三 | NULL | NULL | NULL | NULL |
| 11 | 李四 | NULL | NULL | NULL | NULL |
| 13 | 孙七 | NULL | NULL | NULL | NULL |
| 10 | 李四 | NULL | NULL | NULL | NULL |
| 12 | 赵六 | NULL | NULL | NULL | NULL |
+------+--------+-------+-------+-------+-----------+
用了4个 left join 但是结果比上一种更加的好看一点
17.查询各科成绩最⾼分、最低分和平均分。以如下形式显示:
#课程 ID,课程 name,最⾼分,最低分,平均分,及格率,中等率,
#优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
#要求输出课程号和选修⼈数,查询结果按⼈数降序排列,若⼈数相同,按课程号升序排列
如果单独求最高分最低分,平均值,那这道题非常简单了。
select max(score),min(score),avg(score)
from sc
+------------+------------+------------+
| max(score) | min(score) | avg(score) |
+------------+------------+------------+
| 99.0 | 20.0 | 68.55556 |
+------------+------------+------------+
但是我发现题目好像没有显示完,往后拉,先一步一步来。
select sc.cid,c.cname,
max(sc.score) as '最高分',
min(sc.score) as '最低分',
avg(sc.score) as '平均分',
count(sc.cid) as '选课人数'
from sc
join course as c on sc.cid=c.cid
group by sc.cid,c.cname
order by '选修人数' desc,sc.cid;
+------+--------+-----------+-----------+-----------+--------------+
| cid | cname | 最高分 | 最低分 | 平均分 | 选课人数 |
+------+--------+-----------+-----------+-----------+--------------+
| 01 | 语⽂ | 80.0 | 31.0 | 64.50000 | 6 |
| 02 | 数学 | 90.0 | 30.0 | 72.66667 | 6 |
| 03 | 英语 | 99.0 | 20.0 | 68.50000 | 6 |
+------+--------+-----------+-----------+-----------+--------------+
对于及格率,优秀率的计算,这里需要使用我们的新语法 case then '条件' then '满足条件的结果' else '不满足条件的结果' end
case when sc.score>=60 then 1 else 0 end
相当于java中的
if(sc.score>=60){
return 1;
}
else{
reuturn 0;
}
select sc.cid,c.cname,
max(sc.score) as '最高分',
min(sc.score) as '最低分',
round(avg(sc.score),2) as '平均分',
count(sc.cid) as '选课人数',
round(sum(case when sc.score>=60 then 1 else 0 end)/count(sc.cid),2) as '及格率',
round(sum(case when sc.score>=70 and sc.score<80 then 1 else 0 end)/count(sc.cid),2) as '中等率',
round(sum(case when sc.score>=80 and sc.score<90 then 1 else 0 end)/count(sc.cid),2) as '优良率',
round(sum(case when sc.score>=90 then 1 else 0 end)/count(sc.cid),2) as '优秀率'
from sc
join course as c on sc.cid=c.cid
group by sc.cid,c.cname
order by '选修人数' desc,sc.cid;
+------+--------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+
| cid | cname | 最高分 | 最低分 | 平均分 | 选课人数 | 及格率 | 中等率 | 优良率 | 优秀率 |
+------+--------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+
| 01 | 语⽂ | 80.0 | 31.0 | 64.50 | 6 | 0.67 | 0.33 | 0.33 | 0.00 |
| 02 | 数学 | 90.0 | 30.0 | 72.67 | 6 | 0.83 | 0.00 | 0.50 | 0.17 |
| 03 | 英语 | 99.0 | 20.0 | 68.50 | 6 | 0.67 | 0.00 | 0.33 | 0.33 |
+------+--------+-----------+-----------+-----------+--------------+-----------+-----------+-----------+-----------+
18.按各科平均成绩进⾏排序,并显示排名, Score 重复时保留名次空缺
先获取各科的平均成绩并排序
select cid,round(avg(sc.score),2) as avg_score
from sc
group by cid
order by avg_score
+------+-----------+
| cid | avg_score |
+------+-----------+
| 01 | 64.50 |
| 03 | 68.50 |
| 02 | 72.67 |
+------+-----------+
那么如何获得排名呢?
select s1.*,s2.*
from
(
select cid,round(avg(sc.score),2) as avg_score
from sc
group by cid
)as s1
join (
select cid,round(avg(sc.score),2) as avg_score
from sc
group by cid
)as s2 on s1.avg_score>=s2.avg_score;
自连接
+------+-----------+------+-----------+
| cid | avg_score | cid | avg_score |
+------+-----------+------+-----------+
| 01 | 64.50 | 01 | 64.50 |
| 02 | 72.67 | 01 | 64.50 |
| 03 | 68.50 | 01 | 64.50 |
| 02 | 72.67 | 02 | 72.67 |
| 02 | 72.67 | 03 | 68.50 |
| 03 | 68.50 | 03 | 68.50 |
+------+-----------+------+-----------+
按照s2进行分组,然后统计s1的平均分出现的次数,
为什么要这么搞呢?我们的关联条件 s1.avg_score>=s2.avg_score 中
最大的数字只能找到自己满足条件 值为 1
倒数第二大的数组可以找到自己和最大的数 值为 2
以此类推,得到的结果就是排名
select s2.cid,s2.avg_score,count(distinct s1.avg_score) as rank
from
(
select cid,round(avg(sc.score),2) as avg_score
from sc
group by cid
)as s1
join
(
select cid,round(avg(sc.score),2) as avg_score
from sc
group by cid
)as s2
on s1.avg_score>=s2.avg_score
group by s2.cid,s2.avg_score
order by rank;
+------+-----------+------+
| cid | avg_score | rank |
+------+-----------+------+
| 02 | 72.67 | 1 |
| 03 | 68.50 | 2 |
| 01 | 64.50 | 3 |
+------+-----------+------+
题目要求: Score 重复时保留名次空缺
这里我们三个课程成绩不一样,我们临时修改一下看一下结果
begin;
update sc set score =104.0 where sid=01 and cid=01;
select s2.cid,s2.avg_score,count(distinct s1.avg_score) as rank
from
(
select cid,round(avg(sc.score),2) as avg_score
from sc
group by cid
)as s1
join
(
select cid,round(avg(sc.score),2) as avg_score
from sc
group by cid
)as s2
on s1.avg_score>=s2.avg_score
group by s2.cid,s2.avg_score
order by rank;
rollback;
Score 重复时不保留名次空缺
+------+-----------+------+
| cid | avg_score | rank |
+------+-----------+------+
| 02 | 72.67 | 1 |
| 03 | 68.50 | 2 |
| 01 | 68.50 | 2 |
+------+-----------+------+
begin;
update sc set sc.score=50 where sc.sid=06;
select s1.sid,s1.sname,s1.total_score,count(s1.total_score) as rank
from (
select student.sid,student.sname,sum(sc.score) as total_score
from student
join sc on sc.sid=student.sid
group by student.sid,student.sname
order by total_score
) as s1
join (
select student.sid,student.sname,sum(sc.score) as total_score
from student
join sc on sc.sid=student.sid
group by student.sid,student.sname
order by total_score
) as s2
on s1.total_score<=s2.total_score
group by s1.sid,s1.sname,s1.total_score
order by rank;
rollback;
//Help
+------+--------+-------------+------+
| sid | sname | total_score | rank |
+------+--------+-------------+------+
| 01 | 赵雷 | 269.0 | 1 |
| 03 | 孙⻛ | 240.0 | 2 |
| 02 | 钱电 | 210.0 | 3 |
| 07 | 郑⽵ | 187.0 | 4 |
| 05 | 周梅 | 163.0 | 5 |
| 06 | 吴兰 | 100.0 | 7 |
| 04 | 李云 | 100.0 | 7 |
+------+--------+-------------+------+
19.按各科平均成绩进⾏排序,并显示排名, Score 重复时不保留名次空缺
我们发现第一个数据排名是1,第二是2,那么我们有没有什么简单的方法呢?
由于我们表中没有重复的数据,这里我们临时改一下数据,定义变量:@i
begin;
#临时修改数据
update sc set score =104.0 where sid=01 and cid=01;
select b.cid,b.avg_score,@i:=@i+1 as rank
from
(select @i:=0) as a,
(
select cid,round(avg(score),2) as avg_score
from sc
group by cid
order by avg_score desc
)
as b;
rollback;
Score 重复时不保留名次空缺
+------+-----------+------+
| cid | avg_score | rank |
+------+-----------+------+
| 02 | 72.67 | 1 |
| 01 | 68.50 | 2 |
| 03 | 68.50 | 3 |
+------+-----------+------+
20.查询学⽣的总成绩,并进⾏排名,总分重复时保留名次空缺
由于我们表中没有重复的总分,这里我们临时改一下数据
begin;
update sc set sc.score=50 where sc.sid=06;
select student.sid,student.sname,sum(sc.score) as total_score
from student
join sc on sc.sid=student.sid
group by student.sid,student.sname
order by total_score;
rollback;
+------+--------+-------------+
| sid | sname | total_score |
+------+--------+-------------+
| 06 | 吴兰 | 100.0 |
| 04 | 李云 | 100.0 |
| 05 | 周梅 | 163.0 |
| 07 | 郑⽵ | 187.0 |
| 02 | 钱电 | 210.0 |
| 03 | 孙⻛ | 240.0 |
| 01 | 赵雷 | 269.0 |
+------+--------+-------------+
题目要求:总分重复时保留名次空缺
begin;
update sc set sc.score=50 where sc.sid=06;
select s1.sid,s1.sname,s1.total_score,count(distinct s1.total_score) as rank,
from (
select student.sid,student.sname,sum(sc.score) as total_score
from student
join sc on sc.sid=student.sid
group by student.sid,student.sname
order by total_score;
) as s1
join (
select student.sid,student.sname,sum(sc.score) as total_score
from student
join sc on sc.sid=student.sid
group by student.sid,student.sname
order by total_score;
) as s2
on s1.total_score>=s2.total_score;
group by s1.sid,s1.sname,s1.total_score
order by rank;
rollback;
21.查询学⽣的总成绩,并进⾏排名,总分重复时不保留名次空缺
使用临时变量来实现
begin;
update sc set sc.score=50 where sc.sid=06;
select b.sid,b.total_score,@i:=@i+1 as rank
from
(select @i:=0) as a,
(
select sid,sum(score) as total_score
from sc
group by sid
order by total_score desc
)
as b;
rollback;
不保留名次空缺
+------+-------------+------+
| sid | total_score | rank |
+------+-------------+------+
| 01 | 269.0 | 1 |
| 03 | 240.0 | 2 |
| 02 | 210.0 | 3 |
| 07 | 187.0 | 4 |
| 05 | 163.0 | 5 |
| 04 | 100.0 | 6 |
| 06 | 100.0 | 7 |
+------+-------------+------+
22.统计各科成绩各分数段⼈数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0]及所占百分⽐
select sc.cid,c.cname,
max(sc.score) as '最高分',
min(sc.score) as '最低分',
round(avg(sc.score),2) as '平均分',
count(sc.cid) as '选课人数',
round(sum(case when sc.score>=85 then 1 else 0 end)/count(sc.cid),2) as '[100-85]',
round(sum(case when sc.score>=70 and sc.score<85 then 1 else 0 end)/count(sc.cid),2) as '[85-70]',
round(sum(case when sc.score>=60 and sc.score<70 then 1 else 0 end)/count(sc.cid),2) as '[70-60]',
round(sum(case when sc.score>=0 and sc.score<60 then 1 else 0 end)/count(sc.cid),2) as '[60-0]'
from sc
join course as c on sc.cid=c.cid
group by sc.cid,c.cname
order by '选修人数' desc,sc.cid;
+------+--------+-----------+-----------+-----------+--------------+----------+---------+---------+--------+
| cid | cname | 最高分 | 最低分 | 平均分 | 选课人数 | [100-85] | [85-70] | [70-60] | [60-0] |
+------+--------+-----------+-----------+-----------+--------------+----------+---------+---------+--------+
| 01 | 语⽂ | 80.0 | 31.0 | 64.50 | 6 | 0.00 | 0.67 | 0.00 | 0.33 |
| 02 | 数学 | 90.0 | 30.0 | 72.67 | 6 | 0.50 | 0.17 | 0.17 | 0.17 |
| 03 | 英语 | 99.0 | 20.0 | 68.50 | 6 | 0.33 | 0.33 | 0.00 | 0.33 |
+------+--------+-----------+-----------+-----------+--------------+----------+---------+---------+--------+
23.查询各科成绩前三名的记录
union 加 limit 来完成
select s1.* from(select sid,cid,score
from sc where cid =01
GROUP BY sid,cid,score
order by score desc limit 3
) as s1
union
select s2.* from (select sid,cid,score
from sc where cid =02
GROUP BY sid,cid,score
order by score desc limit 3
) as s2
union
select s3.* from (select sid,cid,score
from sc where cid =03
GROUP BY sid,cid,score
order by score desc limit 3
) as s3;
+------+------+-------+
| sid | cid | score |
+------+------+-------+
| 03 | 01 | 80.0 |
| 01 | 01 | 80.0 |
| 05 | 01 | 76.0 |
| 01 | 02 | 90.0 |
| 07 | 02 | 89.0 |
| 05 | 02 | 87.0 |
| 01 | 03 | 99.0 |
| 07 | 03 | 98.0 |
| 02 | 03 | 80.0 |
+------+------+-------+
24.查询每⻔课程被选修的学⽣数
select sc.cid,count(sc.cid)
from sc
group by sc.cid;
+------+---------------+
| cid | count(sc.cid) |
+------+---------------+
| 01 | 6 |
| 02 | 6 |
| 03 | 6 |
+------+---------------+
25.查询出只选修两⻔课程的学⽣学号和姓名
select stu.sid,stu.sname
from student as stu
join sc on stu.sid=sc.sid
group by stu.sid,stu.sname
having count(sc.cid)=2
+------+--------+
| sid | sname |
+------+--------+
| 05 | 周梅 |
| 06 | 吴兰 |
| 07 | 郑⽵ |
+------+--------+
26.查询男⽣、⼥⽣⼈数
select count(*) from student where Ssex='男';
select count(*) from student where Ssex='女';
+----------+
| count(*) |
+----------+
| 4 |
+----------+
+----------+
| count(*) |
+----------+
| 8 |
+----------+
如果要一起显示,可以这么写
select ssex,count(ssex) as 数量 from student GROUP BY ssex;
+------+--------+
| ssex | 数量 |
+------+--------+
| ⼥ | 8 |
| 男 | 4 |
+------+--------+
27.查询名字中含有「⻛」字的学⽣信息
select * from student where sname like '%风%';
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 03 | 孙⻛ | 1990-12-20 00:00:00 | 男 |
+------+--------+---------------------+------+
28.查询同名同性学⽣名单,并统计同名⼈数
select count(*) as '同名同姓人数'
from student as s1
join student as s2 on s1.sname=s2.sname and s1.Ssex=s2.Ssex and s1.sid!=s2.sid
+--------------------+
| 同名同姓人数 |
+--------------------+
| 2 |
+--------------------+
29.查询 1990 年出⽣的学⽣名单
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙⻛ | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
+------+--------+---------------------+------+
30.查询每⻔课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select sc.cid,avg(sc.score) as avg_score
from sc
group by sc.cid
order by avg_score desc,sc.cid;
+------+-----------+
| cid | avg_score |
+------+-----------+
| 02 | 72.66667 |
| 03 | 68.50000 |
| 01 | 64.50000 |
+------+-----------+
31.查询平均成绩⼤于等于 85 的所有学⽣的学号、姓名和平均成绩
select stu.sid,stu.sname,avg(sc.score) as '平均成绩'
from student as stu
join sc on sc.sid=stu.sid
group by stu.sid,stu.sname
having avg(sc.score)>=85;
+------+--------+--------------+
| sid | sname | 平均成绩 |
+------+--------+--------------+
| 01 | 赵雷 | 89.66667 |
| 07 | 郑⽵ | 93.50000 |
+------+--------+--------------+
32.查询课程名称为「数学」,且分数低于 60 的学⽣姓名和分数
select stu.sname,sc.score,c.cname
from sc
join student as stu on stu.sid=sc.sid
join course as c on c.cid=sc.cid and c.cname='数学'
where sc.score<60;
33.查询所有学⽣的课程及分数情况(存在学⽣没成绩,没选课的情况)
存在学⽣没成绩,没选课的情况,我们应该把学生表作为主表,一门课都没选的学生则用NULL表示
select stu.sid,c.cname,sc.score
from student as stu
left join sc on stu.sid=sc.sid
left join course as c on sc.cid=c.cid
order by stu.sid;
34.查询任何⼀⻔课程成绩在 70 分以上的姓名、课程名称和分数
select stu.sname,c.cname,sc.score
from sc
join student as stu on stu.sid=sc.sid
join course as c on c.cid=sc.cid
where sc.score>70;
+--------+--------+-------+
| sname | cname | score |
+--------+--------+-------+
| 赵雷 | 语⽂ | 80.0 |
| 赵雷 | 数学 | 90.0 |
| 赵雷 | 英语 | 99.0 |
| 钱电 | 英语 | 80.0 |
| 孙⻛ | 语⽂ | 80.0 |
| 孙⻛ | 数学 | 80.0 |
| 孙⻛ | 英语 | 80.0 |
| 周梅 | 语⽂ | 76.0 |
| 周梅 | 数学 | 87.0 |
| 郑⽵ | 数学 | 89.0 |
| 郑⽵ | 英语 | 98.0 |
+--------+--------+-------+
35.查询不及格的课程
select cid,count(score) as '不及格'
from sc
where score<60
group by cid;
+------+-----------+
| cid | 不及格 |
+------+-----------+
| 01 | 2 |
| 02 | 1 |
| 03 | 2 |
+------+-----------+
36.查询课程编号为 01 且课程成绩在 80 分以上的学⽣的学号和姓名
select stu.sid,stu.sname,sc.score as '分数'
from student as stu
join sc on sc.sid=stu.sid and sc.cid='01' and sc.score>=80;
+------+--------+-------+
| sid | sname | 分数 |
+------+--------+-------+
| 01 | 赵雷 | 80.0 |
| 03 | 孙⻛ | 80.0 |
+------+--------+-------+
37.求每⻔课程的学⽣⼈数
select cid,count(cid) as '学生人数'
from sc
group by cid;
+------+--------------+
| cid | 学生人数 |
+------+--------------+
| 01 | 6 |
| 02 | 6 |
| 03 | 6 |
+------+--------------+
38.成绩不重复,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信息及其成绩
select student.*,sc.score
from student
join sc on sc.sid=student.sid and sc.cid='02' and sc.score = (
select max(score)
from student as stu
join sc on sc.sid=stu.sid
join course as c on c.cid=sc.cid
join teacher as t on t.tid=c.tid
where t.tname='张三'
);
+------+--------+---------------------+------+-------+
| SId | Sname | Sage | Ssex | score |
+------+--------+---------------------+------+-------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 90.0 |
+------+--------+---------------------+------+-------+
39.成绩有重复的情况下,查询选修「张三」⽼师所授课程的学⽣中,成绩最⾼的学⽣信息及其成绩
由于我们表中没有重复的总分,这里我们临时改一下数据
begin;
update sc set score=90 where sid=04 and cid=02;
select student.*,sc.score
from student
join sc on sc.sid=student.sid and sc.cid='02' and sc.score = (
select max(score)
from student as stu
join sc on sc.sid=stu.sid
join course as c on c.cid=sc.cid
join teacher as t on t.tid=c.tid
where t.tname='张三'
);
rollback;
+------+--------+---------------------+------+-------+
| SId | Sname | Sage | Ssex | score |
+------+--------+---------------------+------+-------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 90.0 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 90.0 |
+------+--------+---------------------+------+-------+
40.查询不同课程成绩相同的学⽣的学⽣编号、课程编号、学⽣成绩
select distinct s1.sid,s1.cid, s2.score
from sc as s1
join sc as s2 on s1.sid=s2.sid and s1.cid!=s2.cid and s1.score=s2.score;
+------+------+-------+
| sid | cid | score |
+------+------+-------+
| 03 | 02 | 80.0 |
| 03 | 03 | 80.0 |
| 03 | 01 | 80.0 |
| 06 | 03 | 50.0 |
| 06 | 01 | 50.0 |
+------+------+-------+
41.查询每⻔课程成绩最好的前两名
这里用union连接,每次limit两个
select s1.*
from(
select sid,cid,score
from sc where cid =01
GROUP BY sid,cid,score
order by score desc limit 2
) as s1
union
select s2.*
from(
select sid,cid,score
from sc where cid =02
GROUP BY sid,cid,score
order by score desc limit 2
) as s2
union
select s3.*
from(
select sid,cid,score
from sc where cid =03
GROUP BY sid,cid,score
order by score desc limit 2
) as s3
+------+------+-------+
| sid | cid | score |
+------+------+-------+
| 03 | 01 | 80.0 |
| 01 | 01 | 80.0 |
| 01 | 02 | 90.0 |
| 07 | 02 | 89.0 |
| 01 | 03 | 99.0 |
| 07 | 03 | 98.0 |
+------+------+-------+
42.统计每⻔课程的学⽣选修⼈数(超过 5 ⼈的课程才统计)。
select cid,count(cid) as '学生人数'
from sc
group by cid
having count(cid)>5;
+------+--------------+
| cid | 学生人数 |
+------+--------------+
| 01 | 6 |
| 02 | 6 |
| 03 | 6 |
+------+--------------+
43.检索⾄少选修两⻔课程的学⽣学号
select sc.sid,count(sc.sid) as '课程数'
from sc
group by sc.sid
having count(sc.sid)>=2;
+------+-----------+
| sid | 课程数 |
+------+-----------+
| 01 | 3 |
| 02 | 3 |
| 03 | 3 |
| 04 | 3 |
| 05 | 2 |
| 06 | 2 |
| 07 | 2 |
+------+-----------+
44.查询选修了全部课程的学⽣信息
select sc.sid,count(sc.sid) as '课程数'
from sc
group by sc.sid
having count(sc.sid)=(select count(*) from course);
+------+-----------+
| sid | 课程数 |
+------+-----------+
| 01 | 3 |
| 02 | 3 |
| 03 | 3 |
| 04 | 3 |
+------+-----------+
45.查询各学⽣的年龄,只按年份来算
思路:用year函数将now时间与学生时间转换然后相减
select sname,year(now())-year(sage)as 年龄 from student order by sage;
+--------+--------+
| sname | 年龄 |
+--------+--------+
| 郑⽵ | 31 |
| 赵雷 | 30 |
| 李云 | 30 |
| 孙⻛ | 30 |
| 钱电 | 30 |
| 周梅 | 29 |
| 吴兰 | 28 |
| 李四 | 8 |
| 赵六 | 7 |
| 孙七 | 6 |
| 张三 | 3 |
| 李四 | 3 |
+--------+--------+
46.查询各学生的年龄,按照出⽣⽇期来算,当前⽉⽇ < 出⽣年⽉的⽉⽇则,年龄减⼀
使用 TIMESTAMPDIFF 函数,传入年,Sage和现在的时间,计算年份
select sid,sname,TIMESTAMPDIFF(YEAR,Sage,NOW()) as age from student;
+------+--------+------+
| sid | sname | age |
+------+--------+------+
| 01 | 赵雷 | 30 |
| 02 | 钱电 | 29 |
| 03 | 孙⻛ | 29 |
| 04 | 李云 | 29 |
| 05 | 周梅 | 28 |
| 06 | 吴兰 | 28 |
| 07 | 郑⽵ | 31 |
| 09 | 张三 | 2 |
| 10 | 李四 | 2 |
| 11 | 李四 | 8 |
| 12 | 赵六 | 7 |
| 13 | 孙七 | 6 |
+------+--------+------+
47.查询本周过⽣⽇的学⽣
思路:学习week的使用
没有数据,临时改一下
begin;
update student set sage=now() where sid='01';
SELECT *
FROM student where
week(Sage)=week(now());
rollback;
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 2020-09-14 20:00:16 | 男 |
+------+--------+---------------------+------+
48、查询下周过生日的学生
日期加减法比较麻烦,这里只给代码了
SELECT *
FROM student where
week(Sage)=week(now())+1;
49.查询本⽉过⽣⽇的学⽣
思路:学习month的使用
没有数据,临时改一下
begin;
update student set sage=now() where sid='01';
SELECT *
FROM student where
month(Sage)=month(now());
rollback;
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 2020-09-14 20:01:47 | 男 |
+------+--------+---------------------+------+
50.查询下⽉过⽣⽇的学⽣
日期加减法比较麻烦,这里只给代码了
SELECT *
FROM student
where month(Sage)=month(now())+1;
本文地址:https://blog.csdn.net/weixin_44141495/article/details/108588964
希望与广大网友互动??
点此进行留言吧!