Mysql综合练习50题

Mysql综合练习50题

文章目录

表关系

建表

请创建如下表,并创建相关约束
班级表:class

cidcaptiongrade_id
1一年一班1
2一年二班1
3二年一班2
4三年二班3
5四年二班4
6五年二班5
7六年三班6

学生表:class

sidsnamegenderclass_id
1乔丹1
2艾弗森1
3科比2

老师表:teacher

tidtname
1张三
2李四
3王五

课程表:course

cidcnameteacher_id
1生物1
2体育1
3物理2

成绩表:score

sidstudent_idcourse_idscore
11160
21259
32299

年级表:class_grade

gidgname
1一年级
2二年级
3三年级
4四年级
5五年级
6六年级

班级任职表:teach2cls

tcidtidcid
111
212
321
432

建表代码

# 年级表
create table class_grade (
	gid int unsigned primary key auto_increment,
	gname char(10) not null
);

# 教师表
create table teacher (
	tid int unsigned primary key auto_increment,
	tname char(8) not null
);

# 班级表
create table class (
	cid int unsigned primary key auto_increment,
	caption char(10) not null,
	grade_id int unsigned not null,
	foreign key(grade_id) references class_grade(gid)
	on update cascade
	on delete cascade
);

# 学生表
create table student (
	sid int unsigned primary key auto_increment,
	sname char(8) not null,
	gender enum("男", "女") not null default "男",
	class_id int unsigned not null,
	foreign key(class_id) references class(cid)
	on update cascade
	on delete cascade
);

# 课程表
create table course (
	cid int unsigned primary key auto_increment,
	cname char(10) not null,
	teacher_id int unsigned not null,
	foreign key(teacher_id) references teacher(tid)
	on update cascade
	on delete cascade
);

# 成绩表
create table score (
	sid int unsigned primary key auto_increment,
	student_id int unsigned not null,
	course_id int unsigned not null,
	score int,
	foreign key(student_id) references student(sid)
	on update cascade
	on delete cascade,
	foreign key(course_id) references course(cid)
	on update cascade
	on delete cascade
);

# 班级任职表
create table teach2cls (
	tcid int unsigned primary key auto_increment,
	tid int unsigned not null,
	cid int unsigned not null,
	foreign key(tid) references teacher(tid)
	on update cascade
	on delete cascade,
	foreign key(cid) references class(cid)
	on update cascade
	on delete cascade
);

插入数据

插入数据自行准备,这边只提供参考

insert class_grade(gname) values 
	("一年级"),
	("二年级"),
	("三年级"),
	("四年级"),
	("五年级"),
	("六年级");

insert teacher(tname) values 
	("张三"),
	("李四"),
	("王五"),
	("李六"),
	("小明"),
	("罗翔"),
	("吴闲");

insert class(caption, grade_id) values 
	("一年一班", 1),
	("一年二班", 1),
	("二年一班", 2),
	("三年二班", 3),
	("四年一班", 4),
	("五年二班", 5),
	("六年三班", 6);

insert student(sname, gender, class_id) values 
	("乔丹", "男", 1),
	("科比", "男", 1),
	("小美", "女", 2),
	("小明", "男", 2),
	("张三", "男", 3),
	("王五", "女", 3),
	("李四", "女", 4),
	("赵六", "女", 4),
	("奥尔尼", "男", 1),
	("姚明", "男", 3),
	("麦迪", "男", 6),
	("斯科拉", "男", 1),
	("詹姆斯", "男", 2),
	("韦德", "女", 3),
	("费舍尔", "男", 1),
	("保罗", "男", 6),
	("邓肯", "男", 4),
	("吉诺比利", "女", 5),
	("罗斯", "女", 6),
	("麦迪", "男", 5);

insert course(cname, teacher_id) values 
	("生物", 1),
	("体育", 3),
	("物理", 4),
	("数学", 5),
	("语文", 2),
	("英语", 2),
	("Python", 5),
	("艺术", 1),
	("医学", 1),
	("刑法", 6),
	("人生学", 7);

