当前位置:   article > 正文

MySQL中 DQL语句实训_查询参加计算机基础课程考试的学生成绩高于课程平均分的学生信息的查询代码

查询参加计算机基础课程考试的学生成绩高于课程平均分的学生信息的查询代码

学习目标

        创建xscjgl数据库,对xscjgl数据库中的xs,kc,xs_kc表进行查询。

背景铺设:创建数据库并填入数据

  1. create database xscjgl;
  2. use xscjgl;
  3. /*
  4. Navicat Premium Data Transfer
  5. Source Server : phonesy
  6. Source Server Type : MySQL
  7. Source Server Version : 50731
  8. Source Host : localhost:3306
  9. Source Schema : xscjgl
  10. Target Server Type : MySQL
  11. Target Server Version : 50731
  12. File Encoding : 65001
  13. Date: 18/04/2023 09:12:32
  14. */
  15. SET NAMES utf8mb4;
  16. SET FOREIGN_KEY_CHECKS = 0;
  17. -- ----------------------------
  18. -- Table structure for kc
  19. -- ----------------------------
  20. DROP TABLE IF EXISTS `kc`;
  21. CREATE TABLE `kc` (
  22. `课程号` char(3) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  23. `课程名` char(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  24. `开课学期` tinyint(1) NOT NULL DEFAULT 1 COMMENT '只能为1-8',
  25. `学时` tinyint(1) NOT NULL,
  26. `学分` tinyint(1) NULL DEFAULT NULL,
  27. PRIMARY KEY (`课程号`) USING BTREE
  28. ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
  29. -- ----------------------------
  30. -- Records of kc
  31. -- ----------------------------
  32. INSERT INTO `kc` VALUES ('101', '计算机基础', 1, 80, 5);
  33. INSERT INTO `kc` VALUES ('102', '程序设计与语言', 2, 68, 4);
  34. INSERT INTO `kc` VALUES ('206', '离散数学', 4, 68, 4);
  35. INSERT INTO `kc` VALUES ('208', '数据结构', 5, 68, 4);
  36. INSERT INTO `kc` VALUES ('209', '操作系统', 6, 68, 4);
  37. INSERT INTO `kc` VALUES ('210', '计算机原理', 5, 85, 5);
  38. INSERT INTO `kc` VALUES ('212', '数据库原理', 7, 68, 4);
  39. INSERT INTO `kc` VALUES ('301', '计算机网络', 7, 51, 3);
  40. INSERT INTO `kc` VALUES ('302', '软件工程', 7, 51, 3);
  41. -- ----------------------------
  42. -- Table structure for xs
  43. -- ----------------------------
  44. DROP TABLE IF EXISTS `xs`;
  45. CREATE TABLE `xs` (
  46. `学号` char(6) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  47. `姓名` char(8) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  48. `专业名` char(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  49. `性别` tinyint(1) NOT NULL DEFAULT 1 COMMENT '1为男 0为女',
  50. `出生时间` date NOT NULL,
  51. `总学分` tinyint(1) NULL DEFAULT NULL,
  52. `照片` blob NULL,
  53. `备注` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
  54. PRIMARY KEY (`学号`) USING BTREE
  55. ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
  56. -- ----------------------------
  57. -- Records of xs
  58. -- ----------------------------
  59. INSERT INTO `xs` VALUES ('081101', '王林', '计算机', 1, '1990-02-22', 50, NULL, NULL);
  60. INSERT INTO `xs` VALUES ('081102', '程明', '计算机', 1, '1991-02-01', 50, NULL, NULL);
  61. INSERT INTO `xs` VALUES ('081103', '王燕', '计算机', 0, '1989-10-06', 50, NULL, NULL);
  62. INSERT INTO `xs` VALUES ('081104', '韦严平', '计算机', 1, '1990-08-26', 50, NULL, NULL);
  63. INSERT INTO `xs` VALUES ('081106', '李方方', '计算机', 1, '1990-11-20', 50, NULL, NULL);
  64. INSERT INTO `xs` VALUES ('081107', '李明', '计算机', 1, '1990-05-01', 54, NULL, '提前修完《数据结构》,并获学分');
  65. INSERT INTO `xs` VALUES ('081108', '林一帆', '计算机', 1, '1989-08-05', 52, NULL, '已提前修完一门课');
  66. INSERT INTO `xs` VALUES ('081109', '张强民', '计算机', 1, '1989-08-11', 50, NULL, NULL);
  67. INSERT INTO `xs` VALUES ('081110', '张蔚', '计算机', 0, '1991-07-22', 50, NULL, '三好生');
  68. INSERT INTO `xs` VALUES ('081111', '赵琳', '计算机', 0, '1990-03-18', 50, NULL, NULL);
  69. INSERT INTO `xs` VALUES ('081113', '严红', '计算机', 0, '1989-08-11', 48, NULL, '有一门功课不及格,待补考');
  70. INSERT INTO `xs` VALUES ('081201', '王敏', '通信工程', 1, '1989-06-10', 42, NULL, NULL);
  71. INSERT INTO `xs` VALUES ('081202', '王林', '通信工程', 1, '1989-01-29', 40, NULL, '有一门课不及格,待补考');
  72. INSERT INTO `xs` VALUES ('081203', '王玉民', '通信工程', 1, '1990-03-26', 42, NULL, NULL);
  73. INSERT INTO `xs` VALUES ('081204', '马琳琳', '通信工程', 0, '1989-02-10', 42, NULL, NULL);
  74. INSERT INTO `xs` VALUES ('081206', '李计', '通信工程', 1, '1989-09-20', 42, NULL, NULL);
  75. INSERT INTO `xs` VALUES ('081210', '李红庆', '通信工程', 1, '1989-05-01', 44, NULL, '已提前修完一门课,并获得学分');
  76. INSERT INTO `xs` VALUES ('081216', '孙祥欣', '通信工程', 1, '1989-03-09', 42, NULL, NULL);
  77. INSERT INTO `xs` VALUES ('081218', '孙研', '通信工程', 1, '1990-10-09', 42, NULL, NULL);
  78. INSERT INTO `xs` VALUES ('081220', '吴薇华', '通信工程', 0, '1990-03-18', 42, NULL, NULL);
  79. INSERT INTO `xs` VALUES ('081221', '刘燕敏', '通信工程', 0, '1989-11-12', 42, NULL, NULL);
  80. INSERT INTO `xs` VALUES ('081241', '罗林琳', '通信工程', 0, '1990-01-30', 50, NULL, '转专业学习');
  81. -- ----------------------------
  82. -- Table structure for xs_kc
  83. -- ----------------------------
  84. DROP TABLE IF EXISTS `xs_kc`;
  85. CREATE TABLE `xs_kc` (
  86. `学号` char(6) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  87. `课程号` char(3) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  88. `成绩` tinyint(1) NULL DEFAULT NULL,
  89. `学分` tinyint(1) NULL DEFAULT NULL,
  90. PRIMARY KEY (`学号`, `课程号`) USING BTREE
  91. ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
  92. -- ----------------------------
  93. -- Records of xs_kc
  94. -- ----------------------------
  95. INSERT INTO `xs_kc` VALUES ('081101', '101', 80, 5);
  96. INSERT INTO `xs_kc` VALUES ('081101', '102', 78, 4);
  97. INSERT INTO `xs_kc` VALUES ('081101', '206', 76, 4);
  98. INSERT INTO `xs_kc` VALUES ('081102', '102', 78, 4);
  99. INSERT INTO `xs_kc` VALUES ('081102', '206', 78, 4);
  100. INSERT INTO `xs_kc` VALUES ('081103', '101', 62, 5);
  101. INSERT INTO `xs_kc` VALUES ('081103', '102', 70, 4);
  102. INSERT INTO `xs_kc` VALUES ('081103', '206', 81, 4);
  103. INSERT INTO `xs_kc` VALUES ('081104', '101', 90, 5);
  104. INSERT INTO `xs_kc` VALUES ('081104', '102', 84, 4);
  105. INSERT INTO `xs_kc` VALUES ('081104', '206', 65, 4);
  106. INSERT INTO `xs_kc` VALUES ('081106', '101', 65, 5);
  107. INSERT INTO `xs_kc` VALUES ('081106', '102', 71, 4);
  108. INSERT INTO `xs_kc` VALUES ('081106', '206', 81, 4);
  109. INSERT INTO `xs_kc` VALUES ('081107', '101', 78, 5);
  110. INSERT INTO `xs_kc` VALUES ('081107', '102', 80, 4);
  111. INSERT INTO `xs_kc` VALUES ('081107', '206', 68, 4);
  112. INSERT INTO `xs_kc` VALUES ('081108', '101', 85, 5);
  113. INSERT INTO `xs_kc` VALUES ('081108', '102', 64, 4);
  114. INSERT INTO `xs_kc` VALUES ('081108', '206', 87, 4);
  115. INSERT INTO `xs_kc` VALUES ('081109', '101', 66, 5);
  116. INSERT INTO `xs_kc` VALUES ('081109', '102', 83, 4);
  117. INSERT INTO `xs_kc` VALUES ('081109', '206', 70, 4);
  118. INSERT INTO `xs_kc` VALUES ('081110', '101', 95, 5);
  119. INSERT INTO `xs_kc` VALUES ('081110', '102', 90, 4);
  120. INSERT INTO `xs_kc` VALUES ('081110', '206', 89, 4);
  121. INSERT INTO `xs_kc` VALUES ('081111', '101', 91, 5);
  122. INSERT INTO `xs_kc` VALUES ('081111', '102', 70, 4);
  123. INSERT INTO `xs_kc` VALUES ('081111', '206', 76, 4);
  124. INSERT INTO `xs_kc` VALUES ('081113', '101', 63, 5);
  125. INSERT INTO `xs_kc` VALUES ('081113', '102', 79, 4);
  126. INSERT INTO `xs_kc` VALUES ('081113', '206', 60, 4);
  127. INSERT INTO `xs_kc` VALUES ('081201', '101', 80, 5);
  128. INSERT INTO `xs_kc` VALUES ('081202', '101', 65, 5);
  129. INSERT INTO `xs_kc` VALUES ('081203', '101', 87, 5);
  130. INSERT INTO `xs_kc` VALUES ('081204', '101', 91, 5);
  131. INSERT INTO `xs_kc` VALUES ('081210', '101', 76, 5);
  132. INSERT INTO `xs_kc` VALUES ('081216', '101', 81, 5);
  133. INSERT INTO `xs_kc` VALUES ('081218', '101', 70, 5);
  134. INSERT INTO `xs_kc` VALUES ('081220', '101', 82, 5);
  135. INSERT INTO `xs_kc` VALUES ('081221', '101', 76, 5);
  136. INSERT INTO `xs_kc` VALUES ('081241', '101', 90, 5);
  137. SET FOREIGN_KEY_CHECKS = 1;

习题:

(1)查询学生的学号,姓名,出生日期

  1. use xscjgl;
  2. select 学号,姓名,出生时间 from xs;

(2)查询1990年以后出生的学生的学号,姓名,出生日期

select 学号,姓名,出生时间 from xs where 出生时间>="1990-1-1";

(3)按姓名升序显示学生的学号,姓名。

 select 学号,姓名 from xs order by 姓名;

 

(4)按专业名统计各专业的学生人数。

select 专业名,count(*) 各专业人数 from xs group by 专业名;

(5)查询姓“王”的学生的学号,姓名信息

select 学号,姓名 from xs where 姓名 like "王%";

 (6)查询学生的学号,姓名,课程名,成绩信息。

SELECT b.`学号`,b.`姓名`,c.`课程名`,a.`成绩` FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.`课程号`=c.`课程号`;

(7)统计学生的成绩总分并按总分由高到低排名。

SELECT b.*,sum(成绩) 总分 FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.`课程号`=c.`课程号` group by a.学号 order by 总分 desc;

(8)查询学生成绩总分高于200分的学生信息。

SELECT b.*,sum(成绩) 总分 FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.`课程号`=c.`课程号` group by a.学号  having 总分>200;

(9)统计没有参加考试的学生名单。

select * from xs where 学号 not in(SELECT b.学号 FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.`课程号`=c.`课程号` group by a.学号);

(10)显示总分前三名的学生信息。

SELECT b.*,sum(成绩) 总分 FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.`课程号`=c.`课程号` group by a.学号 order by 总分 desc limit 3;

(11)查询参加计算机基础课程考试的学生成绩高于课程平均分的学生信息。

  1. SELECT b.* ,a.`成绩`FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.`课程号`=c.`课程号` AND c.课程名='计算机基础' AND a.成绩>=(
  2. SELECT AVG(a.成绩) FROM xs_kc a,kc b WHERE a.课程号=b.课程号 AND b.课程名='计算机基础');

(12)查询程序设计与语言课程最高分的学生的学号和姓名

  1. SELECT b.学号,姓名,MAX(成绩) FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.课程号=c.课程号 AND c.课程名='程序设计与语言' AND a.`成绩`=(
  2. SELECT MAX(成绩) FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.课程号=c.课程号 AND c.课程名='程序设计与语言');

(13)查询计算机基础,程序设计与语言和离散数学三门课程中最高分学生的学号,

  1. SELECT b.学号,姓名,MAX(成绩) FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.课程号=c.课程号 AND c.课程名='计算机基础' AND a.`成绩`=(
  2. SELECT MAX(成绩) FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.课程号=c.课程号 AND c.课程名='计算机基础')
  3. UNION ALL
  4. SELECT b.学号,姓名,MAX(成绩) FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.课程号=c.课程号 AND c.课程名='程序设计与语言' AND a.`成绩`=(
  5. SELECT MAX(成绩) FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.课程号=c.课程号 AND c.课程名='程序设计与语言')
  6. UNION ALL
  7. SELECT b.学号,姓名,MAX(成绩) FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.课程号=c.课程号 AND c.课程名='离散数学' AND a.`成绩`=(
  8. SELECT MAX(成绩) FROM xs_kc a,xs b,kc c WHERE a.`学号`=b.`学号` AND a.课程号=c.课程号 AND c.课程名='离散数学');

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/701149
推荐阅读
相关标签
  

闽ICP备14008679号