MySQL第41题怎么评分_Mysql41道练习题

本文详细介绍了使用MySQL进行复杂查询的多种方法,包括对比不同科目成绩、筛选平均分超过60的学生、查找特定教师课程的学生、查询学过特定课程组合的学生等。通过实际操作示例,深入理解MySQL的查询技巧和性能优化。
摘要由CSDN通过智能技术生成

1、自行创建测试数据

2、查询“生物”课程比“物理”课程成绩高的所有学生的学号。ps:针对的是自己的生物成绩比物理成绩高,再把符合条件的学生的学号查出来;

0dba8f72a758253f1892c8a6657c189f.gif

# 查到 生物 和 物理的 id:

select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid;

# 将上面的id作为子查询的表 用来查, 所有有 生物 和物理的 表:

select score.student_id,group_concat(num),count(1) from score where course_id in (select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid) group by score.student_id;

# 获取所有的生物成绩的表

select * from score where course_id in (select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid) limit 0,11;

# 获取所有的物理成绩的表

select * from score where course_id in (select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid) limit 11,11;

# 做比较查询:

select shwu_num.student_id,shwu_num.sw,ty from (select student_id,num as sw from score where course_id in (select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid) limit 0,11) as shwu_num left join (select student_id,num as ty from score where course_id in (select course.cid from score left join course on score.course_id = course.cid where course.cname = '生物' or course.cname = '物理' group by course.cid) limit 11,11) as wuli_num on shwu_num.student_id = wuli_num.student_id where shwu_num.sw>wuli_num.ty;

b2c361d166a5a795ac4ad65162da7037.gif

6d0ce5df8337e11c7062674a4190ab3c.png

3、查询平均成绩大于60分的同学的学号和平均成绩;

select student_id,avg(num) from score group by student_id having avg(num) > 60; # 终于碰到简单了 我的天

825a317c0f6dbf4c7ffd916eb288ded3.png

4、查询所有同学的学号、姓名、选课数、总成绩;

# 将学生表和成绩表关联, 为了获取学生姓名:

select * from student left join score on student.sid = score.student_id;

# 将上面的表按照学生姓名分组

select student.sid,group_concat(student.sname),count(score.course_id),sum(num) from student left join score on student.sid = score.student_id group by student.sid;

8620fe32fd7affe4749f4313c46ed54d.png

5、查询姓“李”的老师的个数;

select count(1) from teacher where tname like '李%';

c2788637ec3e0540f25754652d648e0e.png

6、查询没学过“李平”老师课的同学的学号、姓名;

此题和38题 类似, 结果一样:

select sid, sname from student where sid not in(select student_id from score where course_id in (

select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师') group by student_id)

7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

43d3671f992297cfe8607b483b50e8a3.gif

# 等同于 学过生物和物理的学生的学号和姓名 且不需要和 课程连表:

# 1, 获取所有学过001的表

select * from score where course_id = 1;

# 2, 获取所有学过002的表

select * from score where course_id = 2;

# 3, 两张表关联, 并且找出, 既有001 也有002的表:

select s1.student_id from (select * from score where course_id = 1) as s1 left join (select * from score where course_id = 2 ) as s2 on s1.student_id = s2.student_id where s1.course_id = 1 and s2.course_id = 2;

# 4, 用学生表 和上面的表关联 拿到姓名:

select student.sid, student.sname from student right join (select s1.student_id from (select * from score where course_id = 1) as s1 left join (select * from score where course_id = 2 ) as s2 on s1.student_id = s2.student_id where s1.course_id = 1 and s2.course_id = 2) as s3 on student.sid = s3.student_id;

b1337cf7a6707b79d7ecf94ed86dec52.gif

dfba3d8d01d2f91dba39670edbc205da.png

8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

a4a15f17b37c161090ff8f75eb9063c9.gif

# 和第6题 差不多:

# 获取选择了李平老师所教的课程的 学生id表:

select student_id from score where course_id in (

select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师') group by student_id;

# 用学生表 和上面的表 关联,获取学生姓名:

select student.sid,student.sname from student right join (select student_id from score where course_id in (

select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师') group by student_id) as s1 on student.sid = s1.student_id;

ffbff2bc13ef18d142c308145ec581a7.gif

5cdb9b02db6cc53bfd97115e75155f76.png

9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

35c1f645403a39bf2dbbbcc20d209798.gif

# 此题需要从第七题的基础上开始写:

# 1, 获取所有学过001的表

select * from score where course_id = 1;

# 2, 获取所有学过002的表

select * from score where course_id = 2;

# 3, 两张表关联, 并且找出, 既有001 也有002的表:

select s1.student_id from (select * from score where course_id = 1) as s1 left join (select * from score where course_id = 2 ) as s2 on s1.student_id = s2.student_id where s1.num > s2.num;

# 4, 用学生表 和上面的表关联 拿到姓名:

select student.sid, student.sname from student right join (select s1.student_id from (select * from score where course_id = 1) as s1 left join (select * from score where course_id = 2 ) as s2 on s1.student_id = s2.student_id where s1.num > s2.num) as s3 on student.sid = s3.student_id;

d750eaaa3dba91d0e8f9bb189e50adc1.gif

4ea061b95253a152ad447462d932e0ed.png

10、查询有课程成绩小于60分的同学的学号、姓名;

# 1, 从 score表中找到所有小于60分的学生id 并且去重

select distinct student_id from score where num < 60;

# 2, 用子查询的方法 从学生表中找到学生id 在上表中的 学生id 和姓名

select sid,sname from student where sid in (select distinct student_id from score where num < 60);

a2c2e3e39268b273871f034ec43a8464.png

11、查询没有学全所有课的同学的学号、姓名;

# 1, 从score表中 按照学生id分组后找到 count(course_id) < 4 的学生id的表:

select student_id from score group by student_id having count(course_id) < 4;

# 2, 和学生表关联, 找到学生姓名:

select student.sid,student.sname from student right join (select student_id from score group by student_id having count(course_id) < 4) as s1 on student.sid = s1.student_id;

c44de5a5830585ecb0f9cf08ce613fdb.png

12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;

819e24edbdfe61d1d37106b22cd885e7.gif

# 1, 从score表中找到student_id = 学号001同学 所学的课程:

select course_id from score where student_id =1;

# 2, 从score表中 找到除了001 之外的所有学生 course_id in (上表) 中的所有信息:

select * from score where student_id != 1 and course_id in (select course_id from score where student_id =1);

# 3, 上表和学生表 关联, 获取姓名:

select distinct student.sid, student.sname from student right join (select * from score where student_id != 1 and course_id in (select course_id from score where student_id =1)) as s1 on student.sid = s1.student_id order by student.sid;

3fba3bbd6ee1c591a664a01eb0c52191.gif

3e59f74392879b932f4e31fa19f6ae61.png

13、查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名;

8727a54cf19996867adc97ac91f7b75b.gif

# 我觉的和12题一模一样:

# 1, 从score表中找到student_id = 学号001同学 所学的课程:

select course_id from score where student_id =1;

# 2, 从score表中 找到除了001 之外的所有学生 course_id in (上表) 中的所有信息:

select * from score where student_id != 1 and course_id in (select course_id from score where student_id =1);

# 3, 上表和学生表 关联, 获取姓名:

select distinct student.sid, student.sname from student right join (select * from score where student_id != 1 and course_id in (select course_id from score where student_id =1)) as s1 on student.sid = s1.student_id order by student.sid;

aa3e88265dc83b486d812f7c3d886173.gif

14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;

c4181dbc3e038b8538545bfe70fcbaf0.gif

# 1, 获取 002 同学的课程id

select course_id from score where student_id = 2;

# 2, 获取 002 同学选择的课程的 个数:

select count(1) from score where student_id = 2 group by student_id;

# 3, 获取和 002 学的课程数目一样的同学ID:

select student_id from score group by student_id having count(course_id) = (select count(1) from score where student_id = 2 group by student_id);

# 4, 获取除了 002 之外所有同学的课程id 在#1表中并且个数一样的 信息:

select * from score where student_id !=2 and course_id in (select course_id from score where student_id = 2) and student_id = (select student_id from score group by student_id having count(course_id) = (select count(1) from score where student_id = 2 group by student_id));

37a589bc803e1f4bb3759ce21a4257c9.gif

cb52795834d309955352a63de3561970.png

15、删除学习“叶平”老师课的SC表记录;

16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;

2f8126e87a42beb74b204023cb6ec471.gif

# insert 支持语法:

inset into tb1(xx,xx) select x1,x2 from tb2;

# 1, 找出没上过 002 课程的同学:

select student_id from score where student_id not in (select student_id from score where course_id =2) group by student_id;

# 2, 算出 002 课程的平均值:

select avg(num) from score where course_id = 2;

# 3, 插入 这些数据:

insert into score(student_id, course_id, num) select student_id,2,(select avg(num) from score where course_id = 2) from score where student_id not in (select student_id from score where course_id =2) group by student_id;e_id =2) group by student_id;

b04a3594d436ed9e74302a878e901b93.gif

1ebcbe57745226b01f3b0e9c0842716a.png

17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;

c2f2c33a3b86808b7011bddd925a6739.gif

# 1, 获取各个科目的成绩:

select num from score left join course on score.course_id = course.cid where course.cname='生物';

select num from score left join course on score.course_id = course.cid where course.cname='物理';

select num from score left join course on score.course_id = course.cid where course.cname='体育';

and sc.student_id = score.student_id

# 2, 创建表:

select sc.student_id,(select num from score left join course on score.course_id = course.cid where course.cname='生物' and sc.student_id = score.student_id) as sw, (select num from score left join course on score.course_id = course.cid where course.cname='物理' and sc.student_id = score.student_id) as wl, (select num from score left join course on score.course_id = course.cid where course.cname='体育' and sc.student_id = score.student_id) as ty, avg(sc.num) from score as sc group by sc.student_id desc;

ab72584cf33a5f4b476215512b812183.gif

4b8d33a1a107fd7421b8c97d6fdc36b0.png

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

# 1, 创建表:

select course_id, max(num), min(num) from score group by course_id;

03b4a768503575b228839f120a6cd94c.png

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;

select course_id, avg(num) from score group by course_id order by avg(num) desc;

1c1b9efea3354ba99b38e0e00da4a09b.png

20、课程平均分从高到低显示(现实任课老师);

99dd3591542e4bb541f81611681bf604.gif

# 在上一题的基础上 接着写:

# 与老师表连接:

select * from course right join (select course_id, avg(num) from score group by course_id order by avg(num) desc) as s1 on course.cid = s1.course_id;

select tname,cname,avgnum from teacher right join (select * from course right join (select course_id, avg(num) as avgnum from score group by course_id order by avgnum desc) as s1 on course.cid = s1.course_id) as s2 on teacher.tid = s2.teacher_id order by avgnum desc;

0224c3d79689dfd8ccc6bf939bf67eeb.gif

a0fd05a9d4e37e14a05c3dbe2a050331.png

21、查询各科成绩前三名的记录:(不考虑成绩并列情况)

22、查询每门课程被选修的学生数;

select course_id,count(1) from score group by course_id;

2dc9d0c067c7e068af52183c85f6a9b8.png

23、查询出只选修了一门课程的全部学生的学号和姓名;

select student_id,count(course_id) from score group by student_id having count(course_id) = 1;

1d5a84454cbc395e4fc8ee48f2b723e8.png

24、查询男生、女生的人数;

select gender,count(1) from student group by gender;

1e33ba0a20afa6071be374f53a675cbb.png

25、查询姓“张”的学生名单;

select * from student where sname like '张%';

7a87b1ca06696c7d7f0caafe1dbe30e5.png

26、查询同名同姓学生名单,并统计同名人数;

select * from student as s1 left join student as s2 on s1.sid = s2.sid where s1.sid != s2.sid and s1.sname = s2.sname;

ea358bc1654403ad665d061f90e0d1e0.png

27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;

select course_id,avg(num) from score group by course_id order by avg(num) asc, course_id desc;

89415d0542853747c0cb6b9eff238d5e.png

28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;

12f280bca3810add723ff8fa974f15c8.gif

# 1, 查询大于85的 学生 id

select student_id, avg(num) from score group by student_id having avg(num) >85;

# 2, 和学生表匹配 拿到名字

select student.sid, student.sname, s2.avgnum student right join (select student_id, avg(num) as avgnum from score group by student_id having avgnum >85) as s2 on student.sid = s2.student_id;

524e01d839193a2db893a1d1ee94dde9.gif

6f58d92e9a8173c03666de999cc16c89.png

29、查询课程名称为“生物”,且分数低于60的学生姓名和分数;

2d469b67568dc3bcb61ff8eb502da1d9.gif

# 1, 找到低于60分的生物;

select * from score left join course on score.course_id = course.cid where course.cname = '生物' and score.num<60;

# 2, 和学生表关联 拿到名字:

select sname,num from student right join (select * from score left join course on score.course_id = course.cid where course.cname = '生物' and score.num<60) as s2 on student.sid = s2.student_id;

fb2a8a4850b7cc22e43d7ff5859df02c.gif

38895ef7010b3a49c76b16e929c24f8f.png

30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;

# 1, 从score 中找到上面的条件的id

select * from score where course_id = 3 and num>80;

# 2, 和学生表关联:

select student.sid, sname, num from student right join (select * from score where course_id = 3 and num>80) as s2 on student.sid = s2.student_id;

23c4bfff96ee89dda2881b025cfea1d4.png

31、求选了课程的学生人数

32、查询选修“刘海燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩;

3f44f1308cb8012c44451cd20dd8ee9f.gif

# 先把课程表和老师表 关联:

select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '刘海燕老师';

# 再把这个虚拟表和成绩表关联

select * from score right join (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '刘海燕老师') as x_teacher on score.course_id = x_teacher.cid;

# 最后在把这个虚表和学生表关联 拿到学生的名字:

select * from student right join (select * from score right join (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '刘海燕老师') as x_teacher on score.course_id = x_teacher.cid) as x_tea_sco on student.sid = x_tea_sco.student_id;

# 按照成绩 逆序 并且只显示一个学生:

select sname,num from student right join (select * from score right join (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '刘海燕老师') as x_teacher on score.course_id = x_teacher.cid) as x_tea_sco on student.sid = x_tea_sco.student_id order by num desc limit 0,1;

43abb3a6c4549ac2b8815f2242732f87.gif

e2fc15fc11aec9314571f3316b7e4514.png

33、查询各个课程及相应的选修人数;

select course_id, count(1) from score group by course_id;

bc75c46c10770a6b265aa9e136d35a4f.png

34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;

select s1.student_id, s1.course_id, s2.course_id, s1.num from score as s1, score as s2 where s1.num = s2.num and s1.course_id != s2.course_id order by s1.student_id;

8a935812dd4eea92e9bb4a4660759382.png

35、查询每门课程成绩最好的前两名;

2ee8e52fb9c62e18988351ef0d6faf92.gif

# 拿课程表和分数表进行关联:

select * from course left join score on course.cid = score.course_id;

# 按照分数降序排序:

select * from (select * from course left join score on course.cid = score.course_id) as oded_tb order by num desc;

# 按照分数分组, 查看所有:

select num, group_concat(cou_sco_tb.cname) from(select * from (select * from course left join score on course.cid = score.course_id) as oded_tb order by num desc) as cou_sco_tb group by cou_sco_tb.num;

### 目前的结果还需要靠自己手动找找 暂时跳过;

8136ac2238d9425d340d31490a8ee8e2.gif

4726453fc23cf3ba001b349fc5ff4252.png

36、检索至少选修两门课程的学生学号;

select student_id from score group by student_id having count(course_id)>2;

# 注意: 当MySQL 通过语句: set global sql_mode='ONLY_FULL_GROUP_BY'; 之后更改了 SQL的全局模式, 所以在分组以后只能查看当前字段,如果想查看组内信息,需要借助于聚合函数

30ad93c03f709a83540d8cd78f5a4bfc.png

37、查询全部学生都选修的课程的课程号和课程名;

38、查询没学过“李平”老师讲授的任一门课程的学生姓名;

a5844f19423ce1513b2159ebec2d3113.gif

# 1, 先关联老师表和课程表, 获取到"李平老师"教的课程的id:

select * from course,teacher; # 查看着两个表的 笛卡尔积;

select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师'; # 获取李平老师 教的课程id

# 2, 从score中找出选了李平老师的 学生的id

select student_id from score where course_id in (

select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师') group by student_id;

# 3, 从student表中找到不在上面表中的学生的 id和姓名:

select sid, sname from student where sid not in(select student_id from score where course_id in (

select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师') group by student_id);

bb0576415cdd875e4ded1d9baaedae7a.gif

65601cfd20a965fd4d77752db03e768d.png

39、查询两门以上不及格课程的同学的学号及其平均成绩;

select student_id, avg(num) from score where num<60 group by student_id having count(student_id)>1;

22fd90297fe9aaabd4db6219da3de9f9.png

40、检索“004”课程分数小于60,按分数降序排列的同学学号;

select student_id from score where course_id = 4 and num < 60;

625295cc3711027808c7dcfde829897d.png

41、删除“002”同学的“001”课程的成绩;

delete from score where student_id = 2 and course_id = 1;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值