RT每次面试 都死在SQL语句上,问一下这个SQL语句如何写,请写下思路和答案,好了追加分,谢谢
题目:
        给你三张表        S (SNO,SNAME)代表 (学号,姓名)
   
        C (CNO,CNAME,CTEACHER)代表(课号,课名,教师)        SC(SNO,CNO,SCGRADE)代表(学号,课号,成绩)        请用SQL语句查出以下结果。
        1.找出没有选过“liming”老师的所有学生姓名。
        2.找出2们(含2门)不及格学生姓名以及平均成绩。
        3.即选过1号课程又选过2号课程所有学生的姓名请大侠写出答案,如果可以写上思路,在下不甚感激追加分,谢谢

解决方案 »

  1.   

    就是多表查询,练习下就好了,就第一个来说,就是:查找S表中所有学号不在SC表中的学生即可,SC表的课号又需要根据C表的教师得出
      

  2.   


    1. select s.sname from S s, SC sc, C c where sc.cno = c.cno and sc.sno = s.sno and c.cteacher != 'liming';2. select s.sname,avg(sc.scgrade) from S s, SC sc, C c where s.no = sc.sno and
      (select count(*) from SC cc where cc.scgrade < 60 and cc.sno = sc.sno) >= 2 group by s.sname;3. select s.sname from S s, SC sc, C c where s.no = sc.sno and (sc.cno in (1) and sc.cno in (2));
      

  3.   


    -- 1:选出没有选修过“liming”老师讲授课程的所有学生姓名
    select sname  from s  where sno not in (select sno from sc,c where c.cno=sc.cno and cteacher='liming')--- 2:列出有两门以上(含两门)不及格课程的学生姓名及其平均成绩
    select sname as 姓名, avg(scgrade)as 平均成绩 ,count(*) as 不及格课程数 from s inner join sc on s.sno=sc.sno
    where scgrade<60
    group by sname
    having count(*)>=2--- 3:列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
    select sname as 姓名 from s where sno in (select sno from sc,c where c.cno=sc.cno and sc.cno=1  )and sno in 
    (select sno from sc,c where c.cno=sc.cno and  sc.cno=2)
      

  4.   

      1.找出没有选过“liming”老师的所有学生姓名。
      select sname from s where not exists (select 1 from c,sc where c.cno=sc.cno and cteacher='liming' and s.sno=sc.sno)
      2.找出2们(含2门)不及格学生姓名以及平均成绩。
      select sno,count(*) from s,sc where s.sno = sc.sno where scgrade<60 group by sno having count(*)>=2
      select sno,avg(scgrade) from sc group by sno
      3.即选过1号课程又选过2号课程所有学生的姓名
       select sname from s where sno = (select sno from sc sc1,sc sc2 where sc1.sno=sc2.sno and sc1.cno='1' and sc2.cno='2')
      

  5.   

    这些都是简单的SQL,还没涉及过程啊这些,楼主多做下,多思考,关键是怎么去思考,做多了就熟练了,不要急,慢慢来
      

  6.   

    --我记得好像是数据库理论书上的例题啊[code=SQL]--找出没有选过“liming”老师的所有学生姓名
    select s.sname
    from s,sc 
    where not exists (select 1 from c where c.cno=sc.cno and c.cteacher='liming')--找出2们(含2门)不及格学生姓名以及平均成绩
    select s.sname from s 
    where sno in(select sno from sc 
                 where grade<60 
                 group by sno having(count(*)>=2)--两门不及格的学号--即选过1号课程又选过2号课程所有学生的姓名--选了1号课程的学生
    select sname from s
    where exists(select 1 from sc where s.sno=sc.sno and sc.cno=1)
    intersect--求交集得到的就是既选了1号课程又选了2号课程的学生姓名
    --选了2号课程的学生
    select sname from s
    where exists(select 1 from sc where s.sno=sc.sno and sc.cno=2)[/code]
      

  7.   


    写个最先老师教我们的子查询
    1
    select sname
    from s 
    where sno not in(select distinct a.sno from sc,c where c.CTEACHER='liming' and sc.cno=c.cno)2
    select s.sno,max(s.sname),avg(sc.成绩) 
    from s,sc
    where s.sno=sc.sno
    group by s.sno
    having avg(sc.成绩)<60 and count(s.sno)=23
    select s.*
    from s,
    (select sno
    from sc
    where cno in(1,2)
    group by sno
    having count(sno)=2) b
    where s.sno=b.sno
      

  8.   

    惭愧啊,看的不太懂,数据库查询 还可以用exists啊?
    exists是关键字吗?
    不知道啊
      

  9.   

    你应该在去免试前把SQL语句的一些比较常用的语句熟悉下再去考。。这些我们上课都做过类似的- -虽然现在忘了,但是查查语法还是能想起来的。。看来我找工作前也得好好再看看数据库这块啦。。现在自己做项目都是简单的建表和增删