题目
为管理业务培训信息,现需建立3个表:
表S(S#,SN,SD,SA)S#,SN,SD,SA分别代表学号,学员姓名,所属单位,学员年龄、
表C(C#,CN)C#,CN分别代表课程编号,课程名称
表SC(S#,C#,G)S#,C#,G分别代表学号,所选的课程编号,学习成绩
(1)使用标准SQL嵌套语句查询选修课程名称为'税收基础’的学员学号和姓名?
(2)使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位?
(3)使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位?
(4)查询选修了课程的学员人数
(5)查询选修课程超过5门的学员学号和所属单位?
建表sql语句
-- ----------------------------
-- Table structure for c
-- ----------------------------
DROP TABLE IF EXISTS `c`;
CREATE TABLE `c` (`C1` int(0) NULL DEFAULT NULL COMMENT '课程编号',`C2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '课程名称'
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of c
-- ----------------------------
INSERT INTO `c` VALUES (1, '税收基础');
INSERT INTO `c` VALUES (2, '税收进阶');
INSERT INTO `c` VALUES (3, '语文');
INSERT INTO `c` VALUES (4, '数学');
INSERT INTO `c` VALUES (5, '英语');-- ----------------------------
-- Table structure for s
-- ----------------------------
DROP TABLE IF EXISTS `s`;
CREATE TABLE `s` (`S1` int(0) NOT NULL AUTO_INCREMENT COMMENT '学号',`S2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '姓名',`S3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '单位',`S4` int(0) NOT NULL COMMENT '年龄',PRIMARY KEY (`S1`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of s
-- ----------------------------
INSERT INTO `s` VALUES (1, '铭', '啊', 21);
INSERT INTO `s` VALUES (2, '杰', '经', 20);
INSERT INTO `s` VALUES (3, '云', '团', 20);
INSERT INTO `s` VALUES (4, '张三', '团支书', 49);
INSERT INTO `s` VALUES (5, '李斯', '战神', 80);
INSERT INTO `s` VALUES (6, '童锦成', '老六', 100);-- ----------------------------
-- Table structure for sc
-- ----------------------------
DROP TABLE IF EXISTS `sc`;
CREATE TABLE `sc` (`S1` int(0) NULL DEFAULT NULL COMMENT '学号',`C1` int(0) NULL DEFAULT NULL COMMENT '课程编号',`G` int(0) NULL DEFAULT NULL COMMENT '学习成绩'
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of sc
-- ----------------------------
INSERT INTO `sc` VALUES (1, 2, 80);
INSERT INTO `sc` VALUES (2, 2, 78);
INSERT INTO `sc` VALUES (3, 1, 78);
INSERT INTO `sc` VALUES (1, 1, 67);
INSERT INTO `sc` VALUES (2, 1, 65);
INSERT INTO `sc` VALUES (3, 2, 98);
INSERT INTO `sc` VALUES (4, 1, 78);
INSERT INTO `sc` VALUES (5, 2, 68);
INSERT INTO `sc` VALUES (1, 5, 65);
INSERT INTO `sc` VALUES (4, 5, 45);
解题
(1)使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名?
select * from s where s1 in(
select s1 from sc where c1=(select C1 from c where C2 ='税收基础')
)(2)使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位?
select * from s where s1 in(
select s1 from sc where c1 in(
select c1 from c where c1=2)
)
(3)使用标准SQL嵌套语句查询不选修课程编号为’C5的学员姓名和所属单位?
select * from s where s1 not in (select s1 from sc where c1=5)
(4)查询选修了课程的学员人数select count(a.s1) from (select s1 from sc GROUP BY s1)a(5)查询选修课程超过5门的学员学号和所属单位?
select s1,s3 from s where s1 in (
select s1 from sc GROUP BY s1 HAVING count(s1)>=3)