MySQL 测试题

一、 表关系:

请创建如下表,并创建相关约束

创建表sql如下:

/*
Navicat MySQL Data Transfer Source Server : 192.168.118.14
Source Server Version : 50544
Source Host : 192.168.118.14:3306
Source Database : db3 Target Server Type : MYSQL
Target Server Version : 50544
File Encoding : 65001 Date: 2019-02-15 16:34:46
*/ SET FOREIGN_KEY_CHECKS=0; -- ----------------------------
-- Table structure for class
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`caption` varchar(32) DEFAULT NULL,
PRIMARY KEY (`cid`),
UNIQUE KEY `caption` (`caption`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of class
-- ----------------------------
INSERT INTO `class` VALUES ('', '一年二班');
INSERT INTO `class` VALUES ('', '三年三班');
INSERT INTO `class` VALUES ('', '三年二班');
INSERT INTO `class` VALUES ('', '二年九班'); -- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(32) NOT NULL,
`teacher_id` int(11) NOT NULL,
PRIMARY KEY (`cid`),
KEY `fk_course_teacher` (`teacher_id`),
CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('', '生物', '');
INSERT INTO `course` VALUES ('', '物理', '');
INSERT INTO `course` VALUES ('', '体育', '');
INSERT INTO `course` VALUES ('', '美术', ''); -- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`number` int(11) NOT NULL,
PRIMARY KEY (`sid`),
UNIQUE KEY `student_id` (`student_id`,`course_id`),
KEY `fk_score_course` (`course_id`),
CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`),
CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=66 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 ('', '', '', '');
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 ('', '', '', '');
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 ('', '', '', '');
INSERT INTO `score` VALUES ('', '', '', ''); -- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`gender` varchar(2) NOT NULL,
`class_id` int(11) NOT NULL,
`sname` varchar(32) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_student_class` (`class_id`),
CONSTRAINT `fk_student_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=18 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 ('', '男', '', '张四');
INSERT INTO `student` VALUES ('', '女', '', '铁锤');
INSERT INTO `student` VALUES ('', '男', '', '李三');
INSERT INTO `student` VALUES ('', '男', '', '李一');
INSERT INTO `student` VALUES ('', '女', '', '李二');
INSERT INTO `student` VALUES ('', '男', '', '李四');
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` (
`tid` int(11) NOT NULL AUTO_INCREMENT,
`tname` varchar(32) NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('', '张磊老师');
INSERT INTO `teacher` VALUES ('', '李平老师');
INSERT INTO `teacher` VALUES ('', '刘海燕老师');
INSERT INTO `teacher` VALUES ('', '朱云海老师');
INSERT INTO `teacher` VALUES ('', '李杰老师');

表结构及数据

二、表操作及答案

1. 查询“生物”课程比“物理”课程成绩高的所有学生的学号

select A.student_id, sw, wl from
(SELECT score.student_id, number as sw from score LEFT JOIN course on score.course_id = course.cid where course.cname = '生物') as A
LEFT JOIN
(SELECT score.student_id, number as wl from score LEFT JOIN course on score.course_id = course.cid where course.cname = '物理') as B
on A.student_id = B.student_id where sw > wl

2. 查询平均成绩大于60分的同学的学号和平均成绩

SELECT student_id, avg(number) from score GROUP BY score.student_id HAVING avg(number) > 60

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

SELECT score.student_id, student.sname, count(score.course_id), sum(number) from score
LEFT JOIN student on score.student_id = student.sid
GROUP BY score.student_id

4. 查询姓“李”的老师的个数

SELECT count(1) from teacher where teacher.tname like '李%'

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

SELECT sid, sname from student where student.sid  not in (
SELECT DISTINCT score.student_id from score where score.course_id in (
SELECT course.cid from course
LEFT JOIN teacher on course.teacher_id = teacher.tid
where teacher.tname = '李平老师'))

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

SELECT sid, sname from student where student.sid in (
SELECT score.student_id from score where score.course_id = 1 or score.course_id = 2
GROUP BY score.student_id HAVING count(1) > 1);

7. 查询学过“李平老师”老师所教的所有课的同学的学号、姓名

SELECT sid, sname from student where student.sid in (
SELECT score.student_id from score where score.course_id in
(SELECT cid from course LEFT JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师')
GROUP BY score.student_id HAVING count(1) = (SELECT count(1) from course
LEFT JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师'))

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

SELECT sid, sname from student where student.sid in (
select A.student_id from
(select student_id, number as num_2 from score where score.course_id = 2) as A
LEFT JOIN
(select student_id, number as num_1 from score where score.course_id = 1) as B
on A.student_id = B.student_id where A.num_2 < B.num_1)

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

SELECT DISTINCT student.sid, student.sname from score
LEFT JOIN student on score.student_id = student.sid where score.number < 60;

10. 查询没有学全所有课的同学的学号、姓名

SELECT sid, sname from student where sid in (
SELECT score.student_id from score GROUP BY student_id HAVING count(1) != (SELECT count(1) from course))

11. 查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名

SELECT sid, sname from student where sid in (
SELECT DISTINCT student_id from score where score.course_id in (SELECT course_id from score where student_id = 1))
and sid != 1;

12. 查询至少学过学号为“1”同学所有课的其他同学学号和姓名 *****

SELECT sid, sname from student where sid in (
SELECT student_id from score where score.course_id in (SELECT course_id from score WHERE score.student_id =1 )
GROUP BY student_id HAVING count(1) = (SELECT count(1) from score WHERE score.student_id =1))

13. 查询和“1”号的同学学习的课程完全相同的其他同学学号和姓名

SELECT student.sid, sname from score
LEFT JOIN student on score.student_id = student.sid
where score.student_id in (
SELECT score.student_id from score where score.student_id != 1 GROUP BY student_id HAVING count(1) = (SELECT count(1) from score where score.student_id = 1)
and course_id in (SELECT course_id from score where score.course_id in (SELECT course_id from score where score.student_id = 1)
GROUP BY student_id HAVING count(1) = (SELECT count(1) from score where score.student_id = 1)
))

14. 删除学习“叶平”老师课的score表记录

DELETE from score where course_id in (
SELECT course.cid from course LEFT JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师');

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

INSERT into score (student_id, course_id, number)
SELECT sid, 1, (SELECT avg(number) from score where score.course_id = 1)
from student where student.sid not in (select student_id from score where score.course_id = 1)

16. 按平均成绩从低到高 显示所有学生的“生物”、“物理”、“体育”三门的课程成绩,按如下形式显示: 学生ID,生物、物理、体育,有效课程数,有效平均分

SELECT sc.student_id,
(SELECT avg(number) from score LEFT JOIN course on score.course_id = course.cid where score.student_id = sc.student_id and course.cname = '生物') as sw,
(SELECT avg(number) from score LEFT JOIN course on score.course_id = course.cid where score.student_id = sc.student_id and course.cname = '物理') as wl,
(SELECT avg(number) from score LEFT JOIN course on score.course_id = course.cid where score.student_id = sc.student_id and course.cname = '体育') as ty,
count(sc.course_id),
avg(sc.number) as avg_n
from score sc GROUP BY sc.student_id ORDER BY avg_n asc;

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

select course_id, max(number), min(number) from score GROUP BY course_id

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

SELECT score.course_id, avg(number) as avg_n,
(sum(case when score.number > 60 then 1 else 0 end) / count(1) * 100) as percent
from score GROUP BY score.course_id ORDER BY avg_n asc, percent desc;

19. 课程平均分从高到低显示(现实任课老师)

SELECT score.course_id, avg(score.number) as avg_n, teacher.tname from score
LEFT JOIN course on score.course_id = course.cid
LEFT JOIN teacher on course.teacher_id = teacher.tid
GROUP BY score.course_id ORDER BY avg_n desc;

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

SELECT
sc.course_id,
(SELECT number from score where score.course_id = sc.course_id ORDER BY number desc limit 0,1) as first_num,
(SELECT number from score where score.course_id = sc.course_id GROUP BY number ORDER BY number desc limit 1,1) as second_num,
(SELECT number from score where score.course_id = sc.course_id GROUP BY number ORDER BY number desc limit 2,1) as third_num
from score sc GROUP BY sc.course_id

21. 查询每门课程被选修的学生数

SELECT course_id, count(student_id) from score GROUP BY course_id

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

SELECT student.sid, student.sname from score
LEFT JOIN student on score.student_id = student.sid
GROUP BY student_id HAVING count(score.course_id) =1 ;

23. 查询男生、女生的人数

SELECT * from
(SELECT count(1) as boy from student where student.gender = '男') as A,
(SELECT count(1) as girl from student where student.gender = '女') as B

24. 查询姓“张”的学生名单

SELECT * from student where student.sname like '张%'

25. 查询同名同姓学生名单,并统计同名人数

SELECT sname, count(sname) from student GROUP BY sname HAVING count(1) > 1;

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

SELECT course_id, avg(number) as avg_n from score GROUP BY course_id ORDER BY avg_n asc, course_id desc;

27. 查询平均成绩大于80的所有学生的学号、姓名和平均成绩

SELECT score.student_id, student.sname, avg(number) from score
LEFT JOIN student on score.student_id = student.sid
GROUP BY student_id HAVING avg(number) > 80;

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

SELECT * from score
LEFT JOIN student on score.student_id = student.sid
where score.number < 60 and score.course_id in (SELECT cid from course where course.cname = '生物')

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

SELECT student.sid, student.sname from score
LEFT JOIN student on score.student_id = student.sid
where score.course_id = 3 and score.number > 80;

30. 求选了课程的学生人数

SELECT count(DISTINCT student_id) from score where score.course_id is not null;

31. 查询选修 刘海燕老师 所授课程的学生中,成绩最高的学生姓名及其成绩

SELECT student.sname, max(score.number) from score
LEFT JOIN student on score.student_id = student.sid
where score.course_id in (SELECT cid from course
LEFT JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = '刘海燕老师')

32. 查询各个课程及相应的选修人数

SELECT course_id, count(1) from score GROUP BY course_id 

33. 查询不同课程但成绩相同的学生的学号、课程号、学生成绩

SELECT DISTINCT s1.course_id, s2.course_id, s1.number, s2.number from score as s1, score as s2
where s1.number = s2.number and s1.course_id != s2.course_id order by s1.course_id

34. 查询每门课程成绩最好的前两名

SELECT sc.course_id,
(SELECT number as f_n from score where course_id = sc.course_id ORDER BY number desc limit 0, 1) as first_num,
(SELECT number as f_n from score where course_id = sc.course_id GROUP BY number ORDER BY number desc limit 1, 1) as second_num
from score sc GROUP BY sc.course_id

35. 检索至少选修两门课程的学生学号

SELECT student_id, count(1) from score GROUP BY student_id HAVING count(1) > 1;

36. 查询全部学生都选修的课程的课程号和课程名

SELECT score.course_id, course.cname from score
LEFT JOIN course on score.course_id = course.cid
GROUP BY course_id HAVING count(1) = (SELECT count(1) from student)

37. 查询没学过 李平老师 讲授的任一门课程的学生姓名

SELECT sid, sname from student where sid not in (
SELECT DISTINCT student_id from score where score.course_id in (
SELECT cid from course
LEFT JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老师'))

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

SELECT sc.student_id, (SELECT avg(number) from score where score.student_id = sc.student_id) as avg_n from score sc
where sc.number < 60 GROUP BY student_id HAVING count(1) > 1;

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

SELECT student_id from score where score.course_id = 4 and score.number < 60 ORDER BY score.number desc;

40. 删除“002”同学的“001”课程的成绩

DELETE from score where score.student_id = 2 and score.course_id = 1;

MySQL 作业题及答案的更多相关文章

  1. MySQL练习题参考答案

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

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

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

  3. MySql习题和答案

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

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

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

  5. MySQL练习题及答案(复习)

    新建一个叫做 review 的数据库,将测试数据脚本导进去.(可以使用Navicat查询功能) /* Navicat MySQL Data Transfer Source Server : DB So ...

  6. Mysql 练习题 及 答案

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

  7. MySQL练习题及答案

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

  8. 阿里P8整理Mysql面试题答案,助你“脱颖而出”,吊打面试官!(建议收藏)

    前言 作为一名开发人员,每天英高都在和数据库进行着斗智斗勇,尤其是互联网行业,对MySQL的使用是比较多的.同样的,因为mysql的重要性以及普及性,在面试的时候一定是一个面试的重点或者说常问问题,说 ...

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

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

随机推荐

  1. Frequent values UVA - 11235(巧妙地RMQ)

    题意: 给出一个非降序排列的整数数组a1.a2,······,an,你的任务是对于一系列询问(i,j),回答ai,ai+1,······,aj中出现次数最多的值所出现的次数 解析: 白书p198 其实 ...

  2. Tribles UVA - 11021(全概率推论)

    题意: 有k只麻球,每只只活一天,临死之前可能会出生一些新的麻球, 具体出生i个麻球的概率为P,给定m,求m天后麻球全部死亡的概率. 解析: 从小到大,先考虑一只麻球的情况  设一只麻球m天后全部死亡 ...

  3. 【刷题】BZOJ 4199 [Noi2015]品酒大会

    Description 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发"首席品酒家"和"首席猎手&quo ...

  4. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)

    A 模拟 B 发现对于每个连通块,只有为完全图才成立,然后就dfs C 构造 想了20分钟才会,一开始想偏了,以为要利用相邻NO YES的关系再枚举,其实不难.. 考虑对于顺序枚举每一个NO/YES, ...

  5. BZOJ1563/洛谷P1912 诗人小G 【四边形不等式优化dp】

    题目链接 洛谷P1912[原题,需输出方案] BZOJ1563[无SPJ,只需输出结果] 题解 四边形不等式 什么是四边形不等式? 一个定义域在整数上的函数\(val(i,j)\),满足对\(\for ...

  6. 电子商务(电销)平台中系统设置模块(SysSetting)数据库设计明细

    以下是自己在电子商务系统设计中的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 邮件服务器 (sys_smtp_server)|-- 自动编号|-- SMTP服务器地址 (host ...

  7. 【分块】【P2801】教主的魔法

    Description 给你一个长度为 \(n\) 的序列,要求资瓷区间加,查询区间大于等于 \(k\) 的数的个数 Input 第一行是 \(n~,~Q\) 代表序列长度和操作个数 下面一行代表序列 ...

  8. Square Country

    原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1073 分析:dp,dp[i]表示钱为i且恰好用完时能买的最少土地数,易知dp[i]=mi ...

  9. 这年头不会点Git真不行!!!

    版本控制 说到版本控制,脑海里总会浮现大学毕业是写毕业论文的场景,你电脑上的毕业论文一定出现过这番景象! 1 2 3 4 5 6 7 8 9 10 11 毕业论文_初稿.doc 毕业论文_修改1.do ...

  10. Linux下UDP一发一收通信

    实现在Linux环境下的UDP通信测试. 注释了while循环,将代码规范化. udpserver.c代码: /******************************************** ...