MySQL练习题(共41题)

数据库表设计

在这里插入图片描述

建库SQL脚本

create database If Not Exists MyTestDB Character Set gbk;
use MyTestDB;
create table class(
class_id int not null auto_increment primary key, 
caption varchar(32) not null)engine=innodb default charset=gbk;
create table student(
    student_id int not null auto_increment primary key,
    student_name varchar(32) not null,
    gender varchar(8) not null,
    class_id int not null)engine=innodb default charset=gbk;
create table teacher(
     teacher_id int not null auto_increment primary key,
     teacher_name varchar(32) not null)engine=innodb default charset=gbk;
create table course(
    course_id int not null auto_increment primary key,
    course_name varchar(16) not null,
    teacher_id int not null)engine=innodb default charset=gbk;
create table score(
    score_id int not null auto_increment primary key,
    student_id int not null,
    course_id int not null,
    number int not null)engine=innodb default charset=gbk;
SET NAMES gbk;
INSERT INTO class VALUES 
(1,'一年一班'),(2,'一年二班'),(3,'三年一班'),(4,'三年二班'),(5,'二年一班'),(6,'二年二班');

INSERT INTO teacher VALUES 
(1,'叶平'),(2,'李克军'),(3,'李佳'),(4,'张东升'),(5,'王海波'),(6,'李洛克'),(7,'刘培根');

INSERT INTO course VALUES 
(1,'生物',1),(2,'体育',1),(3,'物理',2),(4,'化学',3),(5,'语文',5),(6,'数学',5),(7,'英语',6),(8,'政治',4),(9,'历史',7);

INSERT INTO student VALUES 
(1,'张惠妹','女',1),(2,'吴尊','男',2),(3,'汪东城','男',3),(4,'吴奇隆','男',4),
(5,'林依晨','女',5),(6,'杨丞琳','女',6),(7,'徐熙媛','女',1),(8,'张信哲','男',1),
(9,'徐熙娣','女',2),(10,'苏有朋','男',2),(11,'陈小春','男',2),(12,'佟大为','男',3),
(13,'胡歌','男',3),(14,'张杰','男',6),(15,'李冰冰','女',4),(16,'赵薇','女',5),
(17,'安以轩','女',2),(18,'郭富城','男',6),(19,'周杰伦','男',4),(20,'张柏芝','女',2),
(21,'刘德华','男',1),(22,'范玮琪','女',1),(23,'黎明','男',3),(24,'唐嫣','女',6),
(25,'张学友','男',5),(26,'杨幂','女',1),(27,'霍建华','男',4),(28,'刘亦菲','女',5),
(29,'张卫健','男',6),(30,'蔡依林','女',3),(31,'陆毅','男',2),(32,'舒淇','女',5),
(33,'林宥嘉','男',4),(34,'黄奕','女',6),(35,'马伊琍','女',4),(36,'张家辉','男',5),
(37,'李小璐','女',3),(38,'任泉','男',2),(39,'王菲','女',1),(40,'黄晓明','男',1),
(41,'郑爽','女',5),(42,'何润东','男',6),(43,'张一山','男',1),(44,'谢娜','女',1),
(45,'周润发','男',5),(46,'容祖儿','女',1),(47,'王力宏','男',5),(48,'黄圣依','女',1),
(49,'张雨绮','女',1),(50,'古巨基','男',4),(51,'王心凌','女',1),(52,'谢霆锋','男',3),
(53,'林志玲','女',1),(54,'张震岳','男',2),(55,'孙俪','女',1),(56,'张国荣','男',4),
(57,'李宇春','女',5),(58,'张翰','男',6),(59,'张敬轩','男',1),(60,'周笔畅','女',2),
(61,'林志颖','男',3),(62,'梁咏琪','女',4),(63,'邓超','男',5),(64,'张艺谋','男',6),
(65,'刘若英','女',1),(66,'梁朝伟','男',2),(67,'张韶涵','女',3),(68,'柳岩','女',4),
(69,'陈奕迅','男',5),(70,'刘嘉玲','女',6);

