-- 方式一SELECT s.`S#` ,s.`C#` ,s.score,s2.`C#` ,s2.score ,s3.Sname ,s3.Sage ,s3.Ssex
from SC s ,SC s2,Student s3 where s.`C#` ='01'and s2.`C#` ='02'and s.`S#` = s2.`S#`
and s.score > s2.score and s.`S#` = s3.`S#`;
-- 方式二select A.*,B.`C#`,B.score,C.Sname ,C.Sage ,C.Ssex
from Student C ,(select*from SC where `C#`='01')A
leftjoin(select*from SC where `C#`='02')B
on A.`S#`=B.`S#`
where A.score>B.score and C.`S#` = A.`S#`;
1.1 查询同时存在" 01 "课程和" 02 "课程的情况
-- 方式一SELECT*from SC s,SC s2 where s.`C#` ='01'and s2.`C#` ='02'and s.`S#` = s2.`S#`;
-- 方式二SELECT*from (select*from SC where `C#`='01')A
leftjoin (select*from SC where `C#`='02')B on A.`S#`=B.`S#`
where B.`S#` isnotnull;
1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
SELECT*FROM (select*from SC where `C#`='01') s
LEFTJOIN (select*from SC where `C#`='02') s2
on s.`S#` = s2.`S#`;
1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
SELECT*from SC s where s.`C#` ='02'and s.`S#` notin(SELECT s2.`S#` FROM SC s2 where s2.`C#`='01');
2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select A.`S#`,B.Sname,A.dc from(select `S#`,AVG(score)dc from SC groupby `S#`)A
leftjoin Student B on A.`S#`=B.`S#` where A.dc>=60;
3. 查询在 SC 表存在成绩的学生信息
select*from Student where `S#` in (selectdistinct `S#` from SC);
4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select B.`S#`,B.Sname,A.选课总数,A.总成绩 from
(select `S#`,COUNT(`C#`)选课总数,sum(score)总成绩 from SC groupby `S#`)A
rightjoin Student B on A.`S#`=B.`S#`;
4.1 查有成绩的学生信息
select B.`S#`,B.Sname,A.选课总数,A.总成绩 from
(select `S#`,COUNT(`C#`)选课总数,sum(score)总成绩 from SC groupby `S#`)A
LEFTjoin Student B on A.`S#`=B.`S#`;
5. 查询「李」姓老师的数量
selectCOUNT(*)李姓老师数量 from Teacher where Tname like'李%';
6. 查询学过「张三」老师授课的同学的信息
select*from Student s
where s.`S#` in ( SELECT s2.`S#` from SC s2
WHERE s2.`C#` IN (SELECT c.`C#` from Course c
WHERE c.`T#` IN (SELECT t.`T#` from Teacher t
WHERE t.Tname='张三')));
7. 查询没有学全所有课程的同学的信息
select*from Student
where `S#` in(select `S#` from SC groupby `S#` havingCOUNT(`C#`)<3);
8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select*from Student
where `S#` in(selectdistinct `S#` from SC
where `C#` in(select `C#` from SC where `S#`='01'));
9. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
select*from Student
where `S#` in(select `S#` from SC where `C#` in(selectdistinct `C#` from SC where `S#`='01') and `S#`<>'01'groupby `S#`
havingCOUNT(`C#`)>=3)
10. 查询没学过"张三"老师讲授的任一门课程的学生姓名
select*from Student
where `S#` notin(select `S#` from SC
where `C#` in(select c.`C#` from Course c
where c.`T#` in (SELECT t.`T#` from Teacher t where t.Tname='张三')));
11. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select A.`S#`,A.Sname,B.平均成绩 from Student A rightjoin
(select `S#`,AVG(score)平均成绩 from SC where score<60groupby `S#` havingCOUNT(score)>=2)B
on A.`S#`=B.`S#`;
12. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
SELECT*FROM Student s2
where s2.`S#` in(SELECT s.`S#` from SC s
where s.`C#` ='01'and s.score <60ORDERby score desc);
-- 方法一:select*from SC a where (selectCOUNT(*)from SC where `C#`=a.`C#` and score>a.score)<3orderby a.`C#`,a.score desc;
-- 方法二:select a.`S#`,a.`C#`,a.score from SC a
leftjoin SC b on a.`C#`=b.`C#` and a.score<b.score
groupby a.`S#`,a.`C#`,a.score
havingCOUNT(b.`S#`)<3orderby a.`C#`,a.score desc;
-- 方法三:-- select * from(select *,rank()over (partition by `C#` order by score desc)A from SC)B where B.A<=3;
19. 查询每门课程被选修的学生数
select `C#`,COUNT(`S#`)学生数 from SC groupby `C#`
20. 查询出只选修两门课程的学生学号和姓名
select `S#`,Sname from Student
where `S#` in(select `S#` from(select `S#`,COUNT(`C#`)课程数 from SC groupby `S#`)A
where A.课程数=2);
21. 查询男生、女生人数
select Ssex,COUNT(Ssex)人数 from Student groupby Ssex;
22. 查询名字中含有「风」字的学生信息
select*from Student where Sname like'%风%';
23. 查询同名同性学生名单,并统计同名人数
select A.*,B.同名人数 from Student A
leftjoin (select Sname,Ssex,COUNT(*)同名人数 from Student groupby Sname,Ssex)B
on A.Sname=B.Sname and A.Ssex=B.Ssex
where B.同名人数>1;
24. 查询 1990 年出生的学生名单
select*from Student whereYEAR(Sage)=1990;
25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select `C#`,AVG(score)平均成绩 from SC groupby `C#` orderby 平均成绩 desc,`C#`;
26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select A.`S#`,A.Sname,B.平均成绩 from Student A
leftjoin (select `S#`,AVG(score)平均成绩 from SC groupby `S#`)B on A.`S#`=B.`S#`
where B.平均成绩>85;
27. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
select B.Sname,A.score from(select*from SC
where score<60and `C#`=(select `C#` from Course
where Cname='数学'))A
leftjoin Student B on A.`S#`=B.`S#`;
28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
select A.`S#`,B.`C#`,B.score from Student A leftjoin SC B on A.`S#`=B.`S#`;
29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select A.Sname,D.Cname,D.score from
(select B.*,C.Cname from(select*from SC where score>70)B leftjoin Course C on B.`C#`=C.`C#`)D
leftjoin Student A on D.`S#`=A.`S#`;
30. 查询不及格的课程
SELECT*from Course c where c.`C#` in (select s.`C#` from SC s where score<60);
31. 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
select A.`S#`,B.Sname from (select*from SC where score>80and `C#`='01')A
leftjoin Student B on A.`S#`=B.`S#`;
32. 求每门课程的学生人数
同19题
33. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT*from Student st
where st.`S#` =(select s.`S#` from SC s
where s.`C#`=(select c.`C#` from Course c
where c.`T#`=(select t.`T#` from Teacher t
where t.Tname='张三'))
orderby s.score desc limit 1);
34. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT*from Student st
where st.`S#` =(select `S#` from(select*,DENSE_RANK()over (orderby score desc)A
from SC
where `C#`=(select `C#` from Course
where `T#`=(select `T#` from Teacher where Tname='张三')))B
where B.A=1);
35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select C.`S#`,max(C.`C#`)`C#`,max(C.score)score from SC C
leftjoin(select `S#`,avg(score)A from SC groupby `S#`)B
on C.`S#`=B.`S#`
where C.score=B.A
groupby C.`S#`
havingCOUNT(0)=(selectCOUNT(0)from SC where `S#`=C.`S#`);
36. 查询每门功成绩最好的前两名
select*from
(select*,ROW_NUMBER()over(partitionby `C#` orderby score desc)A from SC)B
where B.A<3;
37. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。
select `C#`,COUNT(`S#`)选修人数 from SC
groupby `C#`
havingCOUNT(`S#`)>5orderby 选修人数 desc,`C#`;
38. 检索至少选修两门课程的学生学号
select `S#` from SC
groupby `S#`
havingCOUNT(`C#`)>=2;
39. 查询选修了全部课程的学生信息
select `S#` from SC
groupby `S#`
havingcount(`C#`)=(selectdistinctCOUNT(0)a from Course);
40. 查询各学生的年龄,只按年份来算
select s.Sname , year(now())-year(s.Sage) as age from Student s
41. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select s.Sname , timestampdiff(year , s.Sage, now()) as age from Student s
42. 查询本周过生日的学生
select*from Student s
where WEEKOFYEAR(s.Sage)=WEEKOFYEAR(NOW());
43. 查询下周过生日的学生
select*from Student s
where WEEKOFYEAR(s.Sage)=WEEKOFYEAR(NOW())+1;
44. 查询本月过生日的学生
select*from Student s
whereMONTH(s.Sage)=MONTH(current_date());
45. 查询下月过生日的学生
select*from Student s
whereMONTH(s.Sage)=MONTH(current_date())+1;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)