做完这33道题,再也不用担心sql笔试,废话不多说,咱们直接上干货!!!
1:题目
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
2、 查询教师所有的单位即不重复的Depart列。
3、 查询Score表中成绩在60到80之间的所有记录。
4、 查询Score表中成绩为85,86或88的记录。
5、 查询Student表中“95031”班或性别为“女”的同学记录。
6、 以Class降序查询Student表的所有记录。
7、 以Cno升序、Degree降序查询Score表的所有记录。
8、 查询“95031”班的学生人数。
9、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
10、 查询每门课的平均成绩。
11、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
12、查询所有学生的Sname、Cno和Degree列。
13、查询所有学生的Sname、Cname和Degree列。
14、 查询“95033”班学生的平均分。
15、 查询所有同学的Sno、Cno和rank列。
16、 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
17、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
18、 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
19、查询和学号为101的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
20、查询“张旭“教师任课的学生成绩。
21、查询选修某课程的同学人数多于5人的教师姓名。
22、查询出“计算机系“教师所教课程的成绩表。
23、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
24、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
25、 查询所有教师和同学的name、sex和birthday.
26、查询所有“女”教师和“女”同学的name、sex和birthday.
27、 查询成绩比该课程平均成绩低的同学的成绩表。
28、查询至少有2名男生的班号。
29、查询Student表中不姓“王”的同学记录。
30、查询Student表中每个学生的姓名和年龄。
31、以班号和年龄从大到小的顺序查询Student表中的全部记录。
32、查询和“李军”同性别并同班的同学Sname.
33、查询所有选修“计算机导论”课程的“男”同学的成绩表。
2:答案
-- 1、查询Student表中的所有记录的Sname、Ssex和Class列。
select st.sname,st.ssex,st.class from student st;
-- 2、查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher;
-- 3、查询Score表中成绩在60到80之间的所有记录。
select * from score sc where sc.degree between 60 and 80;
-- 4、查询Score表中成绩为85,86或88的记录。
select * from score where degree in (85,86,88);
-- 5、查询Student表中“”班或性别为“女”的同学记录。
select * from student st where st.class=95031 or st.ssex = '女'; -- 常规
select * from student st where st.class=95031 union select * from student s where s.ssex = '女' ; -- 优化后
-- 6、以Class降序查询Student表的所有记录。
select * from student order by class desc;
-- 7、以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by cno asc,degree desc;
-- 8、查询“95031”班的学生人数。
select count(1) from student where class = 95031;
-- 9、 查询Score表中的最高分的学生学号和课程号。
select sno,cno from score order by degree desc limit 1;
-- 10、查询每门课的平均成绩。
select cno,AVG(degree) as '平均分' from score group by cno;
-- 11、查询Score表中至少有5名学生选修,并以3开头的课程的平均分数。
select cno,AVG(degree) from score group by cno having count(sno)>=5 and cno like '3%';
-- 12、查询所有学生的Sname、Cno和Degree列。
select sname,cno,degree from student st join score sc on st.sno = sc.sno;
-- 13、查询所有学生的Sname、Cname和Degree列。
select sname,cname,degree from student st join score sc on st.sno = sc.sno join course co on co.cno = sc.cno;
-- 14、 查询“95033”班学生的平均分。
select AVG(degree) from score sc join student st on sc.sno = st.sno and st.class = 95033;
-- 15、查询所有同学的Sno、Cno和rank列:
select sc.sno,sc.cno,gr.rank from score sc join grade gr on sc.degree between gr.low and gr.upp;
-- 16、 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
SELECT * FROM student WHERE
sno IN ( SELECT sno FROM score WHERE cno = '3-105' AND degree > ( SELECT degree FROM score WHERE sno = 109 AND cno = '3-105' ) );
-- 17、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
select * from score where sno in(select sno from score group by sno having count(cno)>1 ) and degree != (select MAX(degree) from score);
-- 18、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from score where degree > (select degree from score where sno = 109 and cno = '3-105');
-- 19、查询和学号为101的同学同年出生的,所有学生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from student where YEAR(sbirthday) like (select YEAR(sbirthday) from student where sno = 101) and sno != 101;
-- 20、查询“张旭“教师任课的学生成绩。
select sc.degree from score sc where sc.cno in (select co.cno from teacher te join course co on te.tno = co.tno and te.tname = '张旭');
-- 21、查询选修某课程的同学人数多于5人的教师姓名。
select tname from teacher where tno in
( select co.tno from score sc join course co on sc.cno = co.cno group by sc.cno having count(sc.sno)>5 );
-- 22、查询出“计算机系“教师所教课程的成绩表。
select * from score where cno in (select co.cno from course co join teacher te on te.depart = '计算机系' and te.tno = co.tno);
-- 23、查询选修编号为“-105“课程且成绩至少高于选修编号为“-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
select * from score where cno='3-105' and degree >
(select max(degree) from score where cno='3-245' )
order by degree desc;
-- 24、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select cno,sno,degree from score where cno='3-105' and degree >(select max(degree) from score where cno='3-245' );
-- 25、查询所有教师和同学的name、sex和birthday.
select tname,tsex,tbirthday from teacher union select sname ,ssex ,sbirthday from student;
-- 26、查询所有“女”教师和“女”同学的name、sex和birthday.
select tname,tsex,tbirthday from teacher where tsex='女' union select sname ,ssex ,sbirthday from student where ssex ='女';
-- 27、查询成绩比该课程平均成绩低的同学的成绩表。
select * from score where degree < any(select AVG(degree) from score group by cno);
-- 28、查询至少有2名男生的班号。
select class from student group by class having count(class)>=2;
-- 29、查询Student表中不姓“王”的同学记录。
select * from student where sname not like '王%';
-- 30、查询Student表中每个学生的姓名和年龄。
select sname, YEAR(now())-YEAR(sbirthday) as '年龄' from student ;
-- 31、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from student order by class desc, YEAR(now())-YEAR(sbirthday) desc ;
-- 32、查询和“李军”同性别并同班的同学Sname.
select sname from student where ssex=(select ssex from student where sname='李军')and
class=(select class from student where sname='李军');
-- 33、查询所有选修“计算机导论”课程的“男”同学的成绩表。
select sc.sno,sc.cno,sc.degree from score sc join course co on sc.cno = co.cno and co.cname = '计算机导论' and sno in (select sno from student where ssex = '男');
3:脚本
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cno` char(5) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`cname` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`tno` int(3) DEFAULT NULL,
PRIMARY KEY (`cno`) USING BTREE,
INDEX `tno`(`tno`) USING BTREE,
CONSTRAINT `course_ibfk_1` FOREIGN KEY (`tno`) REFERENCES `teacher` (`tno`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '课程表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('3-105', '计算机导论', 825);
INSERT INTO `course` VALUES ('3-245', '操作系统', 804);
INSERT INTO `course` VALUES ('6-166', '数字电路', 856);
INSERT INTO `course` VALUES ('9-888', '高等数学', 831);
-- ----------------------------
-- Table structure for grade
-- ----------------------------
DROP TABLE IF EXISTS `grade`;
CREATE TABLE `grade` (
`low` int(11) DEFAULT NULL,
`upp` int(11) DEFAULT NULL,
`rank` char(1) CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci COMMENT = '等级表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of grade
-- ----------------------------
INSERT INTO `grade` VALUES (90, 100, 'A');
INSERT INTO `grade` VALUES (80, 89, 'B');
INSERT INTO `grade` VALUES (70, 79, 'C');
INSERT INTO `grade` VALUES (60, 69, 'D');
INSERT INTO `grade` VALUES (0, 59, 'E');
-- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`sno` int(3) DEFAULT NULL,
`cno` char(5) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`degree` decimal(4, 1) DEFAULT NULL,
INDEX `sno`(`sno`) USING BTREE,
INDEX `cno`(`cno`) USING BTREE,
CONSTRAINT `score_ibfk_1` FOREIGN KEY (`sno`) REFERENCES `student` (`sno`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `score_ibfk_2` FOREIGN KEY (`cno`) REFERENCES `course` (`cno`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '成绩表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES (103, '3-245', 86.0);
INSERT INTO `score` VALUES (105, '3-245', 75.0);
INSERT INTO `score` VALUES (109, '3-245', 68.0);
INSERT INTO `score` VALUES (103, '3-105', 92.0);
INSERT INTO `score` VALUES (105, '3-105', 88.0);
INSERT INTO `score` VALUES (109, '3-105', 76.0);
INSERT INTO `score` VALUES (101, '3-105', 64.0);
INSERT INTO `score` VALUES (107, '3-105', 91.0);
INSERT INTO `score` VALUES (108, '3-105', 78.0);
INSERT INTO `score` VALUES (101, '6-166', 85.0);
INSERT INTO `score` VALUES (107, '6-166', 79.0);
INSERT INTO `score` VALUES (108, '6-166', 81.0);
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sno` int(3) NOT NULL,
`sname` char(8) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`ssex` char(2) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`sbirthday` datetime(0) DEFAULT NULL,
`class` int(5) DEFAULT NULL,
PRIMARY KEY (`sno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '学生表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (101, '李军', '男', '1976-02-20 00:00:00', 95033);
INSERT INTO `student` VALUES (103, '陆君', '男', '1974-06-03 00:00:00', 95031);
INSERT INTO `student` VALUES (105, '匡明', '男', '1975-10-02 00:00:00', 95031);
INSERT INTO `student` VALUES (107, '王丽', '女', '1976-01-23 00:00:00', 95033);
INSERT INTO `student` VALUES (108, '曾华', '男', '1977-09-01 00:00:00', 95033);
INSERT INTO `student` VALUES (109, '王芳', '女', '1975-02-10 00:00:00', 95031);
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`tno` int(3) NOT NULL,
`tname` char(4) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`tsex` char(2) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`tbirthday` datetime(0) DEFAULT NULL,
`prof` char(6) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`depart` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`tno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '教师表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES (804, '李成', '男', '1958-12-02 00:00:00', '副教授', '计算机系');
INSERT INTO `teacher` VALUES (825, '王萍', '女', '1972-05-05 00:00:00', '助教', '计算机系');
INSERT INTO `teacher` VALUES (831, '刘冰', '女', '1977-08-14 00:00:00', '助教', '电子工程系');
INSERT INTO `teacher` VALUES (856, '张旭', '男', '1969-03-12 00:00:00', '讲师', '电子工程系');
SET FOREIGN_KEY_CHECKS = 1;
如果您觉得文章有帮助,欢迎点赞收藏加关注,一连三击呀,感谢!!☺☻