INSERT INTO score VALUES 
(1,1,1,78),(2,1,2,86),(3,1,3,82),(4,1,4,88),(5,39,5,86),(6,60,6,86),(7,61,7,93),(8,52,8,98),(9,1,9,48),
(10,2,1,85),(11,3,2,85),(12,3,3,76),(13,29,4,84),(14,37,5,59),(15,59,6,58),(16,62,7,96),(17,50,8,95),(18,5,9,69),
(19,3,1,84),(20,7,2,81),(21,5,3,98),(22,27,4,82),(23,35,5,67),(24,58,6,86),(25,63,7,85),(26,70,8,85),(27,7,9,67),(28,4,1,79),
(29,8,2,73),(30,7,3,96),(31,25,4,87),(32,33,5,48),(33,57,6,67),(34,64,7,86),(35,60,8,83),(36,9,9,66),(37,5,1,49),(38,20,2,35),
(39,9,3,83),(40,23,4,85),(41,31,5,69),(42,56,6,60),(43,64,7,80),(44,62,8,79),(45,11,9,78),(46,6,1,93),(47,29,2,95),(48,11,3,66),
(49,21,4,83),(50,68,5,76),(51,55,6,67),(52,66,7,84),(53,64,8,75),(54,13,9,79),(55,7,1,94),(56,24,2,91),(57,13,3,76),(58,68,4,81),
(59,66,5,66),(60,54,6,97),(61,67,7,80),(62,68,8,67),(63,15,9,70),(64,8,1,89),(65,30,2,50),(66,15,3,95),(67,67,4,79),(68,64,5,34),
(69,53,6,94),(70,68,7,36),(71,18,8,65),(72,17,9,58),(73,9,1,76),(74,32,2,73),(75,17,3,85),(76,65,4,77),(77,62,5,80),(78,52,6,62),
(79,69,7,76),(80,15,8,60),(81,19,9,59),(82,13,1,59),(83,10,2,78),(84,18,3,80),(85,63,4,77),(86,60,5,82),(87,51,6,79),(88,70,7,74),
(89,12,8,78),(90,21,9,50),(91,11,1,85),(92,11,2,70),(93,31,3,94),(94,62,4,75),(95,50,5,85),(96,2,6,93),(97,31,7,62),(98,10,8,80),
(99,23,9,53),(100,19,1,81),(101,12,2,75),(102,33,3,80),(103,61,4,60),(104,49,5,87),(105,4,6,92),(106,33,7,46),(107,1,8,90),
(108,25,9,45),(109,18,1,80),(110,13,2,83),(111,35,3,85),(112,59,4,90),(113,48,5,76),(114,6,6,92),(115,35,7,66),(116,3,8,70),
(117,27,9,90),(118,14,1,83),(119,14,2,59),(120,37,3,97),(121,58,4,92),(122,47,5,79),(123,8,6,90),(124,37,7,65),(125,5,8,84),
(126,29,9,98),(127,36,1,86),(128,15,2,97),(129,38,3,66),(130,56,4,93),(131,46,5,49),(132,10,6,94),(133,39,7,56),(134,7,8,87),
(135,31,9,90),(136,49,1,54),(137,16,2,96),(138,39,3,79),(139,54,4,55),(140,45,5,94),(141,12,6,95),(142,40,7,69),(143,9,8,81),
(144,33,9,55),(145,62,1,79),(146,17,2,85),(147,41,3,69),(148,52,4,94),(149,44,5,56),(150,14,6,96),(151,41,7,73),(152,54,8,71),
(153,35,9,96),(154,36,1,75),(155,18,2,81),(156,42,3,68),(157,50,4,69),(158,43,5,95),(159,16,6,78),(160,42,7,74),(161,55,8,62),
(162,37,9,88),(163,47,1,74),(164,19,2,49),(165,59,3,89),(166,48,4,65),(167,42,5,73),(168,18,6,77),(169,43,7,73),(170,57,8,76),
(171,39,9,77),(172,26,1,94),(173,37,2,65),(174,20,3,64),(175,46,4,76),(176,41,5,80),(177,20,6,79),(178,44,7,70),(179,36,8,68),
(180,41,9,70),(181,28,1,51),(182,35,2,69),(183,21,3,80),(184,42,4,86),(185,40,5,90),(186,22,6,76),(187,45,7,86),(188,34,8,79),
(189,43,9,49),(190,25,1,91),(191,34,2,80),(192,22,3,43),(193,39,4,98),(194,2,5,98),(195,24,6,81),(196,47,7,85),(197,31,8,56),
(198,45,9,74),(199,46,1,96),(200,46,2,87),(201,23,3,84),(202,38,4,67),(203,4,5,90),(204,26,6,51),(205,22,7,80),(206,29,8,46),(207,47,9,76),
(208,49,1,67),(209,49,2,96),(210,24,3,93),(211,37,4,67),(212,6,5,66),(213,28,6,84),(214,20,7,84),(215,27,8,49),(216,49,9,87),(217,63,1,68),
(218,56,2,90),(219,25,3,94),(220,36,4,66),(221,8,5,69),(222,30,6,40),(223,18,7,79),(224,24,8,55),(225,51,9,83),(226,70,1,90),(227,59,2,76),
(228,26,3,76),(229,35,4,64),(230,10,5,73),(231,32,6,83),(232,17,7,73),(2333,22,8,49),(234,53,9,84),(235,56,1,44),(236,68,2,79),(237,27,3,83),
(238,34,4,86),(239,12,5,74),(240,34,6,35),(241,13,7,56),(242,42,8,48),(243,55,9,80),(244,40,1,82),(245,67,2,83),(246,28,3,73),(247,33,4,89),
(248,14,5,89),(249,36,6,87),(250,9,7,52),(251,14,8,76),(252,57,9,83),(253,30,1,65),(254,61,2,86),(255,29,3,75),(256,32,4,85),(257,21,5,85),
(258,38,6,49),(259,5,7,49),(260,13,8,69),(261,59,9,89),(262,32,1,68),(263,62,2,84),(264,30,3,77),(265,31,4,83),(266,19,5,96),(267,40,6,87),
(268,4,7,58),(269,40,8,52),(270,61,9,66);

