diff --git "a/53 \345\221\250\345\216\232\350\276\260/20231020 \347\254\254\344\272\214\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232 50\351\242\230/50\351\242\230.md" "b/53 \345\221\250\345\216\232\350\276\260/20231020 \347\254\254\344\272\214\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232 50\351\242\230/50\351\242\230.md" new file mode 100644 index 0000000000000000000000000000000000000000..632f891e5a683380cd38668073bb1148ac6c1a52 --- /dev/null +++ "b/53 \345\221\250\345\216\232\350\276\260/20231020 \347\254\254\344\272\214\345\215\201\345\233\233\346\254\241\344\275\234\344\270\232 50\351\242\230/50\351\242\230.md" @@ -0,0 +1,174 @@ +2023年10月23日 + +# 50题 + +## 答案 + +```mysql +-- ## 题目: +-- +-- 1. 查询" 1 "课程比" 2 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 +select ss1.student_id 学号,student_name 姓名,gender 性别,birthday 出生日期,score 课程分数 from student ss1,(select s1.student_id,s1.score from score s1,score s2 where s1.student_id=s2.student_id and s1.course_id=1 and s2.course_id =2 and s1.score>s2.score) ss2 where ss1.student_id=ss2.student_id; +-- 2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 +select t1.student_id 学号,student_name 姓名,gender 性别,birthday 出生日期 from student,(select student_id from score where course_id=1) t1,(select student_id from score where course_id=2) t2 where t1.student_id=t2.student_id and student.student_id=t1.student_id; +-- 3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) +select score.course_id 学生ID,case when score.course_id=t2.course_id then score.course_id when score.course_id=t3.course_id then score.course_id end 课程 from score LEFT JOIN (SELECT * from score where course_id=2) t2 on score.student_id=t2.student_id LEFT JOIN ( +select * from score where course_id=1) t3 on t2.student_id=t3.student_id where score.course_id!=3; +-- 4. 查询不存在" 1 "课程但存在" 2 "课程的情况 +SELECT * from score where course_id!=1 and course_id=2 ; +-- 5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 +with t as (SELECT t1.student_id,student_name,平均成绩 from student t1 ,(SELECT student_id,truncate(avg (score.score),2) 平均成绩 from score GROUP BY score.student_id HAVING avg(score)>60) t2 where t1.student_id=t2.student_id)select distinct * from t; +-- 6. 查询在成绩表存在成绩的学生信息 +select * from student where student_id in (select student_id from score); +-- 7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select s1.student_id,student_name,选课总数,总成绩 from student s1 LEFT JOIN (select distinct student_id,COUNT(course_id) over w 选课总数,SUM(score) over w 总成绩 from score window w as (PARTITION by student_id order by student_id)) s2 on s1.student_id=s2.student_id; +-- 8. 查询「李」姓老师的数量 +select count(teacher_id) from teacher where teacher_name like '李%'; +-- 9. 查询学过「李白」老师授课的同学的信息 +select student_id,student_name,gender,birthday from student where student_id in ( +SELECT student_id from score where course_id=(select course_id from course where teacher_id=(select teacher_id from teacher where teacher_name='李白'))); +-- 10. 查询没有学全所有课程的同学的信息 +select student_id,student_name,gender,birthday from student where student_id in(select student_id from (select student_id,count(s1.course_id) over (PARTITION by student_id) c from score s1)t1 where c !=(select count(course_id) from course)) ; +-- 11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 +select student_id,student_name,gender,birthday 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 * from score where course_id in (select course_id from score where student_id =1) ; +select * from score s1,score s2, score s3 where s1.course_id=s2.course_id and s2.course_id=s3.course_id and s1.course_id ; + +with t as (select * from score where course_id in (select course_id from score where student_id =1) and student_id!=1) +select distinct t1.* from t t1 ORDER BY student_id; + + + + + + + + + + + + +-- 13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 +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=(select teacher_id from teacher where teacher_name='李白'))); +-- 14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +select s.student_id,student_name,平均成绩 from student s ,(select student_id,ROUND(avg(score),1) 平均成绩 from score GROUP BY student_id) s2 where s.student_id=s2.student_id and s.student_id in (select student_id from score where score<60 GROUP BY student_id having count(course_id)>=2) ; +-- 15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 +select student_id,student_name,gender,birthday from student where student_id in (select student_id from Score where course_id=1 order by score desc); +-- 16. 按平均成绩从高到低 显示 所有学生的所有课程的成绩以及平均成绩 +select s1.*,s2.平均成绩 from score s1 ,(select student_id, ROUND(avg(score),1) 平均成绩 from score GROUP BY student_id) s2 where s1.student_id=s2.student_id ORDER BY 平均成绩 desc; +-- 17. 查询各科成绩最高分、最低分和平均分 +select distinct course_id,max(score) over w 最高分,min(score) over w 最低分,ROUND(avg(score) over w ,1) 平均分 from score window w as (partition by course_id); +/* 18. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率, +中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90), +求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列*/ +SELECT distinct s.course_id `课程ID`, +course_name `课程name`, +max(score) over w 最高分, +min(score) over w 最低分, +ROUND(avg(score) over w ,1) 平均分, +t1.`及格率`, +t2.`中等率`, +t3.`优良率`, +t4.`优秀率`, +s.course_id 课程号, +COUNT(s.course_id) over w 选修人数 +from score s,course, +( +SELECT distinct t1.course_id,concat(round( t1.被除数 / t2.人数 *100,2),'%') 及格率 from +(SELECT course_id, COUNT(score) over (PARTITION by course_id ) 被除数 from score where score>=60) t1,(SELECT distinct course_id, COUNT(score) over (PARTITION by course_id ) 人数 from score) t2) t1,( + +SELECT distinct t1.course_id,concat(round( t1.被除数 / t2.人数 *100,2),'%') 中等率 from +(SELECT course_id, COUNT(score) over (PARTITION by course_id ) 被除数 from score where score BETWEEN 70 and 80) t1,(SELECT distinct course_id, COUNT(score) over (PARTITION by course_id ) 人数 from score) t2) t2,( + +SELECT distinct t1.course_id,concat(round( t1.被除数 / t2.人数 *100,2),'%') 优良率 from +(SELECT course_id, COUNT(score) over (PARTITION by course_id ) 被除数 from score where score BETWEEN 80 and 90) t1,(SELECT distinct course_id, COUNT(score) over (PARTITION by course_id ) 人数 from score) t2) t3,( + +SELECT distinct t1.course_id,concat(round( t1.被除数 / t2.人数 *100,2),'%') 优秀率 from +(SELECT distinct course_id, COUNT(score) over (PARTITION by course_id ) 被除数 from score where score >=90) t1,(SELECT distinct course_id, COUNT(score) over (PARTITION by course_id ) 人数 from score) t2) t4 +where s.course_id=course.course_id and t1.course_id=t2.course_id and t2.course_id=t3.course_id and t3.course_id=t4.course_id and t1.course_id=s.course_id window w as (partition by s.course_id)ORDER BY 选修人数 desc,s.course_id asc; +-- 19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 +SELECT + course_name 科, + dense_RANK() + over w 排名, + score +FROM + score + JOIN course ON score.course_id = course.course_id window w AS ( PARTITION BY score.course_id ORDER BY score desc ); +-- 20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 +SELECT + course_name 科, + RANK() + over w 排名, + score +FROM + score + JOIN course ON score.course_id = course.course_id window w AS ( PARTITION BY score.course_id ORDER BY score desc ); +-- 21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +with t as ( +select distinct +student.student_id, +student_name, +SUM(score) over (PARTITION by student.student_id) 总成绩 +from +score join student on score.student_id=student.student_id ) +select DENSE_RANK() over (ORDER BY 总成绩 desc) 排名,student_id 学号,student_name 学生姓名,总成绩 from t; +-- 22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +with t as ( +select distinct +student.student_id, +student_name, +SUM(score) over (PARTITION by student.student_id) 总成绩 +from +score join student on score.student_id=student.student_id ) +select RANK() over (ORDER BY 总成绩 desc) 排名,student_id 学号,student_name 学生姓名,总成绩 from t; +-- 23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 +select + score.course_id 课程编号, +course_name 课程名称, +t1.`N` `[100-85]`, +t2.`N` `[85-70]`, +t3.`N` `[70-60]`, +t4.`N` `[60-0]` +from score +join course on score.course_id=course.course_id +join (select distinct count(score) over (partition by course_id) n ,course_id from score where score between 85 and 100 ) t1 on t1.course_id=score.course_id +join (select distinct count(score) over (partition by course_id) n ,course_id from score where score between 70 and 85 ) t2 on t2.course_id=score.course_id +join (select distinct count(score) over (partition by course_id) n ,course_id from score where score between 60 and 70 ) t3 on t3.course_id=score.course_id +join (select distinct count(score) over (partition by course_id) n ,course_id from score where score<60) t4 on t4.course_id=score.course_id + +; + + +-- 24. 查询各科成绩前三名的记录 +-- 25. 查询每门课程被选修的学生数 +-- 26. 查询出只选修两门课程的学生学号和姓名 +-- 27. 查询男生、女生人数 +-- 28. 查询名字中含有「风」字的学生信息 +-- 29. 查询同名同性学生名单,并统计同名人数 +-- 30. 查询 1990 年出生的学生名单 +-- 31. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 +-- 32. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +-- 33. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +-- 34. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +-- 35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +-- 36. 查询不及格的课程 +-- 37. 查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 +-- 38. 求每门课程的学生人数 +-- 39. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +-- 40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +-- 41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 +-- 42. 查询每门功成绩最好的前两名 +-- 43. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +-- 44. 检索至少选修两门课程的学生学号 +-- 45. 查询选修了全部课程的学生信息 +-- 46. 查询各学生的年龄,只按年份来算 +-- 47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 +-- 48. 查询本周过生日的学生 +-- 49. 查询下周过生日的学生 +-- 50. 查询本月过生日的学生 +-- 51. 查询下月过生日的学生 +``` +