From dc7dd5bd4013b46783fc55a33afee4b86af808f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=BD=E6=99=9F=E5=AE=B8?= <11785147+demon-cat-fruit-orange@user.noreply.gitee.com> Date: Sun, 22 Oct 2023 14:59:37 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 施晟宸 <11785147+demon-cat-fruit-orange@user.noreply.gitee.com> --- ...\346\215\256\345\272\22350\351\242\230.md" | 307 ++++++++++++++++++ 1 file changed, 307 insertions(+) create mode 100644 "22 \346\226\275\346\231\237\345\256\270/20231020\346\225\260\346\215\256\345\272\22350\351\242\230.md" diff --git "a/22 \346\226\275\346\231\237\345\256\270/20231020\346\225\260\346\215\256\345\272\22350\351\242\230.md" "b/22 \346\226\275\346\231\237\345\256\270/20231020\346\225\260\346\215\256\345\272\22350\351\242\230.md" new file mode 100644 index 0000000..86cd949 --- /dev/null +++ "b/22 \346\226\275\346\231\237\345\256\270/20231020\346\225\260\346\215\256\345\272\22350\351\242\230.md" @@ -0,0 +1,307 @@ +```sql +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 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 +SELECT +DISTINCT + b.student_id, + b.student_name, + b.gender, + b.birthday, + b.score + from +(SELECT + stu.student_id, + stu.student_name, + stu.gender, + stu.birthday, + s.score +FROM + student stu + JOIN score s + JOIN course cou ON stu.student_id = s.student_id + AND s.course_id = cou.course_id WHERE s.course_id=1) a + JOIN + (SELECT + stu.student_id, + stu.student_name, + stu.gender, + stu.birthday, + s.score +FROM + student stu + JOIN score s + JOIN course cou ON stu.student_id = s.student_id + AND s.course_id = cou.course_id WHERE s.course_id=2) b + where a.student_id=b.student_id and a.score>b.score; + +-- 2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 +SELECT +DISTINCT + a.student_id, + a.student_name, + a.birthday, + a.gender + from +(SELECT + cou.course_id, + stu.student_id, + stu.student_name, + stu.gender, + stu.birthday, + s.score +FROM + student stu + JOIN score s + JOIN course cou ON stu.student_id = s.student_id + AND s.course_id = cou.course_id WHERE cou.course_id=1 or cou.course_id=2) a + JOIN + (SELECT + cou.course_id, + stu.student_id, + stu.student_name, + stu.gender, + stu.birthday, + s.score + +FROM + student stu + JOIN score s + JOIN course cou ON stu.student_id = s.student_id + AND s.course_id = cou.course_id WHERE cou.course_id=1 or cou.course_id=2) b + where a.course_id=1 and b.course_id=2 and a.student_id=b.student_id; + -- 3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) + select * +from (select student_id ,score from score where score.course_id='01')as t1 left join (select student_id ,score from score where score.course_id='02') as t2 +on t1.student_id=t2.student_id 不会 +-- 4. 查询不存在" 1 "课程但存在" 2 "课程的情况 +select * +from score +where score.student_id not in (select student_id from score where score.course_id='01') +and score.course_id='02' 不会 +-- 5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 +SELECT student.student_id,student_name,avg(score) FROM student RIGHT JOIN score on student.student_id=score.student_id GROUP BY student.student_id HAVING avg(score)>60 +-- 6. 查询在成绩表存在成绩的学生信息 +SELECT DISTINCT student.* FROM student RIGHT JOIN score on student.student_id=score.student_id WHERE score is not null +-- 7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +SELECT student.student_id,student_name,count(course_id),sum(score) FROM student LEFT JOIN score on student.student_id=score.student_id GROUP BY student.student_id ; +-- 8. 查询「李」姓老师的数量 +SELECT count(teacher_id) FROM teacher WHERE teacher_name like '李%' +-- 9. 查询学过「李白」老师授课的同学的信息 +SELECT * FROM student WHERE student_id in(SELECT student_id FROM score WHERE course_id in(SELECT course_id FROM course WHERE teacher_id in(SELECT teacher_id FROM teacher WHERE teacher_name='李白' +))) +-- 10. 查询没有学全所有课程的同学的信息 +SELECT student.*,COUNT(course_id) FROM score RIGHT JOIN student on score.student_id=student.student_id GROUP BY student.student_id HAVING COUNT(course_id)<(SELECT count(*) FROM course +) + + +SELECT * FROM student WHERE student_id not in(SELECT student_id FROM score WHERE course_id=1 and student_id in(SELECT student_id FROM score WHERE course_id=2) and student_id in (SELECT student_id FROM score WHERE course_id=3)) +-- 11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 +SELECT * FROM student WHERE student_id in(SELECT student_id FROM score WHERE course_id in(SELECT course_id FROM score WHERE student_id=1 +)) +-- 12. 查询和" 1 "号的同学学习的课程完全相同的其他同学的信息 +SELECT student_id,course_id FROM score WHERE student_id=1 +SELECT * FROM score WHERE course_id in(SELECT course_id FROM score WHERE student_id=1 +) and course_id=2 and +-- 13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 +SELECT course_id FROM course WHERE teacher_id in(SELECT teacher_id FROM teacher WHERE teacher_name='李白') +SELECT student_id FROM score WHERE course_id =(SELECT course_id FROM course WHERE teacher_id in(SELECT teacher_id FROM teacher WHERE teacher_name='李白')) + +SELECT student_name FROM student WHERE student_id not in(SELECT student_id FROM score WHERE course_id =(SELECT course_id FROM course WHERE teacher_id in(SELECT teacher_id FROM teacher WHERE teacher_name='李白')) +) +-- 14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +SELECT student.student_id,student_name,AVG(score) FROM score left JOIN student on score.student_id=student.student_id WHERE score<60 GROUP BY student.student_id HAVING count(*)>=2 + + +-- 15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 +SELECT student_id FROM score WHERE course_id=1 and score<60 ORDER BY score desc +SELECT * FROM student WHERE student_id in(SELECT student_id FROM score WHERE course_id=1 and score<60 ORDER BY score desc +) +-- 16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +SELECT avg(score) FROM student RIGHT JOIN score on student.student_id=score.student_id GROUP BY student.student_id ORDER BY avg(score) desc +SELECT student_id,AVG(SCORE) FROM score GROUP BY student_id +-- 17. 查询各科成绩最高分、最低分和平均分 +SELECT max(score),min(score),avg(score) FROM score GROUP BY course_id; +-- 18. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 +select score.course_id ,max(score.score)as 最高分,min(score.score)as 最低分,AVG(score.score)as 平均分,count(*)as 选修人数,sum(case when score.score>=60 then 1 else 0 end )/count(*)as 及格率,sum(case when score.score>=70 and score.score<80 then 1 else 0 end )/count(*)as 中等率,sum(case when score.score>=80 and score.score<90 and score.score<80 then 1 else 0 end )/count(*)as 优良率,sum(case when score.score>=90 then 1 else 0 end )/count(*)as 优秀率 不会 +from score +GROUP BY score.course_id +ORDER BY count(*)DESC,score.course_id asc +-- 19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 + select student_id,course_id,score,DENSE_RANK() over (PARTITION by course_id ORDER BY score desc)from score 不会 +-- 20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 +select student_id,course_id,score,DENSE_RANK() over (PARTITION by course_id ORDER BY score desc)from score; 不会 +-- 21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +SELECT student_id, SUM(score) 总成绩,RANK() over(ORDER BY SUM(score) DESC) 排名 from score GROUP BY student_id; 不会 +-- 22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +SELECT student_id, SUM(score) 总成绩,dense_RANK() over(ORDER BY SUM(score) DESC) 排名 from score GROUP BY student_id; 不会 + +-- 23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 +SELECT + course.course_id, + course.course_name, + CASE + WHEN score >= 85 THEN '[100-85]' + WHEN score >= 70 THEN '[85-70]' + WHEN score >= 60 THEN '[70-60]' + ELSE '[60-0]' +END +FROM + score, + course +WHERE + score.course_id = course.course_id ;不会 +-- 24. 查询各科成绩前三名的记录 +select * +from score as t1 +where (select count(*) from score as t2 where t1.course_id=t2.course_id and t2.score >t1.score)<3 +ORDER BY t1.course_id +-- 25. 查询每门课程被选修的学生数 +SELECT course_name,count(student_id) FROM score LEFT JOIN course on score.course_id=course.course_id GROUP BY course_name +-- 26. 查询出只选修两门课程的学生学号和姓名 +SELECT student.student_id,student.student_name FROM student LEFT JOIN score on student.student_id=score.student_id GROUP BY student.student_id HAVING count(course_id)=2 ; +-- 27. 查询男生、女生人数 +SELECT gender,count(student_id) FROM student GROUP BY gender +-- 28. 查询名字中含有「风」字的学生信息 +SELECT * FROM student WHERE student_name like '%风%' +-- 29. 查询同名同性学生名单,并统计同名人数 +SELECT * FROM student s1,st +-- 30. 查询 1990 年出生的学生名单 +SELECT * FROM student WHERE LEFT(birthday,5)=1990 +-- 31. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 +SELECT score.course_id,course_name,avg(score) FROM score LEFT JOIN course on score.course_id=course.course_id GROUP BY score.course_id ORDER BY avg(score) desc,score.course_id asc +-- 32. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +SELECT student.student_id,student_name,avg(score) FROM score LEFT JOIN student on score.student_id=student.student_id GROUP BY student_id HAVING avg(score)>85 +-- 33. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +SELECT course_name,student_name,score FROM score LEFT JOIN student on score.student_id=student.student_id LEFT JOIN course on score.course_id=course.course_id WHERE course_name='数学' and score<60; +-- 34. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +SELECT student_name,course_name,score FROM score right JOIN student on score.student_id=student.student_id LEFT JOIN course on score.course_id=course.course_id +-- 35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +SELECT student_name,course_name,score FROM score right JOIN student on score.student_id=student.student_id LEFT JOIN course on score.course_id=course.course_id WHERE score>70 +-- 36. 查询不及格的课程 +SELECT student_name,course_name,score FROM score right JOIN student on score.student_id=student.student_id LEFT JOIN course on score.course_id=course.course_id WHERE score<60 +-- 37. 查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 +SELECT student.student_id,student_name FROM score right JOIN student on score.student_id=student.student_id WHERE course_id=1 and score>80 +-- 38. 求每门课程的学生人数 +SELECT course_name,COUNT(student.student_id) FROM score left JOIN student on score.student_id=student.student_id LEFT JOIN course on score.course_id=course.course_id GROUP BY score.course_id +-- 39. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +SELECT student.*,score FROM student left join score on student.student_id=score.student_id LEFT JOIN course on score.course_id=course.course_id LEFT JOIN teacher on course.teacher_id=teacher.teacher_id + WHERE teacher_name='李白' LIMIT 1 +-- 40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + +-- 41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 +select * +from score as t1 +where exists(select * from score as t2 where t1.student_id=t2.student_id and t1.course_id!=t2.course_id and t1.score =t2.score ) +-- 42. 查询每门功成绩最好的前两名 +select * +from score as t1 +where (select count(*) from score as t2 where t1.course_id=t2.course_id and t2.score >t1.score)<2 +ORDER BY t1.course_id +-- 43. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +SELECT course_id,count(student_id) FROM score GROUP BY course_id HAVING count(student_id)>5 +-- 44. 检索至少选修两门课程的学生学号 +SELECT student_id,count(course_id) FROM score GROUP BY student_id HAVING count(course_id)>=2 +-- 45. 查询选修了全部课程的学生信息 +SELECT count(*) FROM course +SELECT student.*,COUNT(course_id) FROM score JOIN student on score.student_id=student.student_id GROUP BY student.student_id HAVING COUNT(course_id)=(SELECT count(*) FROM course +) +-- 46. 查询各学生的年龄,只按年份来算 +select student.student_id,student.student_name,YEAR(NOW())-YEAR(birthday) from student; +-- 47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 +select student.student_id,student.student_name,TIMESTAMPDIFF(YEAR,student.birthday,CURDATE()) from student; +-- 48. 查询本周过生日的学生 +SELECT * FROM student WHERE WEEK(birthday) = WEEK(NOW()) +-- 49. 查询下周过生日的学生 +SELECT + * +FROM + student +WHERE WEEK(birthday) = WEEK(NOW()) + 1 +-- 50. 查询本月过生日的学生 +SELECT + * +FROM + student +WHERE MONTH(birthday) = MONTH(NOW()); +-- 51. 查询下月过生日的学生 +-- +SELECT + * +FROM + student +WHERE MONTH(birthday) = MONTH(NOW()) + 1 ; +``` -- Gitee