首页 > 代码库 > oralce之复杂查询举例

oralce之复杂查询举例

表结构:

S(SNO,SNAME) 代表 学号。学生姓名;

C(CNO,CNAME,CTEACHER) 代表 课号,课程名称。授课老师

SC(SNO,CNO,SCGRADE) 代表 学号。课号。课程成绩


1 查询没选过“黎明”老师的全部学生姓名。

select s.sname
from s
join sc
on sc.sno=s.sno
join c
on c.cno=s.cno
where c.cteacher<>'黎明'

2 查询出2门以上(包括2门)不及格学生的姓名及平均成绩。

select s.sname,c.a_grade
from
(select a.sno,count(cno) 
from
(select sno,cno
from sc
where sc.scgrade<60)
group by a.sno
having count(cno)>1)b
join s
on b.sno=s.sno
join 
(select avg(scgrade) a_grade,sno 
from sc
group by sno)c
on c.sno=s.sno

3 既学过1号课程又学过2号课程的学生姓名;

select s.sname 
from s
join
(select count(*),sno
from
(select * from 
sc 
where sc.cno in(1,2))
grout by sno
having cout(*)>1)a
on a.sno=s.sno



oralce之复杂查询举例