有一个学生表S(SNO,SNAME),课程表(CNO,CNAME),选课表SC(SNO,CNO,GRADE),现在要选出选修了全部课程的学生的姓名,SQL语句该怎么写。求一个执行效率比较高的SQL语句。

解决方案 »

  1.   

    select 姓名 from (select 学号, count(学号) as Count from 选课表 group by 学号) as TempTable inner join 学生表 S on 学生表.学号=TempTable.学号 where Count=(select count(*) from 课程表)
    这种东西完全可以用StoredProcedure来简化和提高效率
      

  2.   

    select SNAME
    from s 
    where not existes (select * from  课程表,选课表SC where c.cno=sc.cno and s.sno=sc.sno)
      

  3.   

    不好意思没有表,就那么思想的写的,如果不正确,你把NOT去掉看看,因为以前还加了一个限定了的
      

  4.   

    楼主分好少...
    代码经过测试
    select * from studenttb where sno in 
    (select sno from selectclasstb group by sno having count(cno)= (select count(cno) from classtb) )
      

  5.   

    select * from 学生表 where sno in 
    (select sno from 选课表 group by sno having count(cno)= (select count(cno) from 课程表) )
      

  6.   

    select * from s 
    where sno 
    in(select sno from sc where cno in(1,2))
      

  7.   

    select * from 学生表 where sno in (select sno from 选课表 where cno ='课程1' or cno ='课程2' group by sno having count(sno)=2)
    经测试可以
      

  8.   

    ljqdu1982(风怀宇) ( ) 信誉:100    Blog  2006-10-11 16:58:00  得分: 0  
     
     
       加题了,求课程名为A的成绩比课程名为B的成绩高的学生的学号及姓名
    -----------------------------------
    select * from S
    inner join 
    (
    select SNO from SC SA
    inner join C CA on SA.CNO = CA.CNO and CA.CName = ‘A’
    inner join SC SB on SA.SNO = SC.SBO 
    inner join C CB on SB.CNO = CB.CNO and CB.CName = 'B'
    where SA.Grade > SB.Grade 
    ) T
    on S.SNO = T.SNO效率可能不高
      
     
      

  9.   

    select * from S
    inner join 
    (
    select SNO,SA.Grade AGrade, SB.Grade BGrade from SC SA
    inner join C CA on SA.CNO = CA.CNO and CA.CName = ‘A’
    inner join SC SB on SA.SNO = SC.SBO 
    inner join C CB on SB.CNO = CB.CNO and CB.CName = 'B'
    where SA.Grade > SB.Grade 
    ) T
    on S.SNO = T.SNO
      

  10.   

    select * from 学生表 where sno in (select sno from 选课表 a where sno in 
    (select sno from 选课表 where cno ='课程1' or cno ='课程2' group by sno having count(sno)=2) and cno='课程1'and grade > (select grade from 选课表 where sno = a.sno and cno='课程2'))
      

  11.   

    如果要显示成绩那就这样select 学生表.sname,课程表.cname,选课表.grade from 学生表,课程表,选课表 
    where 选课表.sno in (select sno from 选课表 a where sno in 
    (select sno from 选课表 where cno ='课程1' or cno ='课程2' group by sno having count(sno)=2)
    and cno='1'and grade > (select grade from 选课表 where sno = a.sno and cno='2')) 
    and 学生表.sno = 选课表.sno
    and 课程表.cno = 选课表.cno
    and (选课表.cno = '课程1' or 选课表.cno = '课程2')
      

  12.   

    其实楼上的直接可以
    select distinct a.sno,c.grade as grade1,d.grade as grade2 from selectclasstb a 
    inner join selectclasstb c on c.sno = a.sno  and c.cno = '1'
    inner join selectclasstb d on d.sno =a.sno and d.cno='2'
    where c.grade>=d.grade
      

  13.   

    select sno,sname from s AS x where not exists
      select * from c AS y where y.cno not in 
        (select cno from sc where sno = x.sno)