当前位置:   article > 正文

MySQL的精选15道面试题(学会了就离入职不远了!!!)_mysql面试题

mysql面试题

目录

一,delete、drop和truncate区别 

1.delete:

2.drop:

3.truncate:

二,关于行转列的知识

三.面试题题目+详解

1.建立数据库

2.按要求建立数据表

学生表:

成绩表:

教师表:

课程表:

1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数

2.查询同时存在" 01 "课程和" 02 "课程的情况

3.查询存在" 01 “课程但可能不存在” 02 (不存在时显示为 null )

4.查询不存在" 01 “课程但存在” 02 "课程的情况

5.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

6.查询在t_mysql_score表存在成绩的学生信息

7.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

8..查询「李」姓老师的数量

9.查询学过「张三」老师授课的同学的信息

10. 查询没有学全所有课程的同学的信息 

11.查询没学过"张三"老师讲授的任一门课程的学生姓名

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

13. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息

14. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

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


前言:MySQL在面试中主要是以题目的方式出现,为此我精选了15题来进行拿捏它!!!

一,delete、drop和truncate区别 

delete、DROP和truncate,详解如下:

1.delete:

功能:delete 用于从表中删除一条或多条数据。
影响:delete 操作是 DML(数据操作语言)语句,它会触发触发器、产生回滚日志,且可以使用 WHERE 子句对要删除的数据进行筛选。
回滚:delete 操作可以通过事务回滚来撤消已删除的行。
返回值:delete 操作在执行时会返回删除的行数。


2.drop:

功能:drop 用于删除整个数据库对象(如表、视图、索引等)。
影响:drop 操作是 DDL(数据定义语言)语句,它会永久性地从数据库中删除对象。
回滚:drop 操作不可回滚,一旦执行成功,对象将被永久性删除。
返回值:drop 操作不返回任何值。


3.truncate:

功能:truncate 用于删除表中的所有数据。
影响:truncate 操作是 DDL 语句,它会将表完全清空,并且比 DELETE 操作更快,因为它不会触发触发器、不写回滚日志,并且不使用 WHERE 子句进行筛选。
回滚:truncate 操作不可回滚,一旦执行成功,数据将被永久性删除。
返回值:truncate 操作在执行时不返回删除的行数。
 

总的来说:效率问题是drop>truncate>delete

二,关于行转列的知识

行转列是一种重塑或重新组织数据的操作,将原先以行的形式存储的数据转换为以列的形式存储。行转列的技术可以使用在各种领域,如数据分析、报表生成等。

 

三.面试题题目+详解