练习题及答案 (有空再来更新~)

1、使用数据库。
	use MyTestDB;
	show tables;
	select * from class;
	select * from course;
	select * from score;
	select * from student;
	select * from teacher;
2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
	-- 根据student_id连接
	select * from student left join score on student.student_id = score.student_id;
	-- 根据course_id连接,筛选生物和物理
	select * from score left join course on score.course_id=course.course_id where course_name='生物';
	select * from score left join course on score.course_id=course.course_id where course_name='物理';
	-- 查询“生物”课程比“物理”课程成绩高
	select sw.student_id,sw.number,wl.number from 
	(select sco.student_id,sco.number,co.course_name from score sco left join course co on sco.course_id=co.course_id where course_name='生物') as sw 
	left join 
	(select sco.student_id,sco.number,co.course_name from score sco left join course co on sco.course_id=co.course_id where course_name='物理') as wl 
	on sw.student_id = wl.student_id where sw.number>wl.number;
3、查询平均成绩大于60分的同学的学号和平均成绩;
	-- 按student_id进行分组
	select student_id,avg(number) from score group by student_id order by student_id;
	-- 大于60分
	select student_id,avg(number)  from score group by student_id having avg(number)>60 order by student_id;
4、查询所有同学的学号、姓名、选课数、总成绩;
	-- 查询学号、姓名
	select student_id,student_name from student;
	-- 查询选课数、总成绩
	select student_id,count(student_id),sum(number) from score group by student_id;
	-- 表关联
	select stu.student_id,stu.student_name,s.countCourse,s.sumNum from student as stu left join (select student_id,count(student_id) countCourse,sum(number) sumNum from score group by student_id) as s on stu.student_id=s.student_id;
5、查询姓“李”的老师的个数;
	select count(*) from teacher where teacher_name like '李%';
6、查询没学过“叶平”老师课的同学的学号、姓名;
	-- 在teacher表查出“叶平”老师
	select teacher_id,teacher_name from teacher where teacher_name='叶平';
	-- 在course表查出"叶平"老师教的科目
	select c.course_id,c.course_name,t1.teacher_name from course c, (select teacher_id,teacher_name from teacher where teacher_name='叶平')t1 where t1.teacher_id=c.teacher_id;
	-- 在score表查出课程id是叶平老师教的学生
	select student.student_id,student_name from student where student.student_id not in 
	(select s.student_id 
	from score s inner join course c on s.course_id=c.course_id 
	inner join teacher t on c.teacher_id = t.teacher_id where t.teacher_name='叶平' );
7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
	-- 查询学过001的学生
	select student_id from score where course_id=001;
	-- 查询学过002的学生
	select student_id from score where course_id=002;
	-- 查询同时学过001和002的学生id
	select c1.student_id from (select student_id from score where course_id=001) c1,(select student_id from score where course_id=002) c2 where c1.student_id=c2.student_id;
	-- 查询学号姓名
	select stu.student_id,stu.student_name from 
	(select c1.student_id from (select student_id from score where course_id=001) c1,(select student_id from score where course_id=002) c2 where c1.student_id=c2.student_id) res , student stu
	where res.student_id=stu.student_id;
