From dc577abe7af1b0d654707808bbff6860cb57f71f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=97=BB=E7=A5=A5?= <18396581747@163.com> Date: Tue, 24 Oct 2023 03:48:24 +0000 Subject: [PATCH] =?UTF-8?q?21=20=E5=91=A8=E9=97=BB=E7=A5=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周闻祥 <18396581747@163.com> --- ...0 \345\244\215\344\271\240\351\242\230.md" | 197 ++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 "21 \345\221\250\351\227\273\347\245\245/20231020 \345\244\215\344\271\240\351\242\230.md" diff --git "a/21 \345\221\250\351\227\273\347\245\245/20231020 \345\244\215\344\271\240\351\242\230.md" "b/21 \345\221\250\351\227\273\347\245\245/20231020 \345\244\215\344\271\240\351\242\230.md" new file mode 100644 index 0000000..165b835 --- /dev/null +++ "b/21 \345\221\250\351\227\273\347\245\245/20231020 \345\244\215\344\271\240\351\242\230.md" @@ -0,0 +1,197 @@ +# 作业 + +```MYSQL +-- 1. 查询" 1 "课程比" 2 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 +WITH +a as (SELECT student.*,score.course_id,score.score from student,score where student.student_id=score.student_id), +b as (SELECT student.*,score.course_id,score.score from student,score where student.student_id=score.student_id) +SELECT a.student_id,a.student_name,a.gender,a.birthday,a.course_id,a.score,b.course_id,b.score from a,b WHERE a.student_id=b.student_id and a.course_id=1 and b.course_id=2 and a.score > b.score; +-- 2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 +WITH +a as (SELECT student.*,score.course_id,score.score from student,score where student.student_id=score.student_id), +b as (SELECT student.*,score.course_id,score.score from student,score where student.student_id=score.student_id) +SELECT a.student_id,a.student_name,a.gender,a.birthday from a,b WHERE a.student_id=b.student_id and a.course_id=1 and b.course_id=2; +-- 3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) +SELECT a.*,b.course_id from +(SELECT student.*,score.course_id,score.score from student,score where student.student_id=score.student_id and score.course_id=1) a +LEFT JOIN +(SELECT student.*,score.course_id,score.score from student,score where student.student_id=score.student_id and score.course_id=2) b +on a.student_id=b.student_id; +-- 4. 查询不存在" 1 "课程但存在" 2 "课程的情况 +SELECT a.*,b.course_id from +(SELECT student.*,score.course_id,score.score from student,score where student.student_id=score.student_id and score.course_id=2) a +LEFT JOIN +(SELECT student.*,score.course_id,score.score from student,score where student.student_id=score.student_id and score.course_id=1) b +on a.student_id=b.student_id; +-- 5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 +SELECT * from (SELECT DISTINCT student.*,avg(score) over (PARTITION by student_id) 平均成绩 from student,score where student.student_id=score.student_id) a WHERE 平均成绩 >= 60; +-- 6. 查询在成绩表存在成绩的学生信息 +SELECT * from student WHERE student_id in (SELECT student_id from score); +-- 7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +SELECT DISTINCT student.*,count(*) over (PARTITION by student_id),sum(score) over (PARTITION by student_id) from student +left JOIN +score on student.student_id=score.student_id; +-- 8. 查询「李」姓老师的数量 +SELECT count(*) from teacher where teacher_name like '李%%'; +-- 9. 查询学过「李白」老师授课的同学的信息 +SELECT * from student where student_id in +(SELECT student_id from teacher,course,score WHERE teacher.teacher_id=course.teacher_id and course.course_id=score.course_id and teacher_name = '李白'); +-- 10. 查询没有学全所有课程的同学的信息 +SELECT * from student WHERE student_id not in ( +SELECT DISTINCT student_id from +(SELECT *,count(*) over (PARTITION by student_id) xuk from score) a +WHERE xuk=(SELECT count(*) from course)); +-- 11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 +SELECT * from student,score WHERE student.student_id=score.student_id and course_id in( +SELECT score.course_id from score where student_id=1 ); +-- 12. 查询和" 1 "号的同学学习的课程完全相同的其他同学的信息 +SELECT * from student WHERE student_id in ( +SELECT DISTINCT student_id from +(SELECT *,count(*) over(PARTITION by student_id) co,sum(course_id) over (PARTITION by student_id) su from score) a +WHERE +co=(SELECT count(*) from score WHERE student_id=1) +and +su=(SELECT sum(course_id) from score WHERE student_id=1)) +and student_id != 1; +-- 13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 +SELECT * from student where student_id not in +(SELECT student_id from teacher,course,score WHERE teacher.teacher_id=course.teacher_id and course.course_id=score.course_id and teacher_name = '李白'); +-- 14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +SELECT * from (SELECT DISTINCT student.*,avg(score) over (PARTITION by student_id) 平均成绩 from student,score where student.student_id=score.student_id) a +WHERE student_id in +(SELECT DISTINCT student_id from (SELECT *,count(*) over (PARTITION by student_id) csc from score where score<60) a WHERE csc >=2); +-- 15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 +SELECT student.* from student,score WHERE student.student_id=score.student_id and course_id=1 and score < 60; +-- 16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +SELECT *,avg(score) over (PARTITION by score.student_id) 平均成绩 from student,score where student.student_id=score.student_id ORDER BY 平均成绩; +-- 17. 查询各科成绩最高分、最低分和平均分 +SELECT DISTINCT course_id,max(score) over (PARTITION by course_id),min(score) over (PARTITION by course_id),avg(score) over (PARTITION by course_id) from score; +-- 18. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 +SELECT DISTINCT course_id,(SELECT course_name FROM course WHERE course_id=a.course_id), +count(case +when score>=90 then 1 +else null +end) over (PARTITION by course_id), +count(case +when score<90 and score>=80 then 1 +else null +end) over (PARTITION by course_id), +count(case +when score<80 and score>=60 then 1 +else null +end) over (PARTITION by course_id), +count(case +when score<60 then 1 +else null +end) over (PARTITION by course_id) +from score a; +-- 19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 +SELECT course_id,score,RANK() over(PARTITION by course_id ORDER BY score) FROM score; + +-- 20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 +SELECT course_id,score,DENSE_RANK() over(PARTITION by course_id ORDER BY score) FROM score; + +-- 21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +SELECT 总成绩,rank() over(ORDER BY 总成绩) FROM ( +SELECT sum(score) 总成绩 FROM score GROUP BY student_id) a; + +-- 22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + SELECT 总成绩,dense_rank() over(ORDER BY 总成绩) FROM ( +SELECT sum(score) 总成绩 FROM score GROUP BY student_id) a; +-- 23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 +SELECT course_name,A.* FROM course,( +SELECT +course_id, +count(*) 人数, +sum((CASE WHEN score>=85 and score<=100 THEN 1 END)) `[100-85]`, +sum((CASE WHEN score>=70 and score<85 THEN 1 END)) `[85-70]`, +sum((CASE WHEN score>=60 and score<70 THEN 1 END)) `[70-60]`, +sum((CASE WHEN score>=0 and score<60 THEN 1 END)) `[60-0]`, +sum((CASE WHEN score>=85 and score<=100 THEN 1 END))/count(*), +sum((CASE WHEN score>=70 and score<85 THEN 1 END))/count(*), +sum((CASE WHEN score>=60 and score<75 THEN 1 END))/count(*), +sum((CASE WHEN score>=0 and score<60 THEN 1 END))/count(*) + FROM score GROUP BY course_id) A WHERE A.course_id=course.course_id; +-- 24. 查询各科成绩前三名的记录 +SELECT * FROM ( +SELECT *,rank() over(PARTITION by course_id ORDER BY score desc) 排名 FROM score ) a WHERE 排名<=3 +-- 25. 查询每门课程被选修的学生数 +SELECT *,count(student_id) over(PARTITION by course_id) FROM score; +-- 26. 查询出只选修两门课程的学生学号和姓名 +SELECT * FROM student s,( +SELECT student_id,count(course_id) 选修 FROM score GROUP BY student_id HAVING 选修=2) a WHERE s.student_id= a.student_id; +-- 27. 查询男生、女生人数 +SELECT gender,count(*) FROM student GROUP BY gender; +-- 28. 查询名字中含有「风」字的学生信息 +SELECT * FROM student WHERE student_name like '%风%'; +-- 29. 查询同名同性学生名单,并统计同名人数 +SELECT count(*) FROM student a,student b WHERE a.student_name=b.student_name and a.gender=b.gender and a.student_id!=b.student_id; +-- 30. 查询 1990 年出生的学生名单 +SELECT * FROM student WHERE year(birthday) = 1990; +-- 31. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 +SELECT avg(score) 平均成绩 FROM score GROUP BY course_id ORDER BY 平均成绩 desc,course_id asc; +-- 32. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +SELECT * FROM student s,( +SELECT student_id,avg(score) 平均成绩 FROM score GROUP BY student_id HAVING 平均成绩>=85) a WHERE s.student_id=a.student_id; +-- 33. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +SELECT * FROM student WHERE student_id in( +SELECT student_id FROM score WHERE course_id=( +SELECT course_id FROM course WHERE course_name='数学') and score<60); +-- 34. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +SELECT DISTINCT * FROM student s1 LEFT JOIN score on s1.student_id=score.student_id LEFT JOIN course on score.course_id=course.course_id; +-- 35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +SELECT * FROM student s1,( +SELECT student_id,score,course_id FROM score WHERE score>70) a,course c WHERE s1.student_id=a.student_id and c.course_id=a.course_id; +-- 36. 查询不及格的课程 +SELECT * FROM score WHERE score<60; +-- 37. 查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 +SELECT * FROM student WHERE student_id in ( +SELECT student_id FROM score WHERE score>80 and course_id=1); +-- 38. 求每门课程的学生人数 +SELECT count(*) FROM score GROUP BY course_id; +-- 39. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +SELECT * FROM student s1,score s2 WHERE s1.student_id=s2.student_id and score=( +SELECT max(score) FROM score WHERE course_id=( +SELECT DISTINCT course_id FROM course WHERE teacher_id=( +SELECT teacher_id FROM teacher WHERE teacher_name='张三'))); +-- 40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +SELECT * FROM student s1,score s2 WHERE s1.student_id=s2.student_id and score=( +SELECT max(score) FROM score WHERE course_id=( +SELECT course_id FROM course WHERE teacher_id=( +SELECT teacher_id FROM teacher WHERE teacher_name='张三'))); +-- 41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 +SELECT * FROM student s1,score s2 WHERE s1.student_id=s2.student_id and s1.student_id in ( +SELECT a.student_id FROM score a,score b WHERE a.student_id=b.student_id and a.score=b.score and a.course_id!=b.course_id); +-- 42. 查询每门功成绩最好的前两名 +with a as ( +SELECT student_id,course_id,rank() over(PARTITION by course_id ORDER BY score) 排名 FROM score) +SELECT * FROM a WHERE 排名<=2; +-- 43. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +SELECT course_id,count(*) 选修人数 FROM score GROUP BY course_id HAVING 选修人数 >5 +-- 44. 检索至少选修两门课程的学生学号 +SELECT student_id,count(course_id) 选修 FROM score GROUP BY student_id HAVING 选修 >=2; +-- 45. 查询选修了全部课程的学生信息 +SELECT * FROM student s,( +SELECT student_id,count(course_id) 课程 FROM score GROUP BY student_id HAVING 课程=(SELECT count(*) FROM course)) a WHERE s.student_id=a.student_id; +-- 46. 查询各学生的年龄,只按年份来算 +SELECT student_name,year(NOW())-year(birthday) 年龄 FROM student; +-- 47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 +SELECT student_name,birthday,IF(month(birthday)>MONTH(NOW()),IF(day(birthday)>day(NOW()),year(NOW())-year(birthday),year(NOW())-year(birthday)-1),year(NOW())-year(birthday)-1) FROM student; +-- 48. 查询本周过生日的学生 +with a as ( +SELECT WEEK(birthday,1),student_name 生日周 FROM student) +SELECT * FROM a WHERE a.生日周=(SELECT week(NOW(),1)); +-- 49. 查询下周过生日的学生 +with a as ( +SELECT WEEK(birthday,1),student_name 生日周 FROM student) +SELECT * FROM a WHERE a.生日周=(SELECT week(NOW(),1)+1); +-- 50. 查询本月过生日的学生 +with a as ( +SELECT student_name,MONTH(birthday) 生日月 FROM student) +SELECT * FROM a WHERE 生日月=month(NOW()); +-- 51. 查询下月过生日的学生 +with a as ( +SELECT student_name,MONTH(birthday) 生日月 FROM student) +SELECT * FROM a WHERE 生日月=month(NOW()+1); +``` + -- Gitee