From 15623c25a5b7f2e1a0850870074f18bfc6042032 Mon Sep 17 00:00:00 2001 From: lzlk <1930069369@qq.com> Date: Thu, 13 Oct 2022 21:30:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-10-12\344\275\234\344\270\232.sql" | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 "07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-10-12\344\275\234\344\270\232.sql" diff --git "a/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-10-12\344\275\234\344\270\232.sql" "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-10-12\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..f522ea4 --- /dev/null +++ "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-10-12\344\275\234\344\270\232.sql" @@ -0,0 +1,125 @@ +--10. 查询没学过"张三"老师讲授的任一门课程的学生姓名 + select sname from Student + where sid not in( + select sid from sc + where cid in( + select cid from Course + where tid in(select tid from Teacher where Tname='张三'))) + ; +--11. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩、 + select Student.SId,Student.Sname,sc1.平均成绩 from Student + inner join + (select sid,AVG(score)平均成绩 from sc + where sid in( + select sid from sc + where score<60 + group by sid + having count(*)>=2) + group by sid + )sc1 on Student.SId=sc1.SId + ; +--14. 查询各科成绩最高分、最低分和平均分: + +--以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + +--要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + select cid,count(*)选修人数,max(score)最高分,min(score)最低分,avg(score)平均分, + (select cname from Course c where c.CId=sc.CId)学科, + concat(count((case when score>=60 then '及格' end))*100/count(*),'%')及格, + concat(count((case when score>=70 and score<80 then '中等' end))*100/count(*),'%')中等, + concat(count((case when score>=80 and score<90 then '优良' end))*100/count(*),'%')优良, + concat(count((case when score>=90 then '优秀' end))*100/count(*),'%')优秀 + from sc + group by cid + order by count(*),cid + ; +--17. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 + select cid, + (select cname from Course c where c.CId=sc.CId)学科, + concat(count((case when score<60 then '[60-0]' end))*100/count(*),'%')[60-0], + concat(count((case when score>=60 and score<70 then '[70-60]' end))*100/count(*),'%')[70-60], + concat(count((case when score>=70 and score<85 then '[85-70]' end))*100/count(*),'%')[85-70], + concat(count((case when score>=85 then '[100-85]' end))*100/count(*),'%')[100-85] + from sc + group by sc.cid + order by count(*),sc.cid; +--18. 查询各科成绩前三名的记录 + select * from ( + select *,row_number()over(partition by cid order by score desc)排名 from sc)sc1 + where 排名<=3 + ; +--19. 查询每门课程被选修的学生数 + select cid,count(*)选修数 from sc + group by cid + ; +--22. 查询名字中含有「风」字的学生信息 + select * from student + where sname like '%风%' + ; +--23. 查询同名同性学生名单,并统计同名人数 + select sname,count(*)同名人数 from Student + group by sname + having count(*)>1 + ; +--24. 查询 1990 年出生的学生名单 + select * from Student + where year(sage)=1990 + ; +--25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 + select avg(score)平均成绩 from sc + group by cid + order by avg(score) desc,cid + ; +--30. 查询不及格的课程 + select * from course + where cid in( + select cid from sc + where score<60) + ; +--31. 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 + select * from Student + where + sid in( + select sid from sc where cid=01 and score>80); + ; +--35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 + select * from sc + inner join sc sc1 on sc.SId=sc1.SId + where sc1.score=sc.score and sc1.cid!=sc.cid + ; +--36. 查询每门功成绩最好的前两名 + select * from (select *,ROW_NUMBER() over(partition by cid order by score desc)排名 from sc)sc1 + where 排名<=2 + ; +--41. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 + select *,( + case + when month(sage)>MONTH(GETDATE()) + then year(GETDATE())-year(Sage)-1 + when month(sage)day(GETDATE()) + then year(GETDATE())-year(Sage)-1 + when month(sage)=MONTH(GETDATE()) and day(sage)