#1.1查询01课程与02课程的课程表:select student_id, score as c1_score from score where course_id='01';
select student_id, score as c2_score from score where course_id='02';
#1.2使用内连接,将两个课程表进行连接,并从中选出01select c1.student_id,c1_score,c2_score from
(select student_id, score as c1_score from score where course_id='01') as c1 inner join
(select student_id, score as c2_score from score where course_id='02') as c2
on c1.student_id=c2.student_id
where c1_score>c2_score;
2、查询同时存在01课程和02课程的情况
select c1.student_id,c1_score,c2_score from
(select student_id, score as c1_score from score where course_id='01') c1 inner join
(select student_id, score as c2_score from score where course_id='02') c2
on c1.student_id=c2.student_id;
select c1.student_id, c1.score, c2.score
from score c1 join score c2 on c1.student_id=c2.student_id
where c1.course_id='01'and c2.course_id='02';
3、查询存在01课程但可能不存在02课程的情况(不存在时显示为 null )
select c1.student_id,c1_score,c2_score from
(select student_id, score as c1_score from score where course_id='01') c1 left join
(select student_id, score as c2_score from score where course_id='02') c2
on c1.student_id=c2.student_id;
4、查询不存在01课程但存在02课程的情况
select * from
(select student_id, score as c1_score from score where course_id='01') c1 right join
(select student_id, score as c2_score from score where course_id='02') c2
on c1.student_id=c2.student_id
where c1.student_id isnull;
5、查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select student.id,name,avg(score) from student leftjoin score on student.id=student_id
groupby student_id
havingavg(score)>=60;
6、查询在score表存在成绩的学生信息
selectdistinct student.*from student rightjoin score on id=student_id;
7、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select id, name, count(course_id),sum(score) from student leftjoin score on id=student_id
groupby id;
8、查有成绩的学生信息
selectdistinct student.*from score leftjoin student on id=student_id;
9、查询「李」姓老师的数量
selectcount(id) from teacher
where name like'李%';
10、查询学过「张三」老师授课的同学的信息
select student.* from student
left join score on student.id=student_id
left join course on course_id=course.id
left join teacher on teacher_id=teacher.id
where teacher.name='张三';
11、查询没有学全所有课程的同学的信息
select student.* from student left join
(select student_id, count(course_id) as courseNum from score groupby student_id)
as studentCourse on student.id=studentCourse.student_id
where courseNum<(selectcount(id) as allNum from course);
12、查询至少有一门课与学号为01的同学所学相同的同学的信息
select distinct student.* from student
left join score on student.id=student_id
right join (select course_id from score where student_id='01') as No01Course on score.course_id=No01Course.course_id
where student.id!='01';
13、查询和01号的同学学习的课程完全相同的其他同学的信息
#和01选了完全相同课程的同学,首先选出所选课程是01所选课程的子集的同学,再从中筛选出选课数量与01同学相同的。#13.1 01同学选的所有课程:select course_id from score where student_id='07';
#13.2 选了01没选的课的同学select student_id from score
where course_id notin
(select course_id from score where student_id='07');
#13.3 排除掉这些人,就是01选课列表的子集的同学select student_id from score
where student_id!='07'and student_id notin
(select student_id from score
where course_id notin
(select course_id from score where student_id='07'));
#13.4 选出(01选课列表的子集的同学)选课数量与01选课数量相同的人select student_id from
(select student_id from score
where student_id!='07'and student_id notin
(select student_id from score
where course_id notin
(select course_id from score where student_id='07')
)
) as subset
groupby student_id
having count(*)=
(selectcount(course_id) from score where student_id='07');
14 查询没学过"张三"老师讲授的任一门课程的学生姓名
#14.1 查询学过张三教过的课的同学:select student.id from student
left join score on student.id=student_id
left join course on course_id=course.id
left join teacher on teacher_id=teacher.id
where teacher.name='张三';
#14.2 排除这些同学select name from student
where id notin (select student.id from student
left join score on student.id=student_id
left join course on course_id=course.id
left join teacher on teacher_id=teacher.id
where teacher.name='张三');
15 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
#15.1 选出不及格的课程和同学select student_id from score
where score<60;
#15.2 从中选出count>=2的学生idselect student_id from
(select student_id, score from score
where score<60) as sc
groupby student_id
having count(score)>=2;
#15.3 从中选出学号姓名和平均成绩select student.id, student.name, avg(score) from
student left join score on student.id=student_id
where student.id in (select student_id from
(select student_id, score from score
where score<60) as sc
groupby student_id
having count(score)>=2
)
groupby student.id;
16 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select student.*from
student leftjoin score on student.id=student_id
where course_id='01'and score<60orderby score desc;
17 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
#选出所有同学各自的平均成绩select student_id, avg(score) from score
groupby student_id;
#连接至学生-成绩表select student.id, student.name, score, avg_score from
(student left join score on student.id=student_id) left join
(select student_id, avg(score) as avg_score from score
groupby student_id
) as avgsc on student.id=avgsc.student_id
order by avg_score desc;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~