diff --git "a/09 \351\231\210\345\277\227\345\213\207/20231020\347\273\217\345\205\270\344\272\224\345\215\201\351\242\230.md" "b/09 \351\231\210\345\277\227\345\213\207/20231020\347\273\217\345\205\270\344\272\224\345\215\201\351\242\230.md" new file mode 100644 index 0000000000000000000000000000000000000000..e6c51f47cd490e63c0913ac9efc149dfbc9081d4 --- /dev/null +++ "b/09 \351\231\210\345\277\227\345\213\207/20231020\347\273\217\345\205\270\344\272\224\345\215\201\351\242\230.md" @@ -0,0 +1,170 @@ +# 作业 + + + +```sql +-- 1. 查询" 1 "课程比" 2 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 + +select * from student,score,if(score>) where student.student_id=score.student_id ; +select * from student,score where student.student_id=score.student_id and course_id=1; +select * from student,score where student.student_id=score.student_id and course_id=2; + +select * from (select * from student,score where student.student_id=score.student_id and course_id=1) li,(select * from student,score where student.student_id=score.student_id and course_id=2) jun where li.student_id=jun.student_id; + + +-- 2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 + +select * from student where student_id in (select student_id from score where course_id=1) and student_id in (select student_id from score where course_id=2); + + +-- 3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) + +select * from (select student_id from score where course_id=1) li left join (select student_id from score where course_id=2) jun on li.student_id=jun.student_id; +-- 4. 查询不存在" 1 "课程但存在" 2 "课程的情况 + +select * from (select student_id from score where course_id=1) li right join (select student_id from score where course_id=2) jun on li.student_id=jun.student_id; + +-- 5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 +select * from ( +select DISTINCT student.student_id,student.student_name,avg(score) over(partition by student.student_id) yang from student,score where student.student_id=score.student_id) lijunyang where yang>=60; + +-- 6. 查询在成绩表存在成绩的学生信息 + +select * from student left join score on score.student_id=student.student_id where score is not null; + +-- 7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select DISTINCT student.student_id,student_name,count(course_id) over(partition by student_id) li ,sum(score) over(partition by student_id)jun from student left join score on student.student_id=score.student_id; + +-- 8. 查询「李」姓老师的数量 + +with aa as (select * from student), +bb as (select * from score), +cc as (select * from course), +dd as (select * from teacher) +select count(teacher_name) from aa left join bb on aa.student_id=bb.student_id left join cc on cc.course_id=bb.course_id left join dd on cc.teacher_id=dd.teacher_id where student_name like '李%'; + + +-- 9. 查询学过「李白」老师授课的同学的信息 + +with aa as (select * from student), +bb as (select * from score), +cc as (select * from course), +dd as (select * from teacher) +select aa.* from aa left join bb on aa.student_id=bb.student_id left join cc on cc.course_id=bb.course_id left join dd on cc.teacher_id=dd.teacher_id where teacher_name = '李白'; + + +-- 10. 查询没有学全所有课程的同学的信息 +with aa as (select * from student), +bb as (select * from score), +cc as (select * from course), +dd as (select * from teacher) +select aa.* from aa left join bb on aa.student_id=bb.student_id left join cc on cc.course_id=bb.course_id left join dd on cc.teacher_id=dd.teacher_id where score is null; + + +-- 11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 + +select DISTINCT student.* 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 "号的同学学习的课程完全相同的其他同学的信息 + +select * from ( +with aa as (select * from student), +bb as (select * from score) +select DISTINCT aa.student_id,student_name,sum(bb.course_id) over(partition by aa.student_id) li,count(bb.course_id) over(partition by aa.student_id) jun from aa,bb where aa.student_id=bb.student_id) lijunyang where li=(select DISTINCT sum(course_id) over(partition by score.student_id) from student,score where score.student_id=student.student_id and score.student_id=1) and jun=(select DISTINCT count(course_id) over(partition by score.student_id) from student,score where score.student_id=student.student_id and score.student_id=1) and student_id !=1; + +-- 13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 +select student_name from student,score where student.student_id=score.student_id and score.course_id in (select course_id from course,teacher where course.teacher_id=teacher.teacher_id and teacher_name='李白'); + +-- 14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + +select DISTINCT student.student_id,student_name,avg(score) over(partition by student_name) from score,student where score.student_id=student.student_id and score<60 and score.student_id in (select student_id from( +select DISTINCT student_id,count(course_id) over(partition by student_id) from score)li); + +-- 15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 + +select * from score where course_id=1 and score<60 order by score desc; + +-- 16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select * from( +select DISTINCT student_name,avg(score) over(partition by score.student_id ) avgg from score,student where student.student_id=score.student_id) li order by avgg desc; + +-- 17. 查询各科成绩最高分、最低分和平均分 + +select DISTINCT course_name,max(score) over(partition by course.course_name) 最高分,min(score) over(partition by course.course_name) 最低分,avg(score) over(partition by course.course_name) 平均分 from score,course where course.course_id=score.course_id; + + +-- 18. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + +SELECT + score.course_id, + course.course_name, + max( score.score ) 最高分, + min( score.score ) 最低分, + avg( score.score ) 平均分, + (SUM(IF(score.score >= 60, 1, 0)) / COUNT(*) * 100) 及格率, + (SUM(IF(score.score >= 70 and score.score<80, 1, 0)) / COUNT(*) * 100) 中等率, + (SUM(IF(score.score >= 80 and score.score<90, 1, 0)) / COUNT(*) * 100) 优良率, + (SUM(IF(score.score >= 90, 1, 0)) / COUNT(*) * 100) 优秀率, + count(score.student_id) 选课人数 +FROM + score, + course +WHERE + course.course_id = score.course_id +GROUP BY + score.course_id; + + + +-- 19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 + +SELECT + student_name, + course_name, + score, + rank() OVER (partition by course.course_id ORDER BY score.score DESC) +FROM + student, + score, + course +WHERE + student.student_id = score.student_id + AND score.course_id = course.course_id +ORDER BY + course.course_id,score DESC; + + +-- 20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 +-- 21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +-- 22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +-- 23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 +-- 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. 查询下月过生日的学生 +``` \ No newline at end of file