mysql考试总结
Posted 努力哥-运维自动化
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql考试总结相关的知识,希望对你有一定的参考价值。
USE school; -- 班级表 CREATE TABLE class( cid TINYINT PRIMARY KEY AUTO_INCREMENT, caption VARCHAR(20) ); INSERT INTO class(caption) VALUES("三年二班"),("一年三班"),("三年一班"); SELECT * FROM class; -- 老师表 CREATE TABLE teacher( tid TINYINT PRIMARY KEY AUTO_INCREMENT, tname VARCHAR(20) ); INSERT INTO teacher(tname) VALUES("波多"),("苍空"),("饭岛"); SELECT * FROM teacher; -- 学生表 CREATE TABLE student( sid TINYINT PRIMARY KEY AUTO_INCREMENT, sname VARCHAR(20), gender VARCHAR(10), class_id TINYINT, FOREIGN KEY (class_id) REFERENCES class(cid) ); INSERT INTO student(sname,gender,class_id) VALUES ("钢蛋","女",1), ("铁锤","女",1), ("山炮","男",2); SELECT * FROM student; -- 课程表 CREATE TABLE course( cid TINYINT PRIMARY KEY AUTO_INCREMENT, cname VARCHAR(20), teacher_id TINYINT, FOREIGN KEY (teacher_id) REFERENCES teacher(tid) ); ALTER TABLE course MODIFY cid TINYINT, DROP PRIMARY KEY; ALTER TABLE course ADD CONSTRAINT xx FOREIGN KEY (cid) REFERENCES class(cid); DESC course; SHOW CREATE TABLE course; INSERT INTO course(cname,teacher_id) VALUES ("生物",1), ("体育",1), ("物理",2); SELECT * FROM course; -- 成绩表 CREATE TABLE score( sid TINYINT PRIMARY KEY AUTO_INCREMENT, student_id TINYINT, course_id TINYINT, number INT, FOREIGN KEY (student_id) REFERENCES student(sid), FOREIGN KEY (course_id) REFERENCES course(cid) ); INSERT INTO score(student_id, course_id, number) VALUES (1,1,60), (1,2,59), (2,2,100); SELECT * FROM score; DELETE FROM score WHERE sid=6; # 二、操作表 # # 1、自行创建测试数据 # # 2、查询“生物”课程比“物理”课程成绩高的所有学生的学号; SELECT B.student_id FROM (SELECT score.student_id,score.number FROM score INNER JOIN course ON score.course_id = course.cid WHERE course.cname="生物") as A INNER JOIN (SELECT score.student_id,score.number FROM score INNER JOIN course ON score.course_id = course.cid WHERE course.cname="物理") as B ON A.student_id=B.student_id AND A.number>B.number; # 3、查询平均成绩大于60分的同学的学号和平均成绩; INSERT INTO score(student_id, course_id, number) VALUES(2,3,56), (3,1,46),(3,2,59),(3,3,71),(4,1,90),(4,2,27); SELECT student_id,avg(number) 平均成绩 FROM score GROUP BY student_id HAVING avg(number)>60; # 4、查询所有同学的学号、姓名、选课数、总成绩; SELECT student.sid,student.sname,count(student.class_id) 选课数,sum(number) 总成绩 FROM student INNER JOIN score ON student.sid = score.student_id GROUP BY sname ORDER BY 总成绩; # 5、查询姓“波”的老师的个数; SELECT count(*) 波老师个数 FROM teacher WHERE tname LIKE "波%"; INSERT INTO teacher(tname) VALUES ("波大"); # 6、查询没学过“叶平”老师课的同学的学号、姓名; -- 得到所有同学学过的课程及其对应的老师,然后对应筛选 INSERT INTO teacher(tname) VALUES("叶平"); INSERT INTO course(cname, teacher_id) VALUES("历史",5); INSERT INTO score(student_id, course_id, number) VALUES(1,5,81); SELECT student.sid,student.sname,A.tname FROM score INNER JOIN student ON score.student_id = student.sid INNER JOIN (SELECT DISTINCT course.cid,course.teacher_id,course.cname,teacher.tname FROM course INNER JOIN teacher ON course.teacher_id = teacher.tid) AS A ON score.course_id = A.cid GROUP BY A.tname HAVING A.tname!="叶平"; # 7、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名; INSERT INTO student(sname,gender,class_id) VALUES("张三","男",3); INSERT INTO score(student_id, course_id, number) VALUES(5,2,63); SELECT B.sid 学号,B.sname 姓名 FROM (SELECT student.sid,student.sname FROM score INNER JOIN student ON score.student_id = student.sid WHERE course_id=1) AS A INNER JOIN (SELECT student.sid,student.sname FROM score INNER JOIN student ON score.student_id = student.sid WHERE course_id=2) AS B ON A.sid = B.sid; # 8、查询学过“叶平”老师所教的所有课的同学的学号、姓名; SELECT * FROM student; SELECT * FROM score; INSERT INTO student(sname, gender, class_id) VALUES("王五","男",3); INSERT INTO score(student_id, course_id, number) VALUES(8,5,93); SELECT student.sid 学号,student.sname 姓名 FROM score INNER JOIN student ON score.student_id = student.sid AND course_id=5; # 9、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名; INSERT INTO student(sname, gender, class_id) VALUES("赵六","女",2); INSERT INTO score(student_id, course_id, number) VALUES(9,2,65),(9,1,70); UPDATE score SET number = 65 WHERE sid=21; SELECT * FROM student WHERE (SELECT number FROM score WHERE student.sid=score.student_id AND score.course_id=2)< (SELECT number FROM score WHERE student.sid=score.student_id AND score.course_id=1); # 10、查询有课程成绩小于60分的同学的学号、姓名; SELECT student.sid,student.sname FROM student INNER JOIN score ON student.sid = score.student_id WHERE score.number<60 GROUP BY student.sname; INSERT INTO student(sname, gender, class_id) VALUES("钢镚","男",2); INSERT INTO score(student_id, course_id, number) VALUES(4,3,48); # 11、查询没有学全所有课的同学的学号、姓名; -- 测试 SELECT count(cid) 总课程数 FROM course; SELECT count(course_id) study_course FROM score GROUP BY student_id; SELECT * FROM (SELECT count(cid) 总课程数 FROM course) AS A INNER JOIN (SELECT count(course_id) study_course FROM score GROUP BY student_id) AS B ON A.总课程数 = B.study_course; -- 正确答案 SELECT student.sid,student.sname,count(course_id) 学习课程数 FROM score INNER JOIN student ON score.student_id = student.sid GROUP BY student_id HAVING count(course_id)=(SELECT count(cid) FROM course); # 12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名; # # 13、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名; # # 14、查询和“2”号的同学学习的课程完全相同的其他同学学号和姓名; # 15、删除学习“叶平”老师课的SC表记录; # # 16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩; # # 17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分; # # 18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分; SELECT course_id,max(number) 最高分,min(number) 最低分 FROM score GROUP BY course_id; INSERT INTO score(student_id, course_id, number) VALUES(2,1,76); # 19、按各科平均成绩从低到高和及格率的百分数从高到低顺序; -- 分析求出平均成绩并计算及格率 SELECT avg(number) 课程平均成绩 FROM score GROUP BY course_id; SELECT count(student_id) 各科不及格人数 FROM score WHERE number>60 GROUP BY course_id; SELECT count(student_id) 各科总人数 FROM score GROUP BY course_id; -- 答案如下 SELECT A.课程平均成绩,B.各科不及格人数/C.各科总人数 AS 及格率 FROM (SELECT course_id,avg(number) 课程平均成绩 FROM score GROUP BY course_id) AS A INNER JOIN (SELECT course_id,count(student_id) 各科不及格人数 FROM score WHERE number>60 GROUP BY course_id) AS B INNER JOIN (SELECT course_id,count(student_id) 各科总人数 FROM score GROUP BY course_id) AS C ON A.course_id = B.course_id AND A.course_id = C.course_id ORDER BY A.课程平均成绩; # 20、课程平均分从高到低显示(显示任课老师); SELECT teacher.tname,course.cname FROM teacher,course WHERE course.teacher_id = teacher.tid; SELECT avg(number) 课程平均分 FROM teacher,score GROUP BY course_id; -- 答案如下 SELECT A.tname 任课老师,B.课程平均分 FROM (SELECT teacher.tname,course.cname,course.cid FROM teacher,course WHERE course.teacher_id = teacher.tid) AS A INNER JOIN (SELECT avg(number) 课程平均分,teacher.tname,score.course_id FROM teacher,score GROUP BY course_id) AS B ON A.cid=B.course_id ORDER BY B.课程平均分 DESC; # 21、查询各科成绩前三名的记录:(不考虑成绩并列情况) SELECT * FROM score ORDER BY course_id; # 22、查询每门课程被选修的学生数; -- 分析 根据成绩根据课程进行分类,按照学生ID统计次数 SELECT score.course_id 课程ID,count(student_id) 课程人次 FROM score GROUP BY course_id; # 23、查询出只选修了一门课程的全部学生的学号和姓名; SELECT student.sid,student.sname FROM (SELECT * FROM score GROUP BY student_id HAVING count(student_id)=1) AS A INNER JOIN student ON A.student_id = student.sid; # 24、查询男生、女生的人数; SELECT boy.男,girl.女 FROM (SELECT count(gender) 男 FROM student WHERE gender="男") as boy, (SELECT count(gender) 女 FROM student WHERE gender="女") as girl; # 25、查询姓“张”的学生名单; SELECT sid,sname FROM student WHERE sname LIKE "张%"; # 26、查询同名同姓学生名单,并统计同名人数; SELECT sname 姓名,count(sname) 同名人数 FROM student GROUP BY sname; # 27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列; SELECT A.cid 课程号,B.课程平均分 FROM (SELECT teacher.tname,course.cname,course.cid FROM teacher,course WHERE course.teacher_id = teacher.tid) AS A INNER JOIN (SELECT avg(number) 课程平均分,teacher.tname,score.course_id FROM teacher,score GROUP BY course_id) AS B ON A.cid=B.course_id ORDER BY B.课程平均分 DESC; # 28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩; SELECT student.sid 学号,student.sname 姓名,avg(score.number) 平均成绩 FROM student INNER JOIN score ON student.sid = score.student_id GROUP BY sname; # 29、查询课程名称为“生物”,且分数低于60的学生姓名和分数; SELECT course.cname 课程,student.sname 姓名,score.number 分数 FROM score INNER JOIN student INNER JOIN course ON score.student_id = student.sid AND score.course_id=course.cid WHERE course.cname="生物" AND score.number<60; # 30、查询课程编号为3且课程成绩在80分以上的学生的学号和姓名; SELECT student.sid 学号,student.sname 姓名 FROM score INNER JOIN student ON score.student_id = student.sid AND score.course_id=3 AND score.number>80; # 31、求选了课程的学生人数 INSERT INTO student(sname, gender, class_id) VALUES("李四","男",3); INSERT INTO student(sname, gender, class_id) VALUES("胜七","女",2); INSERT INTO score(student_id, course_id, number) VALUES(7,1,74); -- 答案如下 SELECT count(A.student_id) 选课人数 FROM (SELECT DISTINCT student_id FROM score) AS A; # 32、查询选修“苍空”老师所授课程的学生中,成绩最高的学生姓名及其成绩; SELECT student.sname 姓名,max(number) 成绩 FROM score INNER JOIN student INNER JOIN course INNER JOIN teacher ON score.student_id=student.sid AND score.course_id=course.cid AND course.teacher_id=teacher.tid WHERE teacher.tname="苍空"; # 33、查询各个课程及相应的选修人数; SELECT score.course_id 课程ID,count(student_id) 课程人次 FROM score GROUP BY course_id; # 34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩; # # 35、查询每门课程成绩最好的前两名; # # 36、检索至少选修两门课程的学生学号; -- 分析 根据学生ID进行分组,统计课程出现次数,筛选课程次数大于等于2的学生 SELECT student_id FROM score GROUP BY student_id HAVING count(course_id)>=2; # 37、查询全部学生都选修的课程的课程号和课程名; SELECT score.student_id 学生ID,course.cname 所选课程,course.cid 课程ID FROM score INNER JOIN course ON score.course_id = course.cid ORDER BY 学生ID; # 38、查询没学过“叶平”老师讲授的任一门课程的学生姓名; # # 39、查询两门以上不及格课程的同学的学号及其平均成绩; SELECT A.student_id 学号,B.平均成绩 FROM (SELECT student_id FROM score WHERE number<60 GROUP BY student_id HAVING count(number)>=2) AS A INNER JOIN (SELECT student_id,avg(number) 平均成绩 FROM score GROUP BY student_id) AS B ON A.student_id=B.student_id; # 40、检索“1”课程分数小于60,按分数降序排列的同学学号; SELECT student_id FROM score WHERE course_id=1 AND number<60; # 41、删除“2”同学的“1”课程的成绩; -- 此题已答
以上是关于mysql考试总结的主要内容,如果未能解决你的问题,请参考以下文章