表 student : id (学号),name(姓名)
表 course: id(课程号),name(课程名称),cteacher(授课老师)
表 sc:sid(学号),cid(课程号),score(分数)
请写出下面几个sql
1:找出所有选修了‘王刚’老师所授课的学生学号和姓名
2:找出有一门成绩低于60分的学生的平均成绩
3:找出选修了课程1和课程2,并且课程1的分数比课程2的分数高的学生姓名

解决方案 »

  1.   

    1:
    select a.sid,b.name
    from sc a,student b,course c
    where a.sid=b.id
    and a.cid=c.id
    and c.cteacher='王刚';2:
    select sid,avg(score)  from  sc a
    where exist (
    select sid from sc b where score<60
    and a.sid=b.sid)
    group by sid3.
    select a.sid,b.name
    from(
    select sid,cid,score
    from sc
    where exists (
    select  id from course where name in ('课程1','课程2') and cid=id)) a,
    student b,
    sc c
    where a.sid=b.id
    and a.cid=c.id
    and decode(c.name,'课程1',a.score,0)>decode(c.name,'课程2',a.score,0)没实际测,试试吧
      

  2.   

    1:找出所有选修了‘王刚’老师所授课的学生学号和姓名
    select * from student
    where 
    EXISTS
    (
      select * from sc
      where
      sc.sid=student.id and 
      EXISTS
      (
         select * from course
         where course.cteacher='王刚'
         and course.id=sc.cid
      )
    )
      

  3.   

    3. 
    select a.sid,b.name 
    from( 
    select sid,cid,score 
    from sc 
    where exists ( 
    select  id from course where name in ('课程1','课程2') and cid=id)) a, 
    student b, 
    sc c 
    where a.sid=b.id 
    and a.cid=c.id 
    and decode(c.name,'课程1',a.score,0)>decode(c.name,'课程2',a.score,0) 这个没有看懂是什么意思? 能给解释一下么?
      

  4.   

    先查询出选了课程1,课程2的人员sid及成绩,用它做为一个表,去关联b,c,
    decode(c.name,'课程1',a.score,0)>decode(c.name,'课程2',a.score,0) 这是条件,我没测,不知这样写的结果对不对.
      

  5.   

    decode(c.name,'课程1',a.score,0)>decode(c.name,'课程2',a.score,0)
    这样作为条件的话,并不能比较出课程1与课程2哪个成绩高啊?
      

  6.   

    当然不是数据库作业!
    只是不会写第三个SQL
      

  7.   


    1:找出所有选修了‘王刚’老师所授课的学生学号和姓名 
    select a.id,a.name from student a,course b,sc c
    where a.id=c.id and b.id=c.cid
          and b.cteacher=‘王刚’2:找出有一门成绩低于60分的学生的平均成绩 
    select sid,avg(score) from sc
    where sid in
    (
    select sid from 
    (select sid ,count(*) sum from sc  where score < 60)
    where sum =1)
    group by sid;3:找出选修了课程1和课程2,并且课程1的分数比课程2的分数高的学生姓名 
    select name from student 
    where id in ( select a.id from student a,course b,sc c
                  where a.id=c.id and b.id=c.cid
                  and b.name in ('课程1','课程2'))
    and id in ( select id from 
    (select c.sid as id,c.score as score from sc c,course b where b.id=c.cid and b.name='课程1') d,
    (select c.sid as id,c.score as score from sc c,course b where b.id=c.cid and b.name='课程1') e
    where d.id =e.id and d.score > e.score )没有实际测试
      

  8.   


    厉害,
    改了改第三个,如下select a.name, from 
                (select sid, 
                  sum(decode(b.name, '课程1', score, 0)) score1,  
                  sum(decode(b.name, '课程2', score, 0)) score2 
                from 
                    sc a, course b 
               where a.cid=b.id 
                        and 
                     (b.name in ('课程1','课程2')) 
               group by sid 
               having count(1)=2) b, 
               student a 
          where b.score1>b.score2 and a.id=b.sid;没有测试,自己试试哟。
      

  9.   

     having count(1)=2 
    这个地方是什么意思?