首页 / MYSQL / MySQL 练习题 附答案,未完
MySQL 练习题 附答案,未完
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MySQL 练习题 附答案,未完,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8488字,纯文字阅读大概需要13分钟。
内容图文
![MySQL 练习题 附答案,未完](/upload/InfoBanner/zyjiaocheng/916/8b19d3a5ecef49bfb9b44dab61fdb662.jpg)
综合练习题
表结构
整合一下方便查看
teacher
student
course
scors
练习题
1、自行创建测试数据
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
create table student( sid int primary key auto_increment, sname char(32), gender enum("女","男"), class_id int, constraint fk_class_id foreign key (class_id) references class(cid) on DELETE cascade on update cascade ); create table teacher( tid int primary key auto_increment, tname char(32) ); create table class( cid int primary key auto_increment, caption char(32) ); create table course( cid int primary key auto_increment, cname char(32), teacher_id int, constraint fk_teacher_id foreign key (teacher_id) references teacher(tid) on delete cascade on update cascade ); create table score( sid int primary key auto_increment, student_id int, corse_id int, number int, constraint fk_student_id foreign key (student_id) references student(sid) on delete cascade on update cascade, constraint fk_corse_id foreign key (corse_id) references score(sid) on delete cascade on update cascade );View Code
2、查询“体育”课程比“数学”课程成绩高的所有学生的学号;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select a.student_id from (select * from score where corse_id = (select cid from course where cname = '数学'))a inner join (select * from score where corse_id = (select cid from course where cname = '体育'))b on a.student_id = b.student_id and a.number > b.number;View Code
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select a.student_id from (select score.student_id, score.number from score left join course c on score.corse_id = c.cid where c.cname = '体育') a inner join (select score.student_id, score.number from score left join course c on score.corse_id = c.cid where c.cname = '数学') b on a.student_id = b.student_id and a.number < b.number;View Code
3、查询平均成绩大于60分的同学的学号和平均成绩;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select student_id, AVG(number)AS avg from score group by student_id having avg > 60;View Code
4、查询所有同学的学号、姓名、选课数、总成绩;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select student.sid, sname, count(*),sum(number) from student left join score s on student.sid = s.student_id group by student.sid;View Code
5、查询姓“长”的老师的个数;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select count(*) from teacher where tname like "长%" ;View Code
6、查询没学过“教授”老师课的同学的学号、姓名;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select s.sid,s.sname from score inner join student s on score.student_id = s.sid inner join course c on score.corse_id = c.cid inner join teacher t on c.teacher_id = t.tid where t.tname <> '教授' group by s.sidView Code
7、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select s.sid,s.sname from score inner join student s on score.student_id = s.sid inner join course c on score.corse_id = c.cid where c.cid in (1,2) group by s.sidView Code
8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select distinct s.sname from score left join student s on score.student_id = s.sid where corse_id not in (select cid from course left join teacher t on course.teacher_id = t.tid where tname = '教授')View Code
9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
思路 1 拿到成绩表中,课程为1 的记录 2 成绩表链表学生表 作为一个记录 3 重复上面步骤拿到课程为2 的链表记录 两个记录再基于相同的学生id链表。条件设置为 成绩比较
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select distinct a.sid,a.sname from (select distinct sc.number, st.sname, st.sid from score as sc inner join student st on sc.student_id = st.sid where corse_id = 2) a inner join (select distinct sc.number, st.sname, st.sid from score as sc inner join student st on sc.student_id = st.sid where corse_id = 1) b on a.sid = b.sid where a.number < b.numberView Code
10、查询有课程成绩小于60分的同学的学号、姓名;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select distinct st.sid, st.sname from student as st inner join score s on st.sid = s.student_id where s.number < 60View Code
11、查询没有学全所有课的同学的学号、姓名;
思路 1 查询出来所有的课程然后计数 2 成绩表按照学生分组后统计记录个数 3 小于总课程个数即为没学全 4 相反 等于 则为学全
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select st.sid, st.sname from student as st inner join score sc on st.sid = sc.student_id group by st.sid having count(*) < (select count(cid) from course)View Code
12、查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名;
思路 1 先拿到 1 同学所学的所有课程的id 1.1 要链表 student 和 course 以及 score 2 将1同学的课程列表当做条件来判断是否在里面即可
2.1 因为要查其他学生,排除掉 1 同学
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
from student as st inner join score sc on st.sid = sc.student_id inner join course as co on sc.corse_id = co.cid where st.sid <> 1 and sc.corse_id in (select co.cid from student as st inner join score sc on st.sid = sc.student_id inner join course as co on sc.corse_id = co.cid where st.sid = 1)View Code
13、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
同上,只是不需要再排除 1 同学
14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;
同上上,再条件判断的时候改为完全等于
15、删除学习“长老”老师课的成绩表记录;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
delete sc from score as sc inner join course as co on sc.sid = co.cid inner join teacher te on co.teacher_id = te.tid where tname = '长老'View Code
16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;
17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
20、课程平均分从高到低显示(显示任课老师);
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select co.cname, te.tname, avg(sc.number) av from score as sc, course as co, teacher as te where sc.corse_id = co.cid and co.teacher_id = te.tid group by sc.corse_id order by av descView Code
21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
22、查询每门课程被选修的学生数;
23、查询出只选修了一门课程的全部学生的学号和姓名;
24、查询男生、女生的人数;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select count(*) from student where gender ='男'; select count(*) from student where gender ='女';View Code
25、查询姓“张”的学生名单;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select * from student where sname like '萌%'View Code
26、查询同名同姓学生名单,并统计同名人数;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select sname,count(sname) from student group by snameView Code
27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select co.cname, avg(sc.number) as av from score as sc, course as co where sc.corse_id = co.cid group by sc.corse_id order by av asc, sc.corse_id descView Code
28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select st.sname, avg(sc.number) from student as st, score as sc where st.sid = sc.student_id group by st.sidView Code
29、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select st.sname, avg(sc.number) as av from student as st inner join score as sc on st.sid = sc.student_id inner join course as co on sc.corse_id = co.cid where co.cname = '数学' group by st.sid having av < 60View Code
30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select st.sid, st.sname from student as st inner join score sc on st.sid = sc.student_id where sc.corse_id = 3 and sc.number > 80View Code
31、求选了编号 ‘1 ’课程的学生人数
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select count(*) from student as st inner join score sc on st.sid = sc.student_id where sc.corse_id = 1View Code
32、查询选修“长老”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select st.sid, st.sname, max(sc.number) as max from student as st, score as sc, course as co, teacher as te where sc.corse_id = co.cid and st.sid = sc.student_id and co.teacher_id = te.tid and te.tname = '长老'View Code
33、查询各个课程及相应的选修人数;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select co.cname,count(*) from score as sc inner join course as co on sc.corse_id = co.cid group by sc.corse_idView Code
34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
35、查询每门课程成绩最好的前两名;
36、检索至少选修两门课程的学生学号;
37、查询全部学生都选修的课程的课程号和课程名;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select co.cid, co.cname from student as st, score as sc, course as co where sc.corse_id = co.cid and st.sid = sc.student_id group by sc.corse_id having count(*) = (select count(*) from student)View Code
38、查询没学过“长老”老师讲授的任一门课程的学生姓名;
39、查询两门以上不及格课程的同学的学号及其平均成绩;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select st.sid, st.sname, avg(sc.number) as av from student as st, score as sc, course as co where sc.corse_id = co.cid and st.sid = sc.student_id and sc.number < 60 having count(*) > 2View Code
40、检索“003”课程分数小于60,按分数降序排列的同学学号;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
select st.sid from score as sc inner join course as co on sc.corse_id = co.cid inner join student as st on sc.student_id = st.sid where sc.number < 60 order by sc.number descView Code
41、删除“002”同学的“001”课程的成绩;
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328724.jpg)
![MySQL 练习题 附答案,未完 - 文章图片](/upload/getfiles/0001/2021/5/9/20210509114328749.jpg)
delete sc from score as sc, student as st where sc.student_id = st.sid and st.sid = 2 and sc.corse_id = 1View Code
内容总结
以上是互联网集市为您收集整理的MySQL 练习题 附答案,未完全部内容,希望文章能够帮你解决MySQL 练习题 附答案,未完所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。