新建一个叫做 review 的数据库,将测试数据脚本导进去。(可以使用Navicat查询功能)

/*
Navicat MySQL Data Transfer Source Server : DB
Source Server Version : 50723
Source Host : localhost:3306
Source Database : review Target Server Type : MYSQL
Target Server Version : 50723
File Encoding : 65001 Date: 2019-02-25 23:48:25
*/ SET FOREIGN_KEY_CHECKS=0; -- ----------------------------
-- Table structure for class
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of class
-- ----------------------------
INSERT INTO `class` VALUES ('', '高三1班');
INSERT INTO `class` VALUES ('', '高三2班');
INSERT INTO `class` VALUES ('', '高三3班'); -- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(8) NOT NULL,
`teacher_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `fk_cou_tea` (`teacher_id`),
CONSTRAINT `fk_cou_tea` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('', 'python', '');
INSERT INTO `course` VALUES ('', 'java', '');
INSERT INTO `course` VALUES ('', 'php', '');
INSERT INTO `course` VALUES ('', 'c', ''); -- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) DEFAULT NULL,
`course_id` int(11) DEFAULT NULL,
`mark` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_sco_stu` (`student_id`),
KEY `fk_sco_cou` (`course_id`),
CONSTRAINT `fk_sco_cou` FOREIGN KEY (`course_id`) REFERENCES `course` (`id`),
CONSTRAINT `fk_sco_stu` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', ''); -- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(2) NOT NULL,
`gender` char(1) DEFAULT NULL,
`class_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_cou_cla` (`class_id`),
CONSTRAINT `fk_cou_cla` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('', '德玛', '男', '');
INSERT INTO `student` VALUES ('', '妖姬', '女', '');
INSERT INTO `student` VALUES ('', '盲僧', '男', '');
INSERT INTO `student` VALUES ('', '蜘蛛', '女', '');
INSERT INTO `student` VALUES ('', '卡牌', '男', '');
INSERT INTO `student` VALUES ('', '露露', '女', ''); -- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('', '佩奇');
INSERT INTO `teacher` VALUES ('', '大熊');
INSERT INTO `teacher` VALUES ('', '路飞');

测试数据脚本

数据表结构如下图:

练习题及答案如下:

