1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
#查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select stu.*, sc1.c, sc1.score, sc2.c, sc2.score
from
(select * from sc where c='01')sc1
,(select * from sc where c='02')sc2
,student stu
where stu.s=sc1.s
and stu.s=sc2.s
and sc1.score>sc2.score
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
#查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select stu.s, stu.sname, avg(sc.score) avgscore
from student stu,sc
where stu.s=sc.s
group by stu.S
having avgscore>60
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
#查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select stu.s, stu.sname, count(c), sum(score)
from student stu, sc
where stu.s=sc.s
group by sc.S
6、查询"李"姓老师的数量
#6、查询"李"姓老师的数量
select count(*)
from teacher tch
where tch.Tname like '李%'
7、查询学过"张三"老师授课的同学的信息
#7、查询学过"张三"老师授课的同学的信息
select stu.*
from student stu, sc
where stu.s=sc.s and sc.c in
(select co.c
from teacher te,course co
where te.t=co.t and te.Tname='张三')
8、查询没学过"张三"老师授课的同学的信息
#8、查询没学过"张三"老师授课的同学的信息
select *
from student stu1
where stu1.s not in (
#7、查询学过"张三"老师授课的同学的信息(学号)
select stu.s
from student stu, sc
where stu.s=sc.s and sc.c in
(select co.c
from teacher te,course co
where te.t=co.t and te.Tname='张三')
)
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
#9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select stu.*
from student stu,sc
where stu.s=sc.s and sc.c='02' and stu.s in(
#查询学过编号为"01"的同学
select stu.s
from student stu, sc
where stu.s=sc.s and sc.c='01'
)
#自连接方法,sc与自身sc进行连接
#9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select stu.*
from student stu, sc sc1, sc sc2
where sc1.c='01' and sc2.c='02' and sc1.s=sc2.s and stu.s=sc1.s
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
#10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select stu.*
from student stu,sc
where stu.s=sc.s and sc.c='01' and stu.s not in(
#查询学过编号为"02"的同学
select stu.s
from student stu, sc
where stu.s=sc.s and sc.c='02'
)
11、查询没有学全所有课程的同学的信息
#11、查询没有学全所有课程的同学的信息
select stu.*
from student stu, sc