# 查询所有的课程的名称以及对应的任课老师姓名# 1.先确定好需要几张表的数据 course teacher# 2.查看一下表的数据是否是我们需要的
-- select * from course;
-- select * from teacher;
# 3.先获取课程名称再获取老师数据(连表操作)
-- select course.cname,teacher.tname FROM course inner JOIN teacher on teacher_id=tid;
#4、查询平均成绩大于八十分的同学的姓名和平均成绩
-- 1.先确定需要查询几张表的数据 score student
-- select * from score;
-- select * from student;
-- 2.以学生id为单位 先查询平均成绩
-- select student_id,avg(num) from score GROUP BY student_id HAVING avg(num) >80;
-- 3.再用多表查询 连表操作
-- select student.sname,stu.avg(num) from student inner join (select student_id,avg(num) from score GROUP BY student_id HAVING avg(num) >80) as stu on student.sid = stu.student_id;
#7、查询没有报李平老师课的学生姓名# 1.先确认要查询几张表的数据 student teacher course # 2.先确定李平老师的授课id号
-- SELECT tid FROM teacher WHERE tname='李平老师';
# 3.再获取李平老师课程编号
-- select cid from course where teacher_id =(SELECT tid FROM teacher WHERE tname='李平老师');
# 4.查询李平老师课程的学生id
-- select DISTINCT student_id from score where course_id in(select cid from course where teacher_id =(SELECT tid FROM teacher WHERE tname='李平老师'));
# 5.获取没有李平老师课程的学生姓名
-- select sname from student where sid notin(select DISTINCT student_id from score where course_id in(select cid from course where teacher_id =(SELECT tid FROM teacher WHERE tname='李平老师')));
#8、查询没有同时选修物理课程和体育课程的学生姓名(只要选了其中一门的 两门都选和都没选的都不要)# 1.先确定要查找数据的表有几个 score student course # 2.先获取体育课和物理课程的id
-- select cid from course where cname in ('物理','体育');
# 4.以学生为单位查询分别报了两门课程的学生 筛选出只选了一门课程的学生
-- select score.student_id FROM score where course_id in (select cid from course where cname in ('物理','体育')) GROUP BY score.student_id having COUNT(score.student_id) =1;
# 根据学生id 查询学生姓名
-- select sname from student where sid in(select score.student_id FROM score where course_id in (select cid from course where cname in ('物理','体育')) GROUP BY score.student_id having COUNT(score.student_id) =1);
#9、查询挂科超过两门(包括两门)的学生姓名和班级# 1.先筛选出成绩小于60分的学生
-- select student_id from score where num<60;
# 查询成绩不合格超过两门的学生
-- select student_id from score where num<60 GROUP BY student_id HAVING COUNT(student_id)>=2;
# 获取学生姓名
select sname from student where sid in (select student_id from score where num<60 GROUP BY student_id HAVING COUNT(student_id)>=2) ;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!