-- 1、查询所有的课程的名称以及对应的任课老师姓名
select course.name as "课程",teacher.name as "任课老师" from course
left join
teacher
on
course.teacher_id=teacher.id; ------------------------------------------------------------
-- 2、查询学习课程"python"比课程"java"成绩低的学生的学号
select python.student_id from
(select score.student_id,course.name,score.mark from score inner join course on score.course_id=course.id where course.name="python") as python
inner JOIN
(select score.student_id,course.name,score.mark from score inner join course on score.course_id=course.id where course.name="java") as java
on python.student_id=java.student_id
where python.mark<java.mark; ------------------------------------------------------------
-- 3、查询平均成绩大于65分的同学的id和平均成绩(保留两位小数)
select student_id,round(avg(mark),2) as m from score
group by student_id having m>65; ------------------------------------------------------------
-- 4、查询平均成绩大于65分的同学的姓名和平均成绩(保留两位小数);
select student.name,round(avg(mark),2) as m from score
inner join
student
on
score.student_id=student.id
group by student_id having m>65; ------------------------------------------------------------
-- 5、查询所有同学的姓名、选课数、总成绩
select student.name,count(score.course_id) as "选课数",sum(score.mark) as "总成绩" from score
inner join
student
on
score.student_id=student.id
group by student_id; ------------------------------------------------------------
-- 6、查询没学过"路飞"老师课的同学的姓名
# 1)"路飞"老师任课的课程id
select course.id from course inner join teacher on course.teacher_id=teacher.id where teacher.name="路飞"
# 2)学过"路飞"老师的课的学生id
select score.student_id from score where score.course_id in
(select course.id from course inner join teacher on course.teacher_id=teacher.id where teacher.name="路飞")
group by score.student_id
# 3)最终结果
select student.name from student where id not in
(select score.student_id from score where score.course_id in
(select course.id from course inner join teacher on course.teacher_id=teacher.id where teacher.name="路飞")
group by score.student_id); ------------------------------------------------------------
-- 7、查询学过"python"并且也学过"java"课程的同学的姓名
select student.name from score
left join student on score.student_id=student.id
where
score.course_id=(select id from course where name="python")
or
score.course_id=(select id from course where name="java")
group by score.student_id having count(1)>1; ------------------------------------------------------------
-- 8、查询学过"路飞"老师所教的全部课程的同学的姓名
select student.name from score inner join student on score.student_id=student.id where score.course_id in
(select course.id from course inner join teacher on course.teacher_id=teacher.id where teacher.name="路飞")
group by score.student_id; -- 9、查询有课程成绩小于60分的同学的姓名
select name from student where id in
(select student_id from score where mark<60 group by student_id); ------------------------------------------------------------
-- 10、查询挂科超过两门(包括两门)的学生姓名
select name from student where id in
(select student_id from score where mark<60 group by student_id having count(1)>=2);
------------------------------------------------------------
-- 11、查询选修了全部课程的学生姓名
select name from student where id in
(select student_id from score group by student_id having count(1)=(select count(1) from course)); ------------------------------------------------------------
-- 12、查询至少有一门课程与"卡牌"同学所学课程相同的同学姓名
select name from student where id in
(select student_id from score where course_id in
(select course_id from score inner join student on score.student_id=student.id where student.name="卡牌")
group by student_id)
and name!="卡牌"; ------------------------------------------------------------
-- 13、查询学过"蜘蛛"同学全部课程的其他同学姓名
# 1)"蜘蛛"同学学过的课程id
select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛"
# 2)score表连student表,并筛选出课程id在("蜘蛛"同学学过的课程id),并且学生!="蜘蛛"
select student.name from score inner join student on score.student_id=student.id where score.course_id
in (select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛")
and student.name!="蜘蛛"
# 3)接着对结果进行分组,再次筛选得到最终结果
select student.name from score inner join student on score.student_id=student.id where score.course_id
in (select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛")
and student.name!="蜘蛛"
group by student_id
having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛"); ------------------------------------------------------------
-- 14、查询和"蜘蛛"同学学习的课程完全相同的其他同学姓名;
# 1)找出与"蜘蛛"学习课程数相同的学生id(你学两门,我也学两门)
select * from score where score.student_id
in
(select student_id from score
group by score.student_id
having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛"))
# 2)找出学过"蜘蛛"课程的学生,剩下的一定是至少学过一门"蜘蛛"课程的学生
select * from score where score.student_id
in
(select student_id from score
group by score.student_id
having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛"))
and
score.course_id
in
(select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛")
# 3)根据学生id进行分组,剩下学生数count(1)=貂蝉学生所学课程数
select student_id from score where score.student_id
in
(select student_id from score
group by score.student_id
having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛"))
and
score.course_id
in
(select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛")
group by
score.student_id
having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛")
and
score.student_id!=(select id from student where name="蜘蛛")
# 4)最终结果
select name from student where id in
(select student_id from score where score.student_id
in
(select student_id from score
group by score.student_id
having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛"))
and
score.course_id
in
(select course_id from score inner join student on score.student_id=student.id where student.name="蜘蛛")
group by
score.student_id
having count(1)=(select count(1) from score inner join student on score.student_id=student.id where student.name="蜘蛛")
and
score.student_id!=(select id from student where name="蜘蛛")); ------------------------------------------------------------
-- 15、按平均成绩倒序显示所有学生的"python"、"java"、"php"三门的课程成绩,按如下形式显示: 学生id,python,java,php,课程数,平均分
# 1)先查询单一学生的python课程分数
select mark from score left join course on score.course_id = course.id where course.name = "python" and score.student_id=1;
# 2)将上面查询的结果作为列字段使用,得到最终结果
select student_id,
(select mark from score left join course on score.course_id = course.id where course.name = "python" and score.student_id=sc.student_id) as "python",
(select mark from score left join course on score.course_id = course.id where course.name = "java" and score.student_id=sc.student_id) as "java",
(select mark from score left join course on score.course_id = course.id where course.name = "php" and score.student_id=sc.student_id) as "php",
count(course_id) as "课程数",
avg(mark) as "平均分"
from score as sc
group by student_id order by avg(mark) desc; ------------------------------------------------------------
-- 16、查询各科成绩最高和最低的分:以如下形式显示:课程id,最高分,最低分
select course_id,max(mark) as "最高分",min(mark) as "最低分" from score group by course_id; ------------------------------------------------------------
-- 17、统计各科各分数段人数,显示格式:课程id,课程名称,[100-85],[85-70],[70-60],[<60]
select course_id,course.name,
sum(case when mark between 85 and 100 then 1 else 0 end) as "[100-85]",
sum(case when mark between 70 and 85 then 1 else 0 end) as "[85-70]",
sum(case when mark between 60 and 70 then 1 else 0 end) as "[70-60]",
sum(case when mark < 60 then 1 else 0 end) as "[<60]"
from score
inner join course
on score.course_id=course.id
GROUP BY score.course_id; ------------------------------------------------------------
-- 18、查询每门课程名字及其被选修的次数
select course.name,count(1) from score
inner join course on score.course_id=course.id
group by course_id; ------------------------------------------------------------
-- 19、查询只选修了一门课程的学生的学号和姓名
select student_id,student.name from score
inner join student on score.student_id=student.id
group by student_id having count(course_id)=1; ------------------------------------------------------------
-- 20、查询学生表中男生、女生各有多少人
select
sum(case when gender="男" then 1 else 0 end) as "男生",
sum(case when gender="女" then 1 else 0 end) as "女生"
from student ------------------------------------------------------------
-- 21、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程id降序排列
select course.name,avg(mark) from score
inner join course on score.course_id=course.id
group by course_id
order by avg(mark) asc,course_id desc; ------------------------------------------------------------
-- 22、查询课程名称为"python"且分数低于60的学生姓名和分数
select student.name,mark from score
inner join student on score.student_id=student.id
where mark<60 and course_id=(select id from course where name="python"); ------------------------------------------------------------
-- 23、求选了课程的学生人数
# 方式一:
select count(distinct student_id) from score;
# 方式二:
select count(1) from (select count(student_id) from score group by student_id) as a;

MySQL练习题及答案(复习)的更多相关文章

  1. MySQL练习题参考答案

    MySQL练习题参考答案 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[ ...

  2. s15day12作业:MySQL练习题参考答案

    MySQL练习题参考答案   导出现有数据库数据: mysqldump -u用户名 -p密码 数据库名称 >导出文件路径           # 结构+数据 mysqldump -u用户名 -p ...

  3. python 全栈开发,Day65(MySQL练习题,参考答案)

    一.MySQL练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号.ps:针对的是自己的生物成绩比物理成绩高,再 ...

  4. Mysql 练习题 及 答案

    --1.学生表 Student(S,Sname,Sage,Ssex) --S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表  Course(C,Cname,T ...

  5. MySQL 练习题 附答案,未完

    综合练习题 表结构 整合一下方便查看 teacher  student  course scors 练习题 1.自行创建测试数据 create table student( sid int prima ...

  6. MySQL练习题及答案

    一.现有三张数据库表,分别为部门表.员工表.部门和员工关系表 1.部门表CREATE TABLE `t_dept` ( `id` int(8) NOT NULL AUTO_INCREMENT, `de ...

  7. 数据库---MySQL练习题及答案

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  8. mysql 练习题答案

    一 题目 1.查询所有的课程的名称以及对应的任课老师姓名 2.查询学生表中男女生各有多少人 3.查询物理成绩等于100的学生的姓名 4.查询平均成绩大于八十分的同学的姓名和平均成绩 5.查询所有学生的 ...

  9. MySql习题和答案

    MySQL测试题 一.表关系请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号.ps:针对的是自己的生物成绩比物理成绩高,再把符合 ...

随机推荐

  1. 10.05FZSZ Day2模拟总结

    今天的题目难度比昨天小一些,但是太菜的我还是啥也不会. 今天的出题大佬是Heaplex,他的题目中倒是出现了ZZQ,不知道是否是本人? T1.a 期望得分30,实际得分30 这道题开场发现好像有什么小 ...

  2. SYSUCPC2017 online round La La string 应用manacher算法

    manacher算法给出一个字符串中 以每个位置为对称中心的回文串长度,但是大部分时候我们只需要知道以每个位置为起点的回文串长度,感觉有点浪费. 那么来看看这个不难也不太简单的题目 第一步,我们要想办 ...

  3. bzoj2705 [SDOI2012]Longge的问题——因数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705 一开始自己想了半天... 有了点思路:遍历 n 的因数 k,每个因数要预处理出 gcd ...

  4. DTO和ENTITY的关系

    DTO是数据传输对象:主要用于封装前台页面传过来的数据,在各个层之间进行数据的传递,主要用于接受前台数据进行封装并向各个层之间传递数据(个人理解是向下层传递数据),定义方法跟Bean规范一致 ENTI ...

  5. bzoj 2730: [HNOI2012]矿场搭建【tarjan】

    先tarjan找割点和点双连通分量,然后对一个点双,如果没有割点,那么需要建立两个出口(割掉一个另一个备用):如果只有一个割点,出口可以设立在任意一个非割点的地方:如果有两个及以上个割点,就不用建出口 ...

  6. bzoj 1628: [Usaco2007 Demo]City skyline【贪心+单调栈】

    还以为是dp呢 首先默认答案是n 对于一个影子,如果前边的影子比它高则可以归进前面的影子,高处的一段单算: 和他一样高的话就不用单算了,ans--: 否则入栈 #include<iostream ...

  7. 一些常见的iOS面试问题,一眼就能看出 初级和高级工程师的区别

    前言 面试题中有一些一般性的问题,通常是会问到的.面试iOS应聘者时,切入点很重要,不同的切入点会导致不同的结果,没有找到合适的切入点也无法对应聘者有一个全面的了解. 所以下面的面试问题更多的是提供方 ...

  8. linux学习之路5 系统常用命令

    日期时间 查看设置当前时间 date +%Y--%m--%d 格式化显示时间 -s " "(切换到超级用户)修改时间 hwclock(clock)用以显示硬件时钟时间 命令 cal ...

  9. Android 性能优化(26)*性能工具之「Batterystats,Battery Historian」Batterystats & Battery Historian Walkthrough

    Batterystats & Battery Historian Walkthrough Working with Batterystats & Battery Historian B ...

  10. 转 linux之sed命令详解

    http://jingyan.baidu.com/article/fec4bce2228f60f2618d8bb0.html sed  编辑裁剪文件命令 sed -i "s/\/db\/te ...