MySQL综合练习

发布时间:2024年01月24日

?1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

select Student.*,sc1.s_Score '语文' ,sc2.s_Score '数学'
from Student
left join Score sc1 on sc1.s_id=Student.s_id and sc1.c_id='01'
left join Score sc2 on sc2.s_id=Student.s_id and sc2.c_id='02'
where sc1.s_Score>sc2.s_Score;

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

select Student.*,sc1.s_Score '语文' ,sc2.s_Score '数学'
from Student
left join Score sc1 on sc1.s_id=Student.s_id and sc1.c_id='01'
left join Score sc2 on sc2.s_id=Student.s_id and sc2.c_id='02'
where sc1.s_Score<sc2.s_Score;

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

select Student.s_id,Student.s_name,AVG(Score.s_Score) '平均分'
from Student
left join Score on Score.s_id=Student.s_id
group by Student.s_id having AVG(Score.s_Score)>=60;

?4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)

select Student.s_id,Student.s_name,AVG(Score.s_Score) '平均分'
from Student
left join Score on Score.s_id=Student.s_id
group by Student.s_id having AVG(Score.s_Score)<60 or AVG(Score.s_Score) is NULL

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

select Student.s_id,Student.s_name,count(Score.c_id) '选课总数',ifnull(sum(Score.s_Score),0) '总成绩'
from Student
left join Score on Student.s_id=Score.s_id
group by Student.s_id

6、查询"李"姓老师的数量

select Teacher.t_name,count(Teacher.t_name) '总数' from Teacher
group by Teacher.t_id having Teacher.t_name like '李%'

?

?7、查询学过"张三"老师授课的同学的信息

select Student.* from Student
left join Score on Score.s_id=Student.s_id
left join Course on Course.c_id=Score.c_id
left join Teacher on Teacher.t_id=Course.t_id
 where Teacher.t_name="张三";

??8、查询没学过"张三"老师授课的同学的信息

 select Student.* from Student where Student.s_id not in(
  select Score.s_id from Score where Score.c_id in (select Course.c_id from Course left join Teacher t on t.t_id=Course.t_id where  t.t_name="张三")
 )

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select s.*
from Score sc1
join Score sc2
on
    sc1.s_id=sc2.s_id
and
    sc1.c_id=01
and
    sc2.c_id=02
left join Student s
on
    s.s_id=sc1.s_id;

?10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

select Student.* from Student
inner join Score on Score.s_id = Student.s_id and Score.c_id="01"
where Student.s_id not in (
select st2.s_id from Student st2
inner join Score sc2 on sc2.s_id = st2.s_id and sc2.c_id="02"
);

文章来源:https://blog.csdn.net/zcjh_/article/details/135708536
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。