1.建立数据库

  1. /*
  2. Navicat Premium Data Transfer
  3. Source Server : localhost
  4. Source Server Type : MySQL
  5. Source Server Version : 80018
  6. Source Host : localhost:3306
  7. Source Schema : mybatis_ssm
  8. Target Server Type : MySQL
  9. Target Server Version : 80018
  10. File Encoding : 65001
  11. Date: 04/07/2023 23:53:33
  12. */
  13. SET NAMES utf8mb4;
  14. SET FOREIGN_KEY_CHECKS = 0;
  15. -- ----------------------------
  16. -- Table structure for t_mysql_course
  17. -- ----------------------------
  18. DROP TABLE IF EXISTS `t_mysql_course`;
  19. CREATE TABLE `t_mysql_course` (
  20. `cid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '课程编号',
  21. `cname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '课程名称',
  22. `tid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '教师编号',
  23. PRIMARY KEY (`cid`) USING BTREE
  24. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '课程信息表' ROW_FORMAT = Dynamic;
  25. -- ----------------------------
  26. -- Records of t_mysql_course
  27. -- ----------------------------
  28. INSERT INTO `t_mysql_course` VALUES ('01', '语文', '02');
  29. INSERT INTO `t_mysql_course` VALUES ('02', '数学', '01');
  30. INSERT INTO `t_mysql_course` VALUES ('03', '英语', '03');
  31. -- ----------------------------
  32. -- Table structure for t_mysql_score
  33. -- ----------------------------
  34. DROP TABLE IF EXISTS `t_mysql_score`;
  35. CREATE TABLE `t_mysql_score` (
  36. `sid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生编号,外键',
  37. `cid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '课程编号,外键',
  38. `score` float NULL DEFAULT 0 COMMENT '成绩',
  39. INDEX `sid`(`sid`) USING BTREE,
  40. INDEX `cid`(`cid`) USING BTREE,
  41. CONSTRAINT `t_mysql_score_ibfk_1` FOREIGN KEY (`sid`) REFERENCES `t_mysql_student` (`sid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  42. CONSTRAINT `t_mysql_score_ibfk_2` FOREIGN KEY (`cid`) REFERENCES `t_mysql_course` (`cid`) ON DELETE RESTRICT ON UPDATE RESTRICT
  43. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '成绩信息表' ROW_FORMAT = Dynamic;
  44. -- ----------------------------
  45. -- Records of t_mysql_score
  46. -- ----------------------------
  47. INSERT INTO `t_mysql_score` VALUES ('01', '01', 80);
  48. INSERT INTO `t_mysql_score` VALUES ('01', '02', 90);
  49. INSERT INTO `t_mysql_score` VALUES ('01', '03', 99);
  50. INSERT INTO `t_mysql_score` VALUES ('02', '01', 70);
  51. INSERT INTO `t_mysql_score` VALUES ('02', '02', 60);
  52. INSERT INTO `t_mysql_score` VALUES ('02', '03', 80);
  53. INSERT INTO `t_mysql_score` VALUES ('03', '01', 80);
  54. INSERT INTO `t_mysql_score` VALUES ('03', '02', 80);
  55. INSERT INTO `t_mysql_score` VALUES ('03', '03', 80);
  56. INSERT INTO `t_mysql_score` VALUES ('04', '01', 50);
  57. INSERT INTO `t_mysql_score` VALUES ('04', '02', 30);
  58. INSERT INTO `t_mysql_score` VALUES ('04', '03', 20);
  59. INSERT INTO `t_mysql_score` VALUES ('05', '01', 76);
  60. INSERT INTO `t_mysql_score` VALUES ('05', '02', 87);
  61. INSERT INTO `t_mysql_score` VALUES ('06', '01', 31);
  62. INSERT INTO `t_mysql_score` VALUES ('06', '03', 34);
  63. INSERT INTO `t_mysql_score` VALUES ('07', '02', 89);
  64. INSERT INTO `t_mysql_score` VALUES ('07', '03', 98);
  65. -- ----------------------------
  66. -- Table structure for t_mysql_student
  67. -- ----------------------------
  68. DROP TABLE IF EXISTS `t_mysql_student`;
  69. CREATE TABLE `t_mysql_student` (
  70. `sid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生编号',
  71. `sname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生名称',
  72. `sage` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生年龄',
  73. `ssex` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生性别',
  74. PRIMARY KEY (`sid`) USING BTREE
  75. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '学生信息表' ROW_FORMAT = Dynamic;
  76. -- ----------------------------
  77. -- Records of t_mysql_student
  78. -- ----------------------------
  79. INSERT INTO `t_mysql_student` VALUES ('01', '赵雷', '1990-01-01', '男');
  80. INSERT INTO `t_mysql_student` VALUES ('02', '钱电', '1990-12-21', '男');
  81. INSERT INTO `t_mysql_student` VALUES ('03', '孙风', '1990-12-20', '男');
  82. INSERT INTO `t_mysql_student` VALUES ('04', '李云', '1990-12-06', '男');
  83. INSERT INTO `t_mysql_student` VALUES ('05', '周梅', '1991-12-01', '女');
  84. INSERT INTO `t_mysql_student` VALUES ('06', '吴兰', '1992-01-01', '女');
  85. INSERT INTO `t_mysql_student` VALUES ('07', '郑竹', '1989-01-01', '女');
  86. INSERT INTO `t_mysql_student` VALUES ('09', '张三', '2017-12-20', '女');
  87. INSERT INTO `t_mysql_student` VALUES ('10', '李四', '2017-12-25', '女');
  88. INSERT INTO `t_mysql_student` VALUES ('11', '李四', '2012-06-06', '女');
  89. INSERT INTO `t_mysql_student` VALUES ('12', '赵六', '2013-06-13', '女');
  90. INSERT INTO `t_mysql_student` VALUES ('13', '孙七', '2014-06-01', '女');
  91. -- ----------------------------
  92. -- Table structure for t_mysql_teacher
  93. -- ----------------------------
  94. DROP TABLE IF EXISTS `t_mysql_teacher`;
  95. CREATE TABLE `t_mysql_teacher` (
  96. `tid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '教师编号',
  97. `tname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '教师名称',
  98. PRIMARY KEY (`tid`) USING BTREE
  99. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '教师信息表' ROW_FORMAT = Dynamic;
  100. -- ----------------------------
  101. -- Records of t_mysql_teacher
  102. -- ----------------------------
  103. INSERT INTO `t_mysql_teacher` VALUES ('01', '张三');
  104. INSERT INTO `t_mysql_teacher` VALUES ('02', '李四');
  105. INSERT INTO `t_mysql_teacher` VALUES ('03', '王五');
  106. SET FOREIGN_KEY_CHECKS = 1;

2.按要求建立数据表

-- 1.学生表-t_mysql_student
-- sid 学生编号,sname 学生姓名,sage 学生年龄,ssex 学生性别

-- 2.教师表-t_mysql_teacher
-- tid 教师编号,tname 教师名称

-- 3.课程表-t_mysql_course
-- cid 课程编号,cname 课程名称,tid 教师名称

-- 4.成绩表-t_mysql_score
-- sid 学生编号,cid 课程编号,score 成绩
 

学生表:

  1. CREATE TABLE `t_mysql_student` (
  2. `sid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生编号',
  3. `sname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生名称',
  4. `sage` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生年龄',
  5. `ssex` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生性别',
  6. PRIMARY KEY (`sid`) USING BTREE
  7. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '学生信息表' ROW_FORMAT = Dynamic;
  8. insert into t_mysql_student values('01' , '赵雷' , '1990-01-01' , '男');
  9. insert into t_mysql_student values('02' , '钱电' , '1990-12-21' , '男');
  10. insert into t_mysql_student values('03' , '孙风' , '1990-12-20' , '男');
  11. insert into t_mysql_student values('04' , '李云' , '1990-12-06' , '男');
  12. insert into t_mysql_student values('05' , '周梅' , '1991-12-01' , '女');
  13. insert into t_mysql_student values('06' , '吴兰' , '1992-01-01' , '女');
  14. insert into t_mysql_student values('07' , '郑竹' , '1989-01-01' , '女');
  15. insert into t_mysql_student values('09' , '张三' , '2017-12-20' , '女');
  16. insert into t_mysql_student values('10' , '李四' , '2017-12-25' , '女');
  17. insert into t_mysql_student values('11' , '李四' , '2012-06-06' , '女');
  18. insert into t_mysql_student values('12' , '赵六' , '2013-06-13' , '女');
  19. insert into t_mysql_student values('13' , '孙七' , '2014-06-01' , '女');

成绩表:

  1. CREATE TABLE `t_mysql_score` (
  2. `sid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生编号,外键',
  3. `cid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '课程编号,外键',
  4. `score` float NULL DEFAULT 0 COMMENT '成绩',
  5. INDEX `sid`(`sid`) USING BTREE,
  6. INDEX `cid`(`cid`) USING BTREE,
  7. CONSTRAINT `t_mysql_score_ibfk_1` FOREIGN KEY (`sid`) REFERENCES `t_mysql_student` (`sid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  8. CONSTRAINT `t_mysql_score_ibfk_2` FOREIGN KEY (`cid`) REFERENCES `t_mysql_course` (`cid`) ON DELETE RESTRICT ON UPDATE RESTRICT
  9. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '成绩信息表' ROW_FORMAT = Dynamic;
  10. -- ----------------------------
  11. -- Records of t_mysql_score
  12. -- ----------------------------
  13. INSERT INTO `t_mysql_score` VALUES ('01', '01', 80);
  14. INSERT INTO `t_mysql_score` VALUES ('01', '02', 90);
  15. INSERT INTO `t_mysql_score` VALUES ('01', '03', 99);
  16. INSERT INTO `t_mysql_score` VALUES ('02', '01', 70);
  17. INSERT INTO `t_mysql_score` VALUES ('02', '02', 60);
  18. INSERT INTO `t_mysql_score` VALUES ('02', '03', 80);
  19. INSERT INTO `t_mysql_score` VALUES ('03', '01', 80);
  20. INSERT INTO `t_mysql_score` VALUES ('03', '02', 80);
  21. INSERT INTO `t_mysql_score` VALUES ('03', '03', 80);
  22. INSERT INTO `t_mysql_score` VALUES ('04', '01', 50);
  23. INSERT INTO `t_mysql_score` VALUES ('04', '02', 30);
  24. INSERT INTO `t_mysql_score` VALUES ('04', '03', 20);
  25. INSERT INTO `t_mysql_score` VALUES ('05', '01', 76);
  26. INSERT INTO `t_mysql_score` VALUES ('05', '02', 87);
  27. INSERT INTO `t_mysql_score` VALUES ('06', '01', 31);
  28. INSERT INTO `t_mysql_score` VALUES ('06', '03', 34);
  29. INSERT INTO `t_mysql_score` VALUES ('07', '02', 89);
  30. INSERT INTO `t_mysql_score` VALUES ('07', '03', 98);

教师表:

  1. CREATE TABLE `t_mysql_teacher` (
  2. `tid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '教师编号',
  3. `tname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '教师名称',
  4. PRIMARY KEY (`tid`) USING BTREE
  5. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '教师信息表' ROW_FORMAT = Dynamic;
  6. -- ----------------------------
  7. -- Records of t_mysql_teacher
  8. -- ----------------------------
  9. INSERT INTO `t_mysql_teacher` VALUES ('01', '张三');
  10. INSERT INTO `t_mysql_teacher` VALUES ('02', '李四');
  11. INSERT INTO `t_mysql_teacher` VALUES ('03', '王五');

课程表:

  1. CREATE TABLE `t_mysql_course` (
  2. `cid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '课程编号',
  3. `cname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '课程名称',
  4. `tid` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '教师编号',
  5. PRIMARY KEY (`cid`) USING BTREE
  6. ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '课程信息表' ROW_FORMAT = Dynamic;
  7. -- ----------------------------
  8. -- Records of t_mysql_course
  9. -- ----------------------------
  10. INSERT INTO `t_mysql_course` VALUES ('01', '语文', '02');
  11. INSERT INTO `t_mysql_course` VALUES ('02', '数学', '01');
  12. INSERT INTO `t_mysql_course` VALUES ('03', '英语', '03');

1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数

分析:分别获取01课程(t1)和02的课程(t2)的成绩表的信息

运行结果:

 

代码:

  1. SELECT
  2. st.*,
  3. s1.score '01课程',
  4. s2.score '02课程'
  5. FROM
  6. ( SELECT * FROM t_mysql_score WHERE cid = "01" ) s1,
  7. ( SELECT * FROM t_mysql_score WHERE cid = "02" ) s2,
  8. t_mysql_student st
  9. WHERE
  10. s1.sid = s2.sid
  11. AND s1.sid = st.sid
  12. AND s1.score > s2.score

2.查询同时存在" 01 "课程和" 02 "课程的情况

分析:获取01课程(t1)和02的课程(t2)的成绩表的信息

运行结果:

 

代码:

  1. SELECT
  2. t3.* ,
  3. (CASE WHEN t1.cid='01' THEN t1.score END) 语文,
  4. (CASE WHEN t1.cid='01' THEN t1.score END) 数学
  5. FROM
  6. ( SELECT * FROM t_mysql_score sc WHERE sc.cid = '01' ) t1,-- 有01的sid'
  7. ( SELECT * FROM t_mysql_score sc WHERE sc.cid = '02' ) t2, -- 有02的sid'
  8. t_mysql_student t3
  9. WHERE
  10. t1.sid = t2.sid
  11. AND t1.sid = t3.sid

3.查询存在" 01 “课程但可能不存在” 02 (不存在时显示为 null )

分析:先得使用左外连接把01课程保存,在将有02课的保存下来。

运行结果:

 

代码:

  1. SELECT
  2. t1.*,
  3. t2.cid 数学 ,
  4. t2.score 02成绩
  5. FROM
  6. ( SELECT * FROM t_mysql_score WHERE cid = '01' ) t1
  7. LEFT JOIN ( SELECT * FROM t_mysql_score WHERE cid = '02' ) t2 ON t1.sid = t2.sid

4.查询不存在" 01 “课程但存在” 02 "课程的情况

分析:先获取到01课程的学生的sdi,再查询02课程的学生信息,使用not in并且不包含01课程的sid

运行结果:

 

代码:

  1. SELECT
  2. *
  3. FROM
  4. t_mysql_score
  5. WHERE
  6. sid NOT IN ( SELECT sid FROM t_mysql_score WHERE cid = '01' )
  7. AND cid = '02'

5.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

分析:计算各个学生的平均成绩,即AVG(sc.score),在筛选出大于等于 60 分的同学,即HAVING AVG(sc.score)>=60

运行结果:

 

代码:

  1. SELECT
  2. t3.sid,
  3. t3.sname,
  4. ROUND( AVG( t1.score ), 2 )
  5. FROM
  6. ( SELECT * FROM t_mysql_score sc ) t1,
  7. t_mysql_student t3
  8. WHERE
  9. t1.sid = t3.sid
  10. GROUP BY
  11. t3.sid,
  12. t3.sname

6.查询在t_mysql_score表存在成绩的学生信息

分析:根据成绩表中有sid查询学生表sid

运行结果:

 

代码:

  1. SELECT
  2. t3.*
  3. FROM
  4. ( SELECT * FROM t_mysql_score sc) t1,
  5. t_mysql_student t3
  6. where t1.sid = t3.sid
  7. GROUP BY t3.sid ,t3.sname

7.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

分析:通过左外连接连接学生表,成绩表,再将连表查询后的作为一个新表来查询,再根据sid、sname分组(GROUP BY t1.sid,t1.sname)

运行结果:

 

代码:

  1. SELECT
  2. t1.sid,
  3. t1.sname,
  4. COUNT( t2.cid ),
  5. sum( t2.score )
  6. FROM
  7. ( SELECT * FROM t_mysql_student ) t1
  8. LEFT JOIN ( SELECT * FROM t_mysql_score ) t2 ON t1.sid = t2.sid
  9. GROUP BY
  10. t1.sid,
  11. t1.sname

8..查询「李」姓老师的数量

分析:查询t_mysql_teacher表,筛选name姓李的老师,即(WHERE tname like ‘李%’)使用聚合count(*)

运行结果:

代码:

  1. SELECT * FROM t_mysql_teacher sc WHERE sc.tname like '李%';

9.查询学过「张三」老师授课的同学的信息

分析:先根据「张三」老师的编号获取到教的相应课程,即(t.tid=c.tid)再通过课程的编号获取对应课程的成绩信息,即(c.cid=sc.cid) 再通过对应课程的成绩的学生编号获取对应学生的信息

运行结果:

代码:

  1. SELECT
  2. s.*,c.cid,c.cname,t.tname
  3. FROM
  4. t_mysql_teacher t,
  5. t_mysql_course c,
  6. t_mysql_student s,
  7. t_mysql_score sc
  8. WHERE
  9. t.tid = c.tid AND
  10. c.cid = sc.cid AND
  11. sc.sid = s.sid AND
  12. t.tname = '张三'
  13. GROUP BY
  14. s.sid,
  15. s.sname,c.cid,c.cname,t.tname

10. 查询没有学全所有课程的同学的信息 

 分析:根据学生姓名和学生编号分组(s.sid、s.sname),再筛选出选课总数少于课程总数的学生,即(HAVING COUNT( sc.score ) < ( SELECT count( 1 ) FROM t_mysql_course ))

运行结果:

 

代码:

  1. SELECT
  2. s.sid,
  3. s.sname,
  4. COUNT( sc.score )
  5. FROM
  6. t_mysql_score sc,
  7. t_mysql_student s
  8. WHERE
  9. sc.sid = s.sid
  10. GROUP BY
  11. s.sid,
  12. s.sname
  13. HAVING
  14. COUNT( sc.score ) < ( SELECT count( 1 ) FROM t_mysql_course )

11.查询没学过"张三"老师讲授的任一门课程的学生姓名

分析:先根据张三老师的编号获取到所教课程的编号,再根据所教课程的课程编号在成绩表中获取到所教学生编号,在学生信息表中筛选出不是张三老师的学生

运行结果:

 

代码:

  1. SELECT
  2. s.sname
  3. FROM
  4. t_mysql_student s
  5. WHERE
  6. sid NOT IN (
  7. SELECT
  8. sc.sid
  9. FROM
  10. t_mysql_score sc,
  11. t_mysql_teacher t,
  12. t_mysql_course c
  13. WHERE
  14. t.tid = c.tid
  15. AND c.cid = sc.cid
  16. AND t.tname = '张三'
  17. GROUP BY
  18. sc.sid
  19. )

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

分析:先筛选出各学生不及格的记录,再分组得到存在科目不及格的学生对应科目数,最后得出不及格课程数大于等于二的学生信息即平均成绩

运行结果:

 

代码:

  1. SELECT
  2. s.sid,s.sname,count(sc.score) n,ROUND(AVG(sc.score),2)
  3. FROM
  4. t_mysql_score sc,
  5. t_mysql_student s
  6. WHERE
  7. sc.sid=s.sid
  8. and sc.score<60
  9. GROUP BY
  10. s.sid,s.sname
  11. HAVING
  12. n>=2

13. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息

分析:先查询出01课程成绩小于60分的学生信息,再通过desc进行降序排序

运行结果:

 

代码:

  1. SELECT
  2. s.*,sc.score
  3. FROM
  4. t_mysql_score sc,
  5. t_mysql_student s
  6. WHERE
  7. sc.sid=s.sid
  8. and sc.score<60
  9. and sc.cid='01'
  10. ORDER BY
  11. sc.score desc

14. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

分析:计算出所有学生的平均成绩和查询出所有学生所有课程的成绩,通过平均成绩从高到低排序

运行结果:

 

代码:

  1. SELECT
  2. s.sid,s.sname,ROUND(AVG(sc.score),2) avgNum,
  3. max( CASE WHEN sc.cid = '01' THEN sc.score END ) 语文,
  4. max( CASE WHEN sc.cid = '02' THEN sc.score END ) 数学,
  5. max( CASE WHEN sc.cid = '03' THEN sc.score END ) 英语
  6. FROM
  7. t_mysql_score sc,
  8. t_mysql_student s
  9. WHERE
  10. s.sid=sc.sid
  11. GROUP BY
  12. s.sid,s.sname
  13. ORDER BY avgNum desc

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

分析:使用课程表成绩表,按分数进行分组,算及格人数,中等人数,优良人数,优秀人数

运行结果:

 

代码:

  1. SELECT
  2. c.cid,c.cname,MAX(sc.score) 最高分,min(sc.score) 最低分,ROUND(avg(sc.score),2) 平均分,
  3. ROUND(sum(if(sc.score >=60,1,0))/count(sc.score)*100,2) 及格率,
  4. ROUND(sum(if(sc.score >=70 and sc.score<80,1,0))/count(sc.score)*100,2) 中等率,
  5. ROUND(sum(if(sc.score >=80 and sc.score<90,1,0))/count(sc.score)*100,2) 优良率,
  6. ROUND(sum(if(sc.score >=90 and sc.score<=100,1,0))/count(sc.score)*100,2) 优秀率
  7. from
  8. t_mysql_score sc,
  9. t_mysql_course c,
  10. t_mysql_student s
  11. WHERE
  12. sc.cid=c.cid and sc.sid=s.sid
  13. GROUP BY c.cid,c.cname

希望这篇博客能够帮助到正在学习工作的你!!!

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号