MySQL经典50题
废话少说,快上码!!!
动手敲更有感觉,所以下面都是配图(我使用的客户端是DBeaver)。
在做题之前先把已知条件配置好。
注意:逗号是用来分隔数据的, 而分号是用来结束输入的;
然后开始向每一个表里填充数据。

insert into student (s_id, s_name, s_birth, s_sex) values ('01' , '赵雷' , '1990-01-01' , '男'), ('02' , '钱电' , '1990-12-21' , '男'), ('03' , '孙风' , '1990-05-20' , '男'), ('04' , '李云' , '1990-08-06' , '男'), ('05' , '周梅' , '1991-12-01' , '女'), ('06' , '吴兰' , '1992-03-01' , '女'), ('07' , '郑竹' , '1989-07-01' , '女'), ('08' , '王菊' , '1990-01-20' , '女'); insert into course (c_id, c_name, t_id) values ('01' , '语文' , '02'), ('02' , '数学' , '01'), ('03' , '英语' , '03'); insert into teacher (t_id, t_name) values ('01' , '张三'), ('02' , '李四'), ('03' , '王五'); insert into score (s_id, c_id, c_score) values ('01' , '01' , 80), ('01' , '02' , 90), ('01' , '03' , 99), ('02' , '01' , 70), ('02' , '02' , 60), ('02' , '03' , 80), ('03' , '01' , 80), ('03' , '02' , 80), ('03' , '03' , 80), ('04' , '01' , 50), ('04' , '02' , 30), ('04' , '03' , 20), ('05' , '01' , 76), ('05' , '02' , 87), ('06' , '01' , 31), ('06' , '03' , 34), ('07' , '02' , 89), ('07' , '03' , 98);
到目前,我们的已知条件都配备好了,让我们一起来看看成效吧!
# 先给出一个小目标
1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 6、查询"李"姓老师的数量 7、查询学过"张三"老师授课的同学的信息 8、查询没学过"张三"老师授课的同学的信息 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息 11、查询没有学全所有课程的同学的信息 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 14、查询没学过"张三"老师讲授的任一门课程的学生姓名 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 16、检索"01"课程分数小于60,按分数降序排列的学生信息 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 19、按各科成绩进行排序,并显示排名(实现不完全) 20、查询学生的总成绩并进行排名 21、查询不同老师所教不同课程平均分从高到低显示 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比 24、查询学生平均成绩及其名次 25、查询各科成绩前三名的记录 26、查询每门课程被选修的学生数 27、查询出只有两门课程的全部学生的学号和姓名 28、查询男生、女生人数 29、查询名字中含有"风"字的学生信息 30、查询同名同性学生名单,并统计同名人数 31、查询1990年出生的学生名单 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 34、查询课程名称为"数学",且分数低于60的学生姓名和分数 35、查询所有学生的课程及分数情况; 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数 37、查询不及格的课程 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名 39、求每门课程的学生人数 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 42、查询每门功成绩最好的前两名 43、统计每门课程的学生选修人数(超过5人的课程才统计);要求输出课程号和选修人数,查询结果按人数降序排列。 44、检索至少选修两门课程的学生学号 45、查询选修了全部课程的学生信息 46、查询各学生的年龄 47、查询本周过生日的学生 48、查询下周过生日的学生 49、查询本月过生日的学生 50、查询下月过生日的学生

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数:
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩:

6、查询"李"姓老师的数量 :
7、查询学过"张三"老师授课的同学的信息 :
8、查询没学过"张三"老师授课的同学的信息 :
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息:
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息:
11、查询没有学全所有课程的同学的信息:
解法一:
、
解法二:
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息:
解法一:
解法二:
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息:
14、查询没学过"张三"老师讲授的任一门课程的学生姓名:
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩:
16、检索"01"课程分数小于60,按分数降序排列的学生信息:
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩:
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率:
19、按各科成绩进行排序,并显示排名(待完善)
20、查询学生的总成绩并进行排名:
排名函数参考链接:https://www.cnblogs.com/7moon/p/14210663.html
21、查询不同老师所教不同课程平均分从高到低显示:
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩:
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比:
24、查询学生平均成绩及其名次
25、查询各科成绩前三名的记录
26、查询每门课程被选修的学生数
27、查询出只有两门课程的全部学生的学号和姓名
28、查询男生、女生人数
29、查询名字中含有"风"字的学生信息
30、查询同名同性学生名单,并统计同名人数
31、查询1990年出生的学生名单
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
34、查询课程名称为"数学",且分数低于60的学生姓名和分数
35、查询所有学生的课程及分数情况
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
37、查询不及格的课程
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
39、求每门课程的学生人数
40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
42、查询每门功成绩最好的前两名
43、统计每门课程的学生选修人数(超过5人的课程才统计);要求输出课程号和选修人数,查询结果按人数降序排列
44、检索至少选修两门课程的学生学号
45、查询选修了全部课程的学生信息
46、查询各学生的年龄
47、查询本周过生日的学生
48、查询下周过生日的学生
49、查询本月过生日的学生
50、查询下月过生日的学生
全部代码:

1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 select student.*, s1.c_score as '01课程分数', s2.c_score as '02课程分数' from student, score as s1, score as s2 where s1.s_id = s2.s_id and s1.c_id = '01' and s2.c_id = '02' and s1.c_score >s2.c_score and s1.s_id = student.s_id ; 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数 select student.*, s1.c_score as '01课程分数', s2.c_score as '02课程分数' from student, score as s1, score as s2 where s1.s_id = s2.s_id and s1.c_id = '01' and s2.c_id = '02' and s1.c_score <s2.c_score and s1.s_id = student.s_id ; 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩 select score.s_id, student.s_name, avg(c_score) as 'average score' from score inner join student on score.s_id = student.s_id group by (s_id) having avg(c_score)>= 60; 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 select score.s_id, student.s_name, avg(c_score) as 'average score' from score inner join student on score.s_id = student.s_id group by (s_id) having avg(c_score)< 60; 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 select score.s_id, student.s_name, count(c_id), sum(c_score) from score, student where score.s_id = student.s_id group by (s_id); 6、查询"李"姓老师的数量 select count(t_id) from teacher where t_name like '李%'; 7、查询学过"张三"老师授课的同学的信息 select distinct score.s_id, student.* from score, student where score.s_id = student.s_id and score.c_id in ( select course.c_id from teacher, course where teacher.t_id = course.t_id and teacher.t_name = '张三'); 8、查询没学过"张三"老师授课的同学的信息 select distinct student.* from score, student where score.s_id not in ( select distinct s_id from score where c_id in ( select course.c_id from teacher, course where teacher.t_id = course.t_id and teacher.t_name = '张三')) and score.s_id = student.s_id ; 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 select student .* from score as s1, score as s2, student where s1.s_id = s2.s_id and s1.c_id = '01' and s2.c_id = '02' and s1.s_id = student.s_id ; 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息 select student.* from ( select distinct s_id from score where s_id in ( select s_id from score where c_id = '01') and s_id not in ( select s_id from score where c_id = '02')) as s1, student where s1.s_id = student.s_id ; # 注:1、找到学过01课程的学号,找到没有学过02课程的学号 2、将子查询的结果作为一个表,起别名时as可以省略 11、查询没有学全所有课程的同学的信息 select student .* from ( select * from score group by (score.s_id) having count(score.s_id) <> ( select count(c_id) from course)) as s1, student where s1.s_id = student .s_id ; 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 select student .* from ( select distinct s_id from score where c_id in ( select c_id from score where s_id = '01') and s_id <> '01') as s1, student where s1.s_id = student .s_id; 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 select * from student s where not exists( select * from score s2 where s2.s_id = '01' and not exists( select * from score as s3 where s3.c_id = s2.c_id and s3.s_id = s.s_id)) and not exists( select * from score s2 where s2.s_id = s.s_id and not exists( select * from score as s3 where s3.c_id = s2.c_id and s3.s_id = '01')) and s.s_id <> '01'; # 不存在这样一门课————‘01’号同学学过而我没有学过,并且我学过但‘01’号同学没有学过 14、查询没学过"张三"老师讲授的任一门课程的学生姓名 select * from student s where not exists ( select * from score s2, course c, teacher t where s2.c_id = c.c_id and c.t_id = t.t_id and t.t_name = '张三' and s.s_id = s2.s_id); # 注:不存在这样一门课,我学过但任课老师是"张三" 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 select score.s_id, student.s_name, avg(c_score) from score, student where score.s_id in ( select s_id from score where c_score < 60 group by (s_id) having (count(*)>= 2)) and score.s_id = student .s_id group by (score.s_id) # 注where是先过滤在分组,having 是先分组在过滤 16、检索"01"课程分数小于60,按分数降序排列的学生信息 select distinct student .* from score, student where c_score < 60 and score.s_id = student .s_id order by c_score desc; 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 select score.*, average_score from ( select s_id, avg(c_score) as average_score from score group by (s_id)) as s1, score where s1.s_id = score.s_id order by average_score desc; 注:在score表的基础上添加一列平均成绩 18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 select score.c_id, course.c_name, max(c_score), min(c_score), avg(c_score), count(if(c_score >= 60, true, null))/ count(score.c_id) as '及格率', count(if(c_score >= 70 and c_score <80, true, null))/ count(score.c_id) as '中等率', count(if(c_score >= 80 and c_score <90, true, null))/ count(score.c_id) as '优良率', count(if(c_score >= 90 and c_score <= 100, true, null))/ count(score.c_id) as '优秀率' from score inner join course on score.c_id = course.c_id group by (c_id); 注: count等函数的执行顺序在group by后面 19、按各科成绩进行排序,并显示排名(待完善) select s1.c_id , s1.s_id , s1.c_score , count(s2.c_score)+1 as MyRank from score s1 inner join score s2 on s1.c_score < s2.c_score and s1.c_id = s2.c_id group by s1.c_id , s1.s_id ,s1.c_score order by s1.c_id, MyRank asc; 20、查询学生的总成绩并进行排名 select s.s_id , sum(s.c_score) , dense_rank() over( order by sum(s.c_score) desc) as `RANK` from score s group by s.s_id; 21、查询不同老师所教不同课程平均分从高到低显示 select t.t_name, c.c_name, avg(s.c_score) as average_score from course c , score s , teacher t where c.t_id = t.t_id and c.c_id = s.c_id group by t.t_id, c.c_id order by average_score desc; 注:GROUP BY X, Y意思是将所有具有相同X字段值和Y字段值的记录放到一个分组里 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩 select * from (select s.*, row_number() over(order by s.c_score desc) as MyRANK from score s where s.c_id ='01' limit 1,2) as f1 union select * from (select s.*, row_number() over(order by s.c_score desc) as MyRANK from score s where s.c_id ='02' limit 1,2) as f2 union select * from (select s.*, row_number() over(order by s.c_score desc) as MyRANK from score s where s.c_id ='03' limit 1,2) as f3; -- 注:limit的第一个参数表示偏移量,第二个参数表示返回结果元组的最大条数 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比 select s.c_id, c.c_name, count(if(s.c_score<60, true, null))/ count(c_score) as '[0-60]及所占百分比', count(if(s.c_score<70 and s.c_score >= 60, true, null))/ count(c_score) as '[70-60]及所占百分比', count(if(s.c_score<85 and s.c_score >= 70, true, null))/ count(c_score) as '[85-70]及所占百分比', count(if(s.c_score <= 100 and s.c_score >= 85, true, null))/ count(c_score) as '[100-85]及所占百分比' from score s inner join course c on s.c_id = c.c_id group by s.c_id ; 注:count函数执行于group by函数之后,所以此处的计数是针对于一个组来说的 24、查询学生平均成绩及其名次 select s.s_id , avg(s.c_score), dense_rank() over( order by avg(s.c_score) desc) as 'rank' from score s group by s.s_id ; 25、查询各科成绩前三名的记录 select * from score s where ( select count(*) from score s2 where s.c_id = s2.c_id and s.c_score <= s2.c_score)<= 3 order by s.c_id , s.c_score desc ; 26、查询每门课程被选修的学生数 select c.c_id , c.c_name , count(s.s_id) as num from course c left join score s on c.c_id = s .c_id group by c.c_id ; 注:1、有可能会有某一门课没有被学生选择,所以用了左连接 2、起了别名之后就不要再用原来的名字,否则会报错 27、查询出只有两门课程的全部学生的学号和姓名 select s.s_id, s2.s_name from score s, student s2 where s.s_id = s2.s_id group by s.s_id having count(s.c_id)= 2; 28、查询男生、女生人数 select count(if(s_sex = '男', true, null))as '男', count(if(s_sex = '女', true, null)) as '女' from student; 29、查询名字中含有"风"字的学生信息 select * from student where s_name like '%风%'; 30、查询同名同性学生名单,并统计同名人数 select s1.*, s2.num from ( select s.* from student s group by s.s_name , s.s_sex) as s1, ( select s.s_name, count(*) as num from student s group by s.s_name) as s2 where s1.s_name = s2.s_name ; -- 注:这里用了组合两个表的思想;num表示同名人数 31、查询1990年出生的学生名单 select * from student s where year(s_birth)= 1990; 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 select s.c_id , avg(s.c_score) as average_score from score s group by s.c_id order by average_score desc , s.c_id ; 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 select s.s_id , s2.s_name , avg(s.c_score) as average_score from score s inner join student s2 on s.s_id = s2.s_id group by s.s_id having avg(s.c_score)>= 85; 注:having的执行在select之前 34、查询课程名称为"数学",且分数低于60的学生姓名和分数 select s2.s_name , s.c_score from score s , student s2 , course c where s.c_score <60 and c.c_name = '数学' and s.s_id = s2.s_id and s.c_id = c.c_id ; 35、查询所有学生的课程及分数情况; select * from score s ; 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数 select s2.s_name , c.c_name , s.c_score from score s , course c , student s2 where s.c_score >= 70 and s.s_id = s2.s_id and s.c_id = c.c_id ; 37、查询不及格的课程 select s.s_id , c.c_id , c.c_name , s.c_score from score s, course c where s.c_score <60 and s.c_id = c.c_id ; 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名 select s.s_id , s2.s_name from score s inner join student s2 on s.s_id = s2.s_id where s.c_id = '01' and s.c_score >= 80; 39、求每门课程的学生人数 select s.c_id , count(s.s_id) from score s group by s.c_id ; 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩 select s2.*, s.c_score from score s , course c, teacher t, student s2 where s.c_id = c.c_id and c.t_id = t.t_id and s.s_id = s2.s_id and t.t_name = '张三' order by s.c_score desc limit 1; 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 select t2.* from ( select distinct s.s_id from score s inner join score s2 on s.s_id = s2.s_id where s.c_id <> s2.c_id and s.c_score = s2.c_score) as t1, score as t2 where t1.s_id = t2.s_id -- 注:将找到具有不同课程相同成绩的学生学号作为一个新表 42、查询每门功成绩最好的前两名 select * from score a where ( select count(*) from score b where a.c_id = b.c_id and a.c_score <= b.c_score )<= 2 order by c_id , c_score desc; -- 注:相关子查询,每次父查询取一个元组来和子查询的所有元组进行比较; 43、统计每门课程的学生选修人数(超过5人的课程才统计);要求输出课程号和选修人数,查询结果按人数降序排列。 select s.c_id , count(s.s_id) as num from score s group by s.c_id having count(s.s_id)>5 order by num desc ; 44、检索至少选修两门课程的学生学号 select s.s_id from score s group by s.s_id having count(s.c_id)>= 2; 45、查询选修了全部课程的学生信息 select * from student s where not exists ( select * from course c where not exists( select * from score s2 where s.s_id = s2.s_id and s2.c_id = c.c_id)); -- 注:不存在这样一门课————在课程表里但我没学过 46、查询各学生的年龄 select s.s_name , TIMESTAMPDIFF(year, s.s_birth, curdate()) as age from student s; 47、查询本周过生日的学生 select * from student s where week(date_add(s.s_birth, interval year(curdate())-year(s.s_birth) year))=week(curdate()); -- 注:date_add(s.s_birth, interval year(curdate())-year(s.s_birth) year)的意思是将生日中的年份改为当前年份 48、查询下周过生日的学生 select * from student s where week(date_add(s.s_birth, interval year(curdate())-year(s.s_birth) year))-week(curdate())=1; 49、查询本月过生日的学生 select * from student s where month(s.s_birth)= month(curdate()); 50、查询下月过生日的学生 select * from student s where month(s.s_birth)-month(curdate())= 1;
若您有更优的解法,请评论区留言
相互学习,共同进步!
Thank you!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?