建表:
教师表:
学生表:
课程表:
成绩表:
1.查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select st.*,sc.s_Score as '语文' ,sc2.s_Score '数学'
from Student st
left join Score sc on sc.s_id=st.s_id and sc.c_id='01'
left join Score sc2 on sc2.s_id=st.s_id and sc2.c_id='02'
where sc.s_Score>sc2.s_Score;
2.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select st.s_id,st.s_name,ROUND(AVG(sc.s_Score),2) cjScore from Student st
left join Score sc on sc.s_id=st.s_id
group by st.s_id having AVG(sc.s_Score)>=60;
3.查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select st.* from Student st
inner join Score sc on sc.s_id = st.s_id
inner join Course c on c.c_id=sc.c_id and c.c_id="01"
where st.s_id in (
select st2.s_id from Student st2
inner join Score sc2 on sc2.s_id = st2.s_id
inner join Course c2 on c2.c_id=sc2.c_id and c2.c_id="02");
4.查询没有学全所有课程的同学的信息
select * from Student where s_id not in (
select st.s_id from Student st
inner join Score sc on sc.s_id = st.s_id and sc.c_id="01"
where st.s_id in (
select st2.s_id from Student st2
inner join Score sc2 on sc2.s_id = st2.s_id and sc2.c_id="02"
) and st.s_id in (
select st2.s_id from Student st2
inner join Score sc2 on sc2.s_id = st2.s_id and sc2.c_id="03"));
5.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select st.s_id,st.s_name,avg(sc.s_Score) from Student st
left join Score sc on sc.s_id=st.s_id
where sc.s_id in (
select sc.s_id from Score sc
where sc.s_Score<60 or sc.s_Score is NULL
group by sc.s_id having COUNT(sc.s_id)>=2)
group by st.s_id;