insert score(student_id, course_id, score) values 
	(1, 1, 60),
	(1, 3, 59),
	(2, 2, 99),
	(2, 4, 89),
	(2, 5, 6),
	(2, 6, 60),
	(3, 3, 89),
	(1, 7, 100),
	(5, 3, 66),
	(6, 7, 55),
	(7, 5, 67),
	(8, 9, 100),
	(9, 8, 99),
	(8, 5, 69),
	(15, 10, 76),
	(10, 1, 87),
	(9, 4, 88),
	(13, 5, 78),
	(15, 7, 71),
	(17, 3, 55),
	(12, 4, 58),
	(1, 8, 96),
	(4, 4, 81),
	(16, 1, 99),
	(16, 2, 59);

insert teach2cls(tid, cid) values 
	(1, 1),
	(1, 2),
	(2, 1),
	(2, 4),
	(3, 3),
	(4, 2),
	(5, 5),
	(6, 6);

操作表

1、查询学生总人数;
select count(*) as "学生总人数" from student;
2、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
select sid, sname from student where sid in (select student_id from score where score >= 60 and course_id in (select cid from course where cname = "生物" or cname = "物理"));
3、查询每个年级的班级数,取出班级数最多的前三个年级;

先去掉严格模式

set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
select class_grade.gname, count(class.cid) as "班级数" from class join class_grade on class.grade_id = class_grade.gid group by class_grade.gname order by count(class.cid) desc, class_grade.gid limit 3;
4、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
(select student.sid, student.sname, avg(score.score) as "平均成绩" from student join score on student.sid = score.student_id group by student.sname order by avg(score.score) desc limit 1)
UNION
(select student.sid, student.sname, avg(score.score) as "平均成绩" from student join score on student.sid = score.student_id group by student.sname order by avg(score.score) limit 1);
5、查询每个年级的学生人数;
select gname, count(sname) as "年级人数" from student join class on student.class_id = class.cid join class_grade on class.grade_id = class_grade.gid group by gname;
6、查询每位学生的学号,姓名,选课数,平均成绩;
select student.sid, student.sname, count(cname) as "选课数", avg(score.score) as "平均成绩" from student join score on student.sid = score.student_id join course on course.cid = score.course_id group by student.sname;
7、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
(select student.sname, course.cname, score.score from student join score on student.sid = score.student_id join course on course.cid = score.course_id where student.sid = 2 order by score.score desc limit 1)
UNION
(select student.sname, course.cname, score.score  from student join score on student.sid = score.student_id join course on course.cid = score.course_id where student.sid = 2 order by score.score limit 1);
8、查询姓“李”的老师的个数和所带班级数;
select teacher.tname, count(teach2cls.cid) as "所带班级数" from teacher left join teach2cls on teacher.tid = teach2cls.tid where teacher.tname regexp "^李.*$" group by teacher.tname;
9、查询班级数小于5的年级id和年级名;
select class_grade.gid, class_grade.gname, count(class.caption) as "班级数" from class_grade join class on class_grade.gid = class.grade_id group by class_grade.gname having count(class.caption) < 5;
10、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果如下;
select class.cid, class.caption, class_grade.gname,
case
	when class_grade.gid between 1 and 2 then "低年级"
	when class_grade.gid between 3 and 4 then "中年级"
	when class_grade.gid between 5 and 6 then "高年级"
	else null
end as "年级级别"
 from class join class_grade on class.grade_id = class_grade.gid;
11、查询学过“张三”老师2门课以上的同学的学号、姓名;

方法一、

select student.sid, student.sname from student join score on score.student_id = student.sid join course on score.course_id = course.cid join teacher on course.teacher_id = teacher.tid where teacher.tname = "张三" group by student.sname having count(teacher.tname) >= 2;

方法二、

select sid, sname from student where sid in (select student_id from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = "张三")) group by student_id having count(student_id) >= 2);
12、查询教授课程超过2门的老师的id和姓名;
select * from teacherwhere tid in (select teacher_id from course group by teacher_id having count(teacher_id) > 2);
13、查询学过编号"1"课程和编号“2”课程的同学的学号、姓名;
select sid, sname from student where sid in (select student_id from score where course_id = 1 or course_id = 2);
14、查询没有带过高年级的老师id和姓名;
select * from teacher where tid in (select tid from teach2cls where cid in (select cid from class where grade_id not in (select gid from class_grade where gid between 5 and 6)));
15、查询学过"张三"老师所教的所有课的同学的学号、姓名;
select sid, sname from student where sid in (select student_id from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = "张三")));
16、查询带过超过2个班级的老师的id和姓名;
select * from teacherwhere tid in (select tid from teach2cls group by tid having count(cid) >= 2);
17、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名; &*&
select sid, sname from student where sid in (select score.student_id from score join (select * from score where course_id = 1) as t_1 on t_1.student_id = score.student_id and score.course_id = 2 where t_1.score > score.score);
18、查询所带班级数最多的老师id和姓名;
select * from teacher where tid in (select tid from teach2cls group by tid order by count(cid) desc) limit 1;
19、查询有课程成绩小于60分的同学的学号、姓名;
select sid, sname from student where sid in (select student_id from score where score < 60);
20、查询没有学全所有课的同学的学号、姓名;
select sid, sname from student where sid in (select student_id from score group by student_id having count(course_id) != (select count(cid) from course));
21、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
select sid, sname from student where sid in (select student_id from score where course_id in (select course_id from score where student_id = 1));
22、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;
select sid, sname from student where sid in (select student_id from score where course_id in (select course_id from score where student_id = 1) and student_id != 1);
23、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;
select sid, sname from student where sid in (select student_id from score group by student_id having count(course_id) = (select count(course_id) from score where student_id = 2) and student_id != 2);
24、删除学习“张三”老师课的score表记录;
delete from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = "张三"));
25、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课程的平均成绩;
insert score(student_id, course_id, score)
	select student_id, course_id, avg(score) from score where course_id != 2 group by student_id;
26、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,课程数和平均分; &*&
select score.student_id as "学号", c1 as "语文", c2 as "数学", c3 as "英语", count(score.course_id) as "课程数", avg(score.score) as "平均分" from score 
 	left join
 	(select course_id, student_id, score as c1 from score where course_id in (select cid from course where cname = "语文")) as t_1 
 	on score.student_id = t_1.student_id
 	left join 
 	(select course_id, student_id, score as c2 from score where course_id in (select cid from course where cname = "数学")) as t_2 
 	on score.student_id = t_2.student_id
 	left join
	(select course_id, student_id, score as c3 from score where course_id in (select cid from course where cname = "英语")) as t_3
	on score.student_id = t_3.student_id  group by score.student_id order by avg(score.score);
27、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
select course_id as "课程ID", max(score) as "最高分", min(score) as "最低分" from score group by course_id;
28、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
select course_id, avg(score) as "平均分", round(100*(case when score >=60 then 1 else 0 end)/count(sid), 2) as "及格率" from score group by course_id order by avg(score), (case when score >=60 then 1 else 0 end)/count(sid) desc;
29、课程平均分从高到低显示(现实任课老师);
select teacher.tname, course.cname, avg(score.score) as "平均分" from score join course on score.course_id = course.cid join teacheron course.teacher_id = teacher.tid group by course.cname order by avg(score.score) desc;
30、查询各科成绩前三名的记录(不考虑成绩并列情况) ; &*&
select t_1.sid, t_1.student_id, t_1.course_id, t_1.score from score as t_1 join score as t_2 on t_1.sid = t_2.sid and t_1.score <= t_2.score group by t_1.student_id, t_1.course_id having count(t_2.student_id) <= 3 order by t_1.course_id, t_1.score desc;
31、查询每门课程被选修的学生数;
select course.cname as "课程", count(sid) as "学生数" from score join course on score.course_id = course.cid group by course.cname;
32、查询选修了2门以上课程的全部学生的学号和姓名;
select sid, sname from student where sid in (select student_id from score group by student_id having count(course_id) > 2);
33、查询男生、女生的人数,按倒序排列;
select gender, count(gender) as "人数" from student group by gender order by count(gender) desc;
34、查询姓“张”的学生名单;
select * from student where sname regexp "^张";
35、查询同名同姓学生名单,并统计同名人数;
select sname, count(*) "同名人数" from student group by sname having count(*) > 1;
36、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
select course.cid, course.cname, avg(score.score) as "平均分" from course join score on course.cid = score.course_id group by course.cname order by avg(score.score), course.cid desc;
37、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
select student.sname, score.score from student join score on student.sid = score.student_id where score.course_id in (select cid from course where cname = "数学") and score.score < 60;
38、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;
select sid, sname from student where sid in (select student_id from score where course_id = 3 and score > 80);
39、求选修了课程的学生人数
select count(*) as "以选课程人数" from student where sid in (select distinct(student_id) from score);
40、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
(select student.sname, score.score from student join score on student.sid = score.student_id where score.course_id in (select cid from course where teacher_id in (select tid from teacherwhere tname = "王五")) order by score.score desc limit 1)
UNION
(select student.sname, score.score from student join score on student.sid = score.student_id where score.course_id in (select cid from course where teacher_id in (select tid from teacher where tname = "王五")) order by score.score limit 1);
41、查询各个课程及相应的选修人数;
select c.cname, count(*) as "选修人数" from score as s join course as c on s.course_id = c.cid group by c.cname;
42、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
select distinct(t_1.student_id) as "学号", t_1.course_id as "课程一", t_2.course_id as "课程二", t_1.score from score as t_1, score as t_2 where t_1.score = t_2.score and t_1.course_id != t_2.course_id order by t_1.score desc;
43、查询每门课程成绩最好的前两名学生id和姓名; &*&
select student.sid, student.sname, cid as "课程号", sc as "分数" from student join (select t1.student_id as sid,t1.score as sc,t1.course_id as cid from score as t1 where 2 > (select count(*) from score as t2 where t1.course_id = t2.course_id and t2.score > t1.score)) as tt on student.sid = tt.sid order by cid, sc desc;
44、检索至少选修两门课程的学生学号;
select student_id as "学号", count(course_id) as "课程数" from score group by student_id having count(course_id) >= 2;
45、查询没有学生选修的课程的课程号和课程名;
select cid, cname from course where cid not in (select course_id from score);

46、查询没带过任何班级的老师id和姓名;

select tid, tname from teacher where tid not in (select tid from teach2cls);
47、查询有两门以上课程超过80分的学生id及其平均成绩;
select student.sid,avg(score.score) as "平均成绩", count(score.course_id) as "课程数" from student join score on student.sid = score.student_id where score.score > 80 group by student.sname having count(score.course_id) >= 2;
48、检索“3”课程分数小于60,按分数降序排列的同学学号;
select student_id from score where course_id = 3 and score < 60 order by score desc;
49、删除编号为“2”的同学的“1”课程的成绩;
delete from score where course_id = 1 and student_id = 2;
50、查询同时选修了物理课和生物课的学生id和姓名;
select sid, sname from student where sid in (select student_id from score where course_id in (select cid from course where cname = "物理" or cname = "生物") group by student_id having count(course_id) = 2);

后记

练习题答案最好是用于参考,不要直接抄,题目先自己思考,实在做不出来再去看答案,看完后试着自己做一边,这样才会有学习的效果

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值