From a2d5a09138c8b183ca1bb383b426f203abc341a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=B6=E5=AD=A6=E5=AE=89?= <2820601363@qq.com> Date: Mon, 23 Oct 2023 18:00:33 +0800 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 --- ...01\347\273\203\344\271\240\351\242\230.md" | 566 ++++++++++++++++++ 1 file changed, 566 insertions(+) create mode 100644 "40 \346\227\266\345\255\246\345\256\211/\344\272\224\345\215\201\347\273\203\344\271\240\351\242\230.md" diff --git "a/40 \346\227\266\345\255\246\345\256\211/\344\272\224\345\215\201\347\273\203\344\271\240\351\242\230.md" "b/40 \346\227\266\345\255\246\345\256\211/\344\272\224\345\215\201\347\273\203\344\271\240\351\242\230.md" new file mode 100644 index 0000000..c0b2789 --- /dev/null +++ "b/40 \346\227\266\345\255\246\345\256\211/\344\272\224\345\215\201\347\273\203\344\271\240\351\242\230.md" @@ -0,0 +1,566 @@ +~~~ mysql + + +~~~ mysql + + + + +1.题目: + +-- 1. 查询" 1 "课程比" 2 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 +SELECT student_id, student_name, gender, birthday FROM student WHERE student_id IN (SELECT s1.student_idFROM (SELECT * FROM score WHERE course_id = 1) AS s1 + INNER JOIN + (SELECT * FROM score WHERE course_id = 2) AS s2 + ON s1.student_id = s2. student_id + WHERE s1.score > s2.score); + + + + + +-- 2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 +SELECT sc.student_id, student_name, birthday, gender +FROM ( + SELECT sc1.student_id + FROM (SELECT * FROM score WHERE course_id = 1) AS sc1 + INNER JOIN (SELECT * FROM score WHERE course_id = 2) AS sc2 + ON sc1.student_id = sc2.student_id) AS sc +LEFT JOIN student AS stu +ON sc.student_id= stu.student_id; + + + + + -- 3. 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null ) + SELECT *FROM (SELECT * FROM score WHERE course_id = 1) AS sc1 LEFT JOIN (SELECT * FROM score WHERE course_id = 2) AS sc2 ON sc1.student_id = sc2.student_id; + + + + + -- 4. 查询不存在" 01 "课程但存在" 02 "课程的情况 + SELECT * + FROM (SELECT * FROM score WHERE course_id = 1) AS sc1 + RIGHT JOIN (SELECT * FROM score WHERE course_id = 2) AS sc2 + ON sc1.student_id = sc2.student_id + WHERE sc1.score IS NULL; + + + + + -- 5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 + SELECT stu.student_id 学号, student_name 姓名, AVG(score) 平均成绩 + FROM student AS stu + INNER JOIN score AS sc + ON stu.student_id = sc.student_id + GROUP BY stu.student_id + HAVING AVG(score) >= 60; + + + + + -- 6. 查询在成绩表存在成绩的学生信息 + SELECT * FROM student WHERE student_id IN (SELECT DISTINCT student_id FROM score); + + + + + + -- 7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + SELECT stu.student_id, student_name, SUM(course_id) 选课总数, SUM(score) 总成绩 FROM student AS stu LEFT JOIN score AS sc ON stu.student_id = sc.student_id GROUP BY stu.student_id; + + + + + + -- 8. 查询「李」姓老师的数量 + SELECT COUNT(teacher_id) 李姓老师的数量 FROM teacher WHERE teacher_name LIKE '李%'; + + + + + + -- 9. 查询学过「李白」老师授课的同学的信息 + + SELECT stu.student_id, student_name, birthday, gender + FROM student AS stu + INNER JOIN score AS sc + ON stu.student_id = sc.student_id + WHERE course_id IN ( + SELECT course_id + FROM course + WHERE teacher_id IN ( + SELECT teacher_id FROM teacher WHERE teacher_name = '李白')); + + + + -- 10. 查询没有学全所有课程的同学的信息 + SELECT student_id, student_name, birthday, gender + FROM student + WHERE student_id IN ( + SELECT student_id + FROM score + GROUP BY student_id + HAVING COUNT(course_id) < (SELECT COUNT(course_id) FROM course)); + + + + + -- 11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 + select * from student where student_id in ( + select distinct student_id + from score s + inner join ( + select course_id from score where student_id = 1 + ) t1 + on s.course_id = t1.course_id + ); + + + -- 12. 查询和" 1 "号的同学学习的课程完全相同的其他同学的信息 + select * from student where student_id in ( + select student_id + from score s + inner join ( + select course_id from score where student_id = 1 + ) t1 + on s.course_id = t1.course_id + where student_id != 1 + group by student_id having count(*) = ( + select count(*) from score where student_id = 1 + ) + ); + + + + -- 13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 + select student_name + from student + where student_id not in ( + select student_id + from score sc + inner join ( + select course_id + from course c + inner join teacher t on c.teacher_id = t.teacher_id + where teacher_name = '李白' + ) t1 + on sc.course_id = t1.course_id + ); + + + + + -- 14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + select s.student_id,s.student_name,round(avg(score), 2) as avg_score + from student s + inner join ( + select student_id + from score + where score < 60 + group by student_id + having count(*) >= 2 + ) t1 + on s.student_id = t1.student_id + inner join score sc on s.student_id = sc.student_id + group by s.student_id; + + + + + -- 15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 + select s.*,score as score_01 + from student s + inner join ( + select student_id,score + from score + where course_id = 1 + and score < 60 + ) t1 + on s.student_id = t1.student_id + order by score_01 desc; + + + + + + + + -- 16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 + select + s.student_id as 学号,s.student_name as 姓名, + sum(case course_id when 1 then score else 0 end) as 语文, + sum(case course_id when 2 then score else 0 end) as 数学, + sum(case course_id when 3 then score else 0 end) as 英语, + ifnull(round(avg(score), 2), 0) as 平均成绩 + from student s + left join score sc on s.student_id = sc.student_id + group by s.student_id + order by 平均成绩 desc; + SELECT course_id, MAX(score) 最高分, MIN(score) 最低分, AVG(score) 平均分 FROM score GROUP BY course_id; + + + + + -- 17. 查询各科成绩最高分、最低分和平均分 + SELECT course_id, MAX(score) 最高分, MIN(score) 最低分, AVG(score) 平均分 FROM score GROUP BY course_id; + + + + + -- 18. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + select + c.course_id as 课程ID,c.course_name as 课程name, + max(score) as 最高分, + min(score) as 最低分, + round(avg(score), 2) as 平均分, + concat(round(sum(case when score >= 60 then 1 else 0 end) / count(*) * 100, 2), '%') as 及格率, + concat(round(sum(case when score between 70 and 80 then 1 else 0 end) / count(*) * 100, 2), '%') as 中等率, + concat(round(sum(case when score between 80 and 90 then 1 else 0 end) / count(*) * 100, 2), '%') as 优良率, + concat(round(sum(case when score >= 90 then 1 else 0 end) / count(*) * 100, 2), '%') as 优秀率 + from course c + inner join score s on c.course_id = s.course_id + group by c.course_id; + + + + + + + + -- 19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 + -- 20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 + -- 21. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + SET @crank = 0; + SELECT @crank := @crank + 1 AS 编号 ,q.student_id 学号, stu.student_name 名字,total 总计 +FROM ( + SELECT sc.student_id, SUM(sc.score) AS total + FROM score sc + GROUP BY sc.student_id + ORDER BY total DESC +) q + LEFT JOIN student stu + ON q.student_id = stu.student_id; + + -- 22. 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 + -- 23. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + select + c.course_id as 课程编号,c.course_name as 课程名称, + sum(case when score between 85 and 100 then 1 else 0 end) as '[100-85]', + concat(round(sum(case when score between 85 and 100 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比, + sum(case when score between 70 and 85 then 1 else 0 end) as '[85-70]', + concat(round(sum(case when score between 70 and 85 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比, + sum(case when score between 60 and 70 then 1 else 0 end) as '[70-60]', + concat(round(sum(case when score between 60 and 70 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比, + sum(case when score between 0 and 60 then 1 else 0 end) as '[0-60]', + concat(round(sum(case when score between 0 and 60 then 1 else 0 end) / count(*) * 100, 2), '%') as 百分比 + from course c + inner join score sc + on c.course_id = sc.course_id + group by c.course_id; + + + + -- 24. 查询各科成绩前三名的记录 + select c.course_id,c.course_name,s.student_id,s.student_name,score + from ( + select * + from score sc + where ( + select count(*) + from score sc1 + where sc.course_id = sc1.course_id + and sc.score < sc1.score + ) < 3 + ) + t1 + inner join student s on t1.student_id = s.student_id + inner join course c on t1.course_id = c.course_id + order by c.course_id,score desc; + + + + + + + + -- 25. 查询每门课程被选修的学生数 + select c.course_id 课程编号,c.course_name 课程名称,cnt_student 数量 + from course c + inner join ( + select course_id,count(*) as cnt_student + from score + group by course_id + ) t1 + on c.course_id = t1.course_id; + + + + + -- 26. 查询出只选修两门课程的学生学号和姓名 + select student_id 学生学号,student_name 姓名 + from student + where student_id in ( + select student_id + from score + group by student_id + having count(*) = 2 + ); + + + + + + -- 27. 查询男生、女生人数 + select gender 性别 ,count(*) as 数量 from student group by gender; + + + + -- 28. 查询名字中含有「风」字的学生信息 + select * from student where student_name like '%风%'; + + + + -- 29. 查询同名同性学生名单,并统计同名人数 + select count(*) from student group by student_name,gender having count(*) > 1; + + + + -- 30. 查询 1990 年出生的学生名单 + select *from student where year(birthday) = '1990'; + + + + -- 31. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + + select c.course_id 编号,c.course_name 课程,avg_score 平均成绩 + from course c + inner join ( + select course_id,round(avg(score), 2) as avg_score + from score + group by course_id + ) t1 + on c.course_id = t1.course_id + order by avg_score desc,c.course_id; + + + + + -- 32. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 + + select s.student_id 编号,s.student_name 名字,avg_score 平均成绩 + from student s + inner join ( + select student_id,round(avg(score), 2) as avg_score + from score + group by student_id + having avg_score >= 85 + ) t1 + on s.student_id = t1.student_id; + + + -- 33. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 + + select student_name 名字,score 分数 + from student s + inner join ( + select student_id,score + from score sc + inner join course c on sc.course_id = c.course_id + where course_name = '数学' + and score < 60 + ) t1 + on s.student_id = t1.student_id; + + + + -- 34. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) + select + s.student_id 编号,s.student_name 名字, + sum(case course_id when 1 then score else 0 end) as 语文, + sum(case course_id when 2 then score else 0 end) as 数学, + sum(case course_id when 3 then score else 0 end) as 英语 + from student s + left join score sc on s.student_id = sc.student_id + group by s.student_id; + + + + -- 35. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +SELECT st.student_name 姓名,co.course_name 课程,sc.score 分数 FROM +student as st +INNER JOIN +score as sc on st.student_id=sc.student_id +INNER JOIN +course as co on sc.course_id=co.course_id +WHERE sc.score>70 + + + + + -- 36. 查询不及格的课程 + select s.student_id,s.student_name,c.course_id,c.course_name,score + from score sc + inner join student s on sc.student_id = s.student_id + inner join course c on sc.course_id = c.course_id + where score < 60; + + + + -- 37. 查询课程编号为 1 且课程成绩在 80 分以上的学生的学号和姓名 + select s.student_id,s.student_name + from student s + where student_id in ( + select student_id + from score + where course_id = 1 + and score >= 80 + ); + + + + + + -- 38. 求每门课程的学生人数 + select c.course_id 编号,c.course_name 课程,cnt_student 人数 + from course c + inner join ( + select course_id,count(*) as cnt_student + from score + group by course_id + ) t1 + on c.course_id = t1.course_id; + + + + + + + -- 39. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + + + + + + -- 40. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 + + select s.*,score as max_score + from student s + inner join ( + select student_id,score + from score sc + inner join ( + select course_id + from course c + inner join teacher t on c.teacher_id = t.teacher_id + where teacher_name = '张三' + ) t1 + on sc.course_id = t1.course_id + order by score desc + limit 1 + ) t2 + on s.student_id = t2.student_id; + + + + + + -- 41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + + + SELECT s1.*FROM Score + AS s1 + INNER JOIN + Score AS s2 + ON s1.student_id = s2.student_id + AND s1.course_id <> s2.course_id + AND s1.score = s2.score; + + + + + + + -- 42. 查询每门功成绩最好的前两名 + + SELECT DISTINCT sc.course_id,sc.score + + FROM score sc WHERE ( + SELECT COUNT(1) FROM score WHERE sc.course_id = score.course_id + AND sc.score 5 +) t1 +on c.course_id = t1.course_id +order by cnt_student desc,c.course_id; + + + + + -- 44. 检索至少选修两门课程的学生学号 + select student_id from score group by student_id having count(*) >= 2; + + -- 45. 查询选修了全部课程的学生信息 + select *from student where student_id in ( + select student_id + from score + group by student_id + having count(*) = ( + select count(*) from course + ) +); + -- 46. 查询各学生的年龄,只按年份来算 + select *, + case when year(now()) >= year(birthday) then year(now()) - year( +birthday) +else 'other' end as 'age' +from Student; + + -- 47. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + select *, + case when year(now()) < year(birthday) then year(now()) - year( +birthday) - 1 + when year(now()) >= year(birthday) then year(now()) - year( +birthday) +else 'other' end as 'age' +from Student; + -- 48. 查询本周过生日的学生 + select * + from student + where + datediff(concat(year(current_date()), date_format(birthday, '-%m-%d')), current_date()) + between 0 and 7 + or + datediff(concat(year(current_date()) + 1, date_format(birthday, '-%m-%d')), current_date()) + between 0 and 7; + -- 49. 查询下周过生日的学生 + select * + from student + where + datediff(concat(year(current_date()), date_format(birthday, '-%m-%d')), current_date()) + between 7 and 14 + or + datediff(concat(year(current_date()) + 1, date_format(birthday, '-%m-%d')), current_date()) + between 7 and 14; + + -- 50. 查询本月过生日的学生 + select * from student where month(birthday) = month(current_date()); + -- 51. 查询下月过生日的学生 + select * from student where month(birthday) = 12; +~~~ + -- Gitee