From 31c48036f4c346adebed4d1f59f899a6bf0f59c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=BF=8A=E4=BC=9F?= <2421084001@qq.com> Date: Mon, 23 Oct 2023 19:51:35 +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 --- .../1020\344\275\234\344\270\232.md" | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 "12\346\236\227\344\277\212\344\274\237/1020\344\275\234\344\270\232.md" diff --git "a/12\346\236\227\344\277\212\344\274\237/1020\344\275\234\344\270\232.md" "b/12\346\236\227\344\277\212\344\274\237/1020\344\275\234\344\270\232.md" new file mode 100644 index 0000000..9c92f0b --- /dev/null +++ "b/12\346\236\227\344\277\212\344\274\237/1020\344\275\234\344\270\232.md" @@ -0,0 +1,95 @@ +1. 查询" 1 "课程比" 2 "课程成绩高的学生的信息(学号、姓名、性别、出生日期)及课程分数 +select distinct s.*,s1.score from score s1,student s where s1.score>(select max(score) from score where course_id=2) and s1.student_id=s.student_id; +-- 2. 查询同时参与" 1 "课程和" 2 "课程考试的学生信息 +select distinct s.* from score s1,student s where s1.course_id in(1,2) and s1.student_id=s.student_id; + +-- 3. 查询存在" 1 "课程但可能不存在" 2 "课程的情况(不存在时显示为 null ) +select *,ifnull(s.score,null) from score s where s.course_id=1 or s.course_id=2; + +-- 4. 查询不存在" 1 "课程但存在" 2 "课程的情况 +select * from score where course_id<>1 and course_id=2; +-- 5. 查询平均成绩大于等于 60 分的同学的学生编号、姓名和平均成绩 +with + a as (select distinct student_id,avg(score) over(partition by student_id) as n from score s) + select a.student_id,stu.student_name,a.n from a,student stu where a.student_id=stu.student_id and a.n>60; +-- 6. 查询在成绩表存在成绩的学生信息 +select distinct stu.* from student stu,score s where s.student_id=stu.student_id; +-- 7. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +SELECT DISTINCT + stu.student_id, + student_name, + count( s.course_id ) over ( PARTITION BY s.student_id ), + c.course_name, + sum(s.score) over(PARTITION BY s.student_id) +FROM + student stu join + score s join + course c +on stu.student_id=s.student_id and + s.course_id = c.course_id; + + select distinct s.* from student s , score sc where s.student_id >= sc.student_id; +-- 8. 查询「李」姓老师的数量 + select count(*) from teacher where teacher_name like '李%'; +-- 9. 查询学过「李白」老师授课的同学的信息 +select stu.* from student stu,score s,teacher t,course c where stu.student_id=s.student_id and s.course_id=c.course_id and c.teacher_id=t.teacher_id and t.teacher_name='李白'; +-- 10. 查询没有学全所有课程的同学的信息 +SELECT + stu.* +FROM + score s, + student stu +WHERE + s.student_id = stu.student_id +GROUP BY + s.student_id +HAVING + count(s.course_id)<(SELECT count(*) FROM course ); +-- 11. 查询至少有一门课与学号为" 1 "的同学所学相同的同学的信息 +with +a as (select count(course_id) over(partition by student_id) as a1 from score), +b as (select count(*) as b1 from score where student_id=1) +select distinct s.student_id from score s,a,b where a.a1 = b.b1 and student_id <>1; +-- 12. 查询和" 1 "号的同学学习的课程完全相同的其他同学的信息 +with +a as ( select course_id,count(course_id) over(partition by student_id) as a1 from score where student_id <>1), +b as ( select count(*) over() n,course_id as b1 from score where student_id=1) +select distinct stu.* from student stu,score s,a,b where a.a1=b.n and s.course_id=a.course_id and stu.student_id=s.student_id and s.student_id<>1; +-- 13. 查询没学过「李白」老师讲授的任一门课程的学生姓名 +select * from student where student_id not in( +select student_id from score where course_id in( +(select course_id from course c,teacher t where c.teacher_id=t.teacher_id and t.teacher_name='李白'))); +-- 14. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +with +a as (select *,count(*) over(partition by student_id) as n,avg(score) over(partition by student_id) as ss from score where score <60) +select distinct stu.student_id,stu.student_name,a.ss from student stu,a where stu.student_id=a.student_id and a.n>=2; +-- 15. 检索" 1 "课程分数小于 60,按分数降序排列的学生信息 +select s.*,s1.score from student s,score s1 where s1.score<60 and s1.course_id=1 order by score; +-- 16. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +with a as( +select *,avg(score) over(partition by student_id) as n from score) +select distinct s.*,a.n from score s,a where s.student_id=a.student_id ORDER BY a.n desc; +-- 17. 查询各科成绩最高分、最低分和平均分 +select distinct max(score) over(partition by course_id) as 最高分,min(score) over(partition by course_id) as 最低分,avg(score) over(partition by course_id) as 平均分 from score; +-- 18. 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率(及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90),要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 +WITH +a as (select course_id,course_name as 课程号 from course), +b as (select distinct course_id, + max(score) over(partition by course_id) as 最高分, + min(score) over(partition by course_id) as 最低分, + avg(score) over(partition by course_id) as 平均分 +from score), +c as (select (select count(score) from score where score>=60)/count(score) as 及格率 from score), +d as (select (select count(score) from score where score>70 and score<80)/count(score) as 中等率 from score), +e as (select (select count(score) from score where score>80 and score<90)/count(score) as 优良率 from score), +f as (select (select count(score) from score where score>90)/count(score) as 优秀率 from score), +g as (select distinct course_id as id,count(student_id) over(partition by course_id ORDER BY course_id) as rs from score) +select * from a,b,c,d,e,f,g WHERE +a.course_id=b.course_id AND +a.course_id=g.id; +-- 19. 按各科成绩进行排序,并显示排名,Score 重复时保留名次空缺 +select student_id,course_name,score,row_number() over(partition by student_id order by score desc) 排名 + from score sco left join course c on sco.course_id = c.course_id ; +-- 20. 按各科成绩进行排序,并显示排名,Score 重复时合并名次 +select student_id,course_name,score,dense_rank() over(partition by student_id order by score desc) 排名 + from score sco left join course c on sco.course_id = c.course_id ; \ No newline at end of file -- Gitee