From fa7c3ac189956d3b64574336613af9602784b4d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E9=9B=AA=E7=90=BC?= <3403255493@qq.com> Date: Sun, 22 Oct 2023 22:06:23 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=B0=E9=9B=AA=E7=90=BC=E7=9A=84=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...23\351\242\230\344\275\234\344\270\232.md" | 229 ++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 "35 \347\224\260\351\233\252\347\220\274/20231020 50\351\201\223\351\242\230\344\275\234\344\270\232.md" diff --git "a/35 \347\224\260\351\233\252\347\220\274/20231020 50\351\201\223\351\242\230\344\275\234\344\270\232.md" "b/35 \347\224\260\351\233\252\347\220\274/20231020 50\351\201\223\351\242\230\344\275\234\344\270\232.md" new file mode 100644 index 0000000..35450e7 --- /dev/null +++ "b/35 \347\224\260\351\233\252\347\220\274/20231020 50\351\201\223\351\242\230\344\275\234\344\270\232.md" @@ -0,0 +1,229 @@ +# 作业 + +```mysql +CREATE DATABASE tt charset utf8; +use tt; +# 学生表 +CREATE TABLE IF NOT EXISTS `student`( + `student_id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '学号', + `student_name` VARCHAR(10) NOT NULL DEFAULT '匿名' COMMENT '姓名', + `birthday` DATETIME NOT NULL COMMENT '出生日期', + `gender` VARCHAR(10) NOT NULL DEFAULT '男' COMMENT '性别', + PRIMARY KEY(`student_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `student` VALUES +(1 , '赵雷' , '1990-01-01' , '男'), +(2 , '钱电' , '1990-12-21' , '男'), +(3 , '孙风' , '1990-12-20' , '男'), +(4 , '李云' , '1990-12-06' , '男'), +(5 , '周梅' , '1991-12-01' , '女'), +(6 , '吴兰' , '1992-01-01' , '女'), +(7 , '郑竹' , '1989-01-01' , '女'), +(8 , '张三' , '2017-12-20' , '女'), +(9 , '李四' , '2017-12-25' , '女'), +(10 , '李四' , '2012-06-06' , '女'), +(11 , '赵六' , '2013-06-13' , '女'), +(12 , '孙七' , '2014-06-01' , '女'); +-- 课程表 +CREATE TABLE IF NOT EXISTS `course`( + `course_id` INT(4) NOT NULL AUTO_INCREMENT COMMENT '课程编号', + `course_name` VARCHAR(10) NOT NULL COMMENT '课程名', + `teacher_id` INT(10) NOT NULL COMMENT '任课教师工号', + PRIMARY KEY(`course_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `course` VALUES +(1, '语文', 2), +(2, '数学', 1), +(3, '英语', 3); +# 教师表 +CREATE TABLE IF NOT EXISTS `teacher`( + `teacher_id` INT(10) NOT NULL AUTO_INCREMENT COMMENT '教师工号', + `teacher_name` VARCHAR(10) NOT NULL DEFAULT '匿名' COMMENT '教师姓名', + PRIMARY KEY(`teacher_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `teacher` VALUES +(1, '高斯'), +(2, '李白'), +(3, 'Trump'); + +-- 成绩表 +CREATE TABLE IF NOT EXISTS `score`( + `student_id` INT(10) NOT NULL COMMENT '学号', + `course_id` INT(4) NOT NULL COMMENT '课程编号', + `score` DECIMAL(18,1) COMMENT '成绩', + KEY(`course_id`) +)ENGINE=INNODB CHARSET=utf8; + +INSERT INTO `score` VALUES +(1 , 1 , 80), +(1 , 2 , 90), +(1 , 3 , 99), +(2 , 1 , 70), +(2 , 2 , 60), +(2 , 3 , 80), +(3 , 1 , 80), +(3 , 2 , 80), +(3 , 3 , 80), +(4 , 1 , 50), +(4 , 2 , 30), +(4 , 3 , 20), +(5 , 1 , 76), +(5 , 2 , 87), +(6 , 1 , 31), +(6 , 3 , 34), +(7 , 2 , 89), +(7 , 3 , 98); + + +-- 1. 查询" 1 "课程比" 2 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 +WITH a AS(SELECT s.student_id,s.student_name,s.gender,s.birthday,c.course_id,c.score from student s,score c WHERE s.student_id=c.student_id and c.course_id=1), + b AS(SELECT s.student_id,s.student_name,s.gender,s.birthday,c.course_id,c.score from student s,score c WHERE s.student_id=c.student_id and c.course_id=2) +SELECT * from a,b where a.student_id=b.student_id and a.score>b.score; + +-- 2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 +WITH a AS (SELECT s.student_id,s.student_name,s.birthday,s.gender,c.course_id,c.score from student s, score c WHERE s.student_id=c.student_id and course_id=1), + b as (SELECT s.student_id,s.student_name,s.birthday,s.gender,c.course_id,c.score from student s, score c WHERE s.student_id=c.student_id and course_id=2) +SELECT * from a,b WHERE a.student_id=b.student_id; +-- 3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) +WITH a AS (SELECT s.student_id,s.student_name,s.birthday,s.gender,c.course_id,c.score from student s, score c WHERE s.student_id=c.student_id and course_id=1), + b as (SELECT s.student_id,s.student_name,s.birthday,s.gender,c.course_id,c.score from student s, score c WHERE s.student_id=c.student_id and course_id=2) +SELECT * from a left JOIN b on a.student_id=b.student_id where b.gender is null; + +-- 4. 查询不存在" 1 "课程但存在" 2 "课程的情况 +WITH a AS (SELECT s.student_id,s.student_name,s.birthday,s.gender,c.course_id,c.score from student s, score c WHERE s.student_id=c.student_id and course_id=1), + b as (SELECT s.student_id,s.student_name,s.birthday,s.gender,c.course_id,c.score from student s, score c WHERE s.student_id=c.student_id and course_id=2) +SELECT * from a RIGHT JOIN b on a.student_id=b.student_id where a.gender is null; +-- 5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 +SELECT DISTINCT s.student_id,s.student_name,avg(score) over(PARTITION by s.student_id) from student s ,score c WHERE s.student_id=c.student_id and score>60; +-- 6. 查询在成绩表存在成绩的学生信息 +SELECT * from student s LEFT JOIN score c on s.student_id=c.student_id WHERE c.score is not null ; +-- 7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +SELECT s.student_id,s.student_name,COUNT(c.course_id) over(PARTITION BY s.student_id) ,SUM(score) over(PARTITION by s.student_id) from student s LEFT JOIN score c on s.student_id=c.student_id; +-- 8. 查询「李」姓老师的数量 +SELECT COUNT(teacher_name) from teacher WHERE teacher_name LIKE'李%'; +-- 9. 查询学过「李白」老师授课的同学的信息 +SELECT * from student s,course c,teacher t,score r WHERE s.student_id=r.student_id and c.teacher_id=t.teacher_id and c.course_id=r.course_id and t.teacher_name='李白'; +-- 10. 查询没有学全所有课程的同学的信息 +SELECT *, COUNT(s.student_id) over(PARTITION by s.student_id),sum(c.course_id) over(PARTITION by s.student_id) from student s LEFT JOIN score c on s.student_id=c.student_id WHERE score is null; +-- 11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 +select * from student,score where student.student_id=score.student_id and course_id in( +select course_id from student,score where student.student_id=score.student_id and student.student_id=1); +-- 12. 查询和" 1 "号的同学学习的课程完全相同的其他同学的信息 +with aa as +(select distinct student.student_id,student.student_name,sum(score.course_id) over (partition by student.student_id ) 课程编号和 from student,score where student.student_id=score.student_id) +select aa.student_id,aa.student_name from aa where 课程编号和=(select sum(course_id) from student,score where student.student_id=score.student_id and student.student_id=1 and aa.student_id!=1); +-- 13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 +select distinct student.student_name from student,score,course,teacher where student.student_id=score.student_id and score.course_id=course.course_id and course.teacher_id=teacher.teacher_id and teacher.teacher_name !='李白' ; +-- 14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +select distinct student.student_id,count(student.student_id) over (partition by student.student_id) 不及格课程个数,student.student_name,avg(score) over (partition by student.student_id) from student,score where student.student_id=score.student_id and score.score<60; + +-- 15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 +SELECT * from student s,score c WHERE s.student_id=c.student_id and c.course_id=1 and score<60 ORDER BY c.score desc; +-- 16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +with aa as +(SELECT student.student_id,student.student_name,student.birthday,student.gender,score.course_id,score,avg( score ) over (PARTITION BY student.student_id ) 平均成绩 FROM student,score WHERE student.student_id = score.student_id) select * from aa order by 平均成绩 desc; +; +-- 17. 查询各科成绩最高分、最低分和平均分 +SELECT DISTINCT c.course_name,MAX(score) over(PARTITION by c.course_id),MIN(score) over(PARTITION by c.course_id),AVG(score) over(PARTITION by c.course_id) from score s,course c WHERE s.course_id=c.course_id ; + +-- 18. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 +-- 19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 +SELECT c.course_name,score,RANK() over(PARTITION by c.course_id ORDER BY score desc) from score s,course c where s.course_id=c.course_id; +-- 20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 +SELECT c.course_name,score,DENSE_RANK() over(PARTITION by c.course_id ORDER BY score desc) from score s,course c where s.course_id=c.course_id; +-- 21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +WITH a as +(SELECT DISTINCT s.student_id,s.student_name,sum(score) over(PARTITION by s.student_id) 总成绩 from student s,score c WHERE s.student_id=c.student_id) +SELECT *,RANK() over(ORDER BY 总成绩 desc ) from a; +-- 22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +WITH a as +(SELECT DISTINCT s.student_id,s.student_name,sum(score) over(PARTITION by s.student_id) 总成绩 from student s,score c WHERE s.student_id=c.student_id) +SELECT *,DENSE_RANK() over(ORDER BY 总成绩 desc ) from a; +-- 23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 +-- 24. 查询各科成绩前三名的记录 +with aa as +(SELECT student.student_id,student.student_name,student.birthday,student.gender,score.course_id,score, DENSE_RANK() over (PARTITION BY score.course_id order by score.score desc) 排名 FROM student,score WHERE student.student_id = score.student_id) +select * from aa where 排名<=3; +-- 25. 查询每门课程被选修的学生数 +select distinct course.course_id,course_name,count(student.student_id) over (partition by score.course_id) 学生数 from student,score,course where student.student_id=score.student_id and score.course_id=course.course_id; +-- 26. 查询出只选修两门课程的学生学号和姓名 +WITH a as +(SELECT DISTINCT s.student_id,s.student_name,COUNT(c.course_id) over(PARTITION by s.student_id) 课程数量 from student s,course c,score r WHERE s.student_id=r.student_id and c.course_id=r.course_id) +SELECT * from a WHERE 课程数量=2; +-- 27. 查询男生、女生人数 +SELECT DISTINCT gender,COUNT(*) over(PARTITION by gender) from student; +-- 28. 查询名字中含有「风」字的学生信息 +SELECT * from student where student_name like '%风%'; +-- 29. 查询同名同性学生名单,并统计同名人数 +-- 30. 查询 1990 年出生的学生名单 +SELECT * from student WHERE YEAR(birthday)=1990; +-- 31. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 +WITH a as +(SELECT DISTINCT c.course_name,c.course_id,avg(score) over(PARTITION by c.course_id ) 平均成绩 from score s,course c WHERE s.course_id=c.course_id) +SELECT *,ROW_NUMBER() over(order by 平均成绩 desc),ROW_NUMBER() over(order by a.course_id) FROM a; + +-- 32. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +with a as +(SELECT DISTINCT s.student_id,s.student_name,avg(c.score) over(PARTITION by s.student_id) 平均成绩 from student s, score c WHERE s.student_id=c.student_id) +SELECT * FROM a where 平均成绩>85; +-- 33. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +SELECT c.course_name, s.student_name,r.score from student s,course c,score r WHERE s.student_id=r.student_id and c.course_id=r.course_id and c.course_name='数学' and r.score<60; +-- 34. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +SELECT * from student s left JOIN score r on s.student_id= r.student_id left JOIN course c on c.course_id=r.course_id; +-- 35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +SELECT s.student_name,c.course_name,r.score from student s,score r,course c WHERE s.student_id=r.student_id and c.course_id=r.course_id and score>70 ORDER BY c.course_id; +-- 36. 查询不及格的课程 +SELECT DISTINCT c.course_name from student s,course c,score r WHERE s.student_id=r.student_id and c.course_id=r.course_id and score<60; +-- 37. 查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 +SELECT s.student_id,s.student_name from student s,score r WHERE s.student_id=r.student_id and course_id=1 and r.score>=80; +-- 38. 求每门课程的学生人数 +SELECT DISTINCT c.course_name,COUNT(s.student_id) over(PARTITION by c.course_id) from student s, course c,score r WHERE s.student_id=r.student_id and c.course_id=r.course_id; +-- 39. 成绩不重复,查询选修「Trump」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select * from student,score where student.student_id=score.student_id and score=( +select max(score) from student,score,course,teacher where student.student_id=score.student_id and score.course_id=course.course_id and course.teacher_id=teacher.teacher_id and teacher_name='Trump'); +-- 40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select * from student,score,course,teacher where student.student_id=score.student_id and score.course_id=course.course_id and course.teacher_id=teacher.teacher_id and score=( +select max(score) from student,score,course,teacher where student.student_id=score.student_id and score.course_id=course.course_id and course.teacher_id=teacher.teacher_id and teacher_name='李白') and teacher_name='李白'; +-- 41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 +with bb as +(with aa as +(select distinct student.student_id,score.course_id,score,count(student.student_id) over (partition by score ) 课程成绩相同个数 from student,score where student.student_id=score.student_id) +select *,count(student_id) over(partition by student_id) 学生编号个数 from aa where 课程成绩相同个数 >=2) +select bb.student_id,bb.course_id,bb.score from bb where 学生编号个数>=2; + +-- 42. 查询每门功成绩最好的前两名 +with aa as +(SELECT student.student_id,student.student_name,student.birthday,student.gender,score.course_id,score, DENSE_RANK() over (PARTITION BY score.course_id order by score.score desc) 排名 FROM student,score WHERE student.student_id = score.student_id) +select * from aa where 排名<=2; +-- 43. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +with aa as +(select distinct course_id,count(student.student_id) over (partition by course_id) 学生选修人数 from student,score where student.student_id=score.student_id) +select * from aa where 学生选修人数>5; +-- 44. 检索至少选修两门课程的学生学号 +WITH a as +(SELECT DISTINCT s.student_id,COUNT(c.course_id) over(PARTITION by s.student_id) 课程数量 from student s,course c,score r WHERE s.student_id=r.student_id and c.course_id=r.course_id) +SELECT * from a WHERE 课程数量>=2; +-- 45. 查询选修了全部课程的学生信息 +WITH a as +(SELECT DISTINCT s.student_id,s.student_name,s.gender,s.birthday,COUNT(c.course_id) over(PARTITION by s.student_id) 课程数量 from student s,course c,score r WHERE s.student_id=r.student_id and c.course_id=r.course_id) +SELECT * from a WHERE 课程数量=3; +-- 46. 查询各学生的年龄,只按年份来算 +select *,datediff(NOW(),birthday)/365 from student; +-- 47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + +-- 48. 查询本周过生日的学生 +select week(now(),1) +select *,week(birthday,1) from student where week(birthday,1)=week(NOW(),1); +-- 49. 查询下周过生日的学生 +select week(now(),1) +select *,week(birthday,1) from student where week(birthday,1)=week(NOW(),1)+1; +-- 50. 查询本月过生日的学生 +select *,MONTH(birthday) from student where MONTH(birthday) =MONTH(NOW()); +-- 51. 查询下月过生日的学生 +select *,MONTH(birthday) from student where MONTH(birthday) =MONTH(NOW())+1; + +``` + -- Gitee