1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
1.1 查询同时存在" 01 "课程和" 02 "课程的情况
1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
3.查询在 SC 表存在成绩的学生信息
4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
4.1 查有成绩的学生信息
5.查询「李」姓老师的数量
6.查询学过「张三」老师授课的同学的信息
7.查询没有学全所有课程的同学的信息
8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
10.查询没学过"张三"老师讲授的任一门课程的学生姓名
1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
思路:因为需要全部的学生信息,则需要在sc表中得到符合条件的SId后与student表进行join,可以left join 也可以 right join
select * from Student RIGHT JOIN (
select t1.SId, class1, class2 from
(select SId, score as class1 from sc where sc.CId = '01')as t1,
(select SId, score as class2 from sc where sc.CId = '02')as t2
where t1.SId = t2.SId AND t1.class1 > t2.class2
)r
on Student.SId = r.SId;
1.1 查询同时存在" 01 "课程和" 02 "课程的情况
select * from
(select * from sc where sc.CId = '01') as t1,
(select * from sc where sc.CId = '02') as t2
where t1.SId = t2.SId;
1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select * from
(select * from sc where sc.CId = '01') as t1
left join
(select * from sc where sc.CId = '02') as t2
on t1.SId = t2.SId;
1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
select * from sc
where sc.SId not in (
select SId from sc
where sc.CId = '01'
)
AND sc.CId= '02';
2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select student.SId,sname,ss from student,(
select SId, AVG(score) as ss from sc
GROUP BY SId
HAVING AVG(score)> 60
)r
where student.sid = r.sid;
3.查询在 SC 表存在成绩的学生信息
select DISTINCT student.*
from student,sc
where student.SId=sc.SId
4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select student.sid, student.sname,r.coursenumber,r.scoresum
from student,
(select sc.sid, sum(sc.score) as scoresum, count(sc.cid) as coursenumber from sc
group by sc.sid)r
where student.sid = r.sid;
如要显示没选课的学生(显示为NULL),需要使用join:
select s.sid, s.sname,r.coursenumber,r.scoresum
from (
(select student.sid,student.sname
from student
)s
left join
(select
sc.sid, sum(sc.score) as scoresum, count(sc.cid) as coursenumber
from sc
group by sc.sid
)r
on s.sid = r.sid
);
4.1 查有成绩的学生信息
exists()
select * from student
where exists (select sc.sid from sc where student.sid = sc.sid);
in()
select * from student
where student.sid in (select sc.sid from sc);
5.查询「李」姓老师的数量
select count(*)
from teacher
where tname like '李%';
6.查询学过「张三」老师授课的同学的信息
select student.* from student,teacher,course,sc
where
student.sid = sc.sid
and course.cid=sc.cid
and course.tid = teacher.tid
and tname = '张三';
7.查询没有学全所有课程的同学的信息
debug
select * from student
where student.sid not in (
select sc.sid from sc
group by sc.sid
having count(sc.cid)= (select count(cid) from course)
);
8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
debug
select * from student
where student.sid in (
select sc.sid from sc
where sc.cid in(
select sc.cid from sc
where sc.sid = '01'
)
);
9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
select * from student
left join
(select * from sc
left join
select * from course where cid="01"
on sc.cid = course.cid)as t1
on student.sid=t1.sid;
(select * from sc
left join
select * from course where cid="01"
on sc.cid = course.cid)as t1
select * from sc
inner join
(select * from course where cid="01")as t1
on sc.cid = t1.cid;
select * from student,
(select * from sc
inner join
(select * from course where cid="01")as t1
on sc.cid = t1.cid)as t
where student.sid=t.sid;
10.查询没学过"张三"老师讲授的任一门课程的学生姓名
select * from student
where student.sid not in (
select sc.sid from sc where sc.cid in(
select course.cid from course where course.tid in(
select teacher.tid from teacher where tname="张三"
)
)
);