ORACLE求救:高手指点~~~~
三个关系表:
S(Sno,Sname)
C(Cno,Cname,Cteacher)
Sc(Sno,Cno,Scgrade)
1、 列出没有上过老师叫‘LiQing’的课程的所有学生的姓名
2、 列出两门及其以上(含两门)不及格课程的学生的姓名与平均成绩
3、 列出所有上课程1、课程2所有学生的姓名
4、 课程1比课程2的成绩高的所有学生的学号及其课程1和课程2的平均成绩。

解决方案 »

  1.   

    三个关系表:
    S(Sno,Sname)
    C(Cno,Cname,Cteacher)
    Sc(Sno,Cno,Scgrade)
    1、 列出没有上过老师叫‘LiQing’的课程的所有学生的姓名
    select s.sname
    from s,sc ,c
    where c.cteacher<>'LiQing'
    and s.sno=sc.sno
    and sc.cno=c.cno
    2、 列出两门及其以上(含两门)不及格课程的学生的姓名与平均成绩
    select s.sname,avg(sc.scgrade)
    from s,sc
    where s.sno=sc.sno
    and sc.sno in(select sno from sc where scgrade<60 group by sno having count(*)>=2)
    3、 列出所有上课程1、课程2所有学生的姓名
    select s.sname
    from s,sc ,c
    where c.cname in ('课程1','课程2')
    and s.sno=sc.sno
    and sc.cno=c.cno
    4、 课程1比课程2的成绩高的所有学生的学号及其课程1和课程2的平均成绩。select sno,avg(c1) ,avg(c2)
    from
    (--先行转列取出课程1和课程2的成绩
    select s.sno,
    max(decode(c.cname,'课程1',sc.scgrade,0)) as c1,
    max(decode(c.cname,'课程2',sc.scgrade,0)) as c2,
    from s,sc ,c
    where c.cname in ('课程1','课程2')
    and s.sno=sc.sno
    and sc.cno=c.cno
    group by s.sno)
    where c1-c2>0
      

  2.   

    --1、 列出没有上过老师叫‘LiQing’的课程的所有学生的姓名
    select sname from student
    where sno not in
    (
    select sno from sc a,clas b
    where a.cno = b.cno
    and b.cteacher = 'LiQing'
    )--2、 列出两门及其以上(含两门)不及格课程的学生的姓名与平均成绩
    select sname,avg(scgrade) from sc a,student b
    where a.sno =b.sno
    and a.sno in 
    (
    select a.sno from sc a
    where a.scgrade < 60 
    group by a.sno
    having count(a.sno) >= 2
    )
    group by sname--3、 列出所有上课程1、课程2所有学生的姓名
    select sname from student 
    where sno in 
    (
    select sno from sc
    where sc.cno in (1,2)
    )
    --4、 课程1比课程2的成绩高的所有学生的学号及其课程1和课程2的平均成绩。
    select a.sno,avg(a.scgrade) avg_class1,avg(b.scgrade) avg_class2 from 
    (
    select * from sc
    where sc.cno = 3
    ) a
    ,
    (
     select * from sc
     where sc.cno = 2 
    ) b
    where a.sno = b.sno
    and a.scgrade > b.scgrade
    group by a.sno