8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
	-- 参考第6题
	select student.student_id,student_name from student where student.student_id in 
	(select s.student_id 
	from score s inner join course c on s.course_id=c.course_id 
	inner join teacher t on c.teacher_id = t.teacher_id where t.teacher_name='叶平' );
9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
	--查询002成绩学同学
	select student_id,course_id,number from score where course_id=002;
	--查询001成绩学同学
	select student_id,course_id,number from score where course_id=001;
	--查询002比001成绩低的同学id
	select c2.student_id,c2.course_id,c2.number 002num,c1.course_id,c1.number 001num from 
	(select student_id,course_id,number from score where course_id=002) c2 
	inner join(select student_id,course_id,number from score where course_id=001) c1 
	where c2.number<c1.number;
10、查询有课程成绩小于60分的同学的学号、姓名;
	-- 查询分数小于60分的学号
	select student_id, number from score where number<60;
	-- 查询学号和姓名
	select stu.student_id,stu.student_name from student stu where stu.student_id in 
	(select sco.student_id from score sco where number < 60 and stu.student_id=sco.student_id);
11、查询没有学全所有课的同学的学号、姓名;
	-- 查询所有课程的总数
	select count(*)from course;
	-- 查询每个学生学习课程的总数
	select student_id,count(*) from score group by student_id;
	-- 查询没有学全所有课的同学的学号、姓名
	select student_id,student_name from student stu 
	where (select count(*) from score sco where stu.student_id=sco.student_id)< (select count(*)from course);
12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
	-- 查询学号"001"同学学习的课程号
	select course_id from score where student_id=1;
	-- 查询同学的学号和姓名
	select student_id,student_name from student;
	-- 查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名
	select student_id,student_name from student where student_id in 
	(select distinct student_id from score where course_id not in 
	(select course_id from score where student_id=1));
13、查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名;
-- 与12题一样??
14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;
	-- 查询002号同学学习的课程
	select count(course_id) from score where student_id=2;
	-- 查询和002号学生选课个数相同的学生id
	select student_id,count(student_id) from score group by student_id having count(student_id)=(select count(1) from score where student_id=2);
	-- 查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名
	select sco.student_id,stu.student_name from score sco left join student stu on sco.student_id=stu.student_id where sco.student_id in 
	(select student_id from score group by student_id having count(student_id)=(select count(1) as a from score where student_id=2))
	and 
	course_id in (select course_id from score where student_id=2) group by 
	student_id having count(course_id)=(select count(1) as a from score where student_id=2);
15、删除学习“叶平”老师课的SC表记录;
	-- 查询"叶平"老师所教的课的课程编号
	select course.course_id,teacher.teacher_name from course left join teacher on course.teacher_id=teacher.teacher_id where teacher.teacher_name="叶平";
	-- 删除学习“叶平”老师课的成绩score表记录;
	delete from score where course_id in 
	(select course_id from course left join teacher on course.teacher_id=teacher.teacher_id where teacher.teacher_name="叶平");
	-- Query OK, 60 rows affected (0.07 sec)
16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;
17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

– 思路:通过课程id来进行分组,这个时候会显示四行,然后用聚合函数max,min来找出最大值和最小值。

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

– 新知识点:case when then相当于if判断

20、课程平均分从高到低显示(现实任课老师);
21、查询各科成绩前三名的记录:(不考虑成绩并列情况)?
22、查询每门课程被选修的学生数;
23、查询出只选修了一门课程的全部学生的学号和姓名;
24、查询男生、女生的人数;
25、查询姓“张”的学生名单;
26、查询同名同姓学生名单,并统计同名人数;
27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
29、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
31、求选了课程的学生人数
32、查询选修“杨艳”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
33、查询各个课程及相应的选修人数;
34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
35、查询每门课程成绩最好的前两名;
36、检索至少选修两门课程的学生学号;
37、查询全部学生都选修的课程的课程号和课程名;
38、查询没学过“叶平”老师讲授的任一门课程的学生姓名;
39、查询两门以上不及格课程的同学的学号及其平均成绩;
40、检索“004”课程分数小于60,按分数降序排列的同学学号;
41、删除“002”同学的“001”课程的成绩;
发布了27 篇原创文章 · 获赞 0 · 访问量 2032

猜你喜欢

转载自blog.csdn.net/qq_32834005/article/details/103410887
今日推荐