查询、课程-Oracle数据库之SQL语句练习-by小雨

本文纯属个人见解,是对前面习学的总结,如有描述不确正的地方还请高手指正~

             由于被拉去行进建模赛比,小生的sql训练划计直接被延后了三天。趁着今晚没课(逃了)将这一套

     题集行进一下训练,强固下之前的习学。须要说明的是,训练所针对的据数都是来自上一盘文章中的

     http://blog.csdn.net/kiritor/article/details/8790214。据数未几,也就是生学、课程的关相息信。

             1、查询“C001”课程比“C002”课程绩成高的全部生学的息信。    

            ☞  在from子句面后用使子查询

select  s.*,a.cno,a.score from student s, 
 (
 select * from score where cno='C001'
 )a,
 (
  select * from score where cno='C002'
 )b
 where a.sno=b.sno and a.score>=b.score and s.SNO = a.SNO
 ;

                用使关相子查询方法实现

select * from student s, score a
   where a.cno ='C001'
   and exists
    (
       select * from score b 
       where b.cno='C002'
       and a.score >=b.score
       and a.sno = b.sno    
    )  
    and s.sno =a.sno
 ;

                

    2、查询均平绩成大于60分的全部生学的息信,括包其均平绩成

                       第一种方法

    

select s.sno,t.sname, avg(s.score) from student t, score s
 where t.sno=s.sno 
  group by (s.sno,t.sname) 
  having avg(s.score)>60
 ;

               

    ☞  第二种方法

    

select t.sno ,t.sname, m.avg_s from student t,
   (
     select s.sno, avg(s.score) avg_s from  score s
     group by s.sno having avg(s.score)>60
   ) m
   where m.sno = t.sno
 ;

                 3、查询全部同窗的姓名、学号、总绩成、选课数

    

/*思绪:可以道知的是选课数、总绩成可以通过
   子查询中的置内函数查询出来的*/   
--首先查询出总绩成与选课数
select sum(score) ,count(cno) from score group by sno;
--后之查询生学的学号姓名等息信就牵强附会了
select t.* ,tmp.sum_sc,tmp.sum_cn from student t,
  (
    select sno, sum(score) sum_sc ,count(cno) sum_cn from score group by sno
  ) tmp
 where t.sno = tmp.sno
 ;

                 4、查询姓为“刘”的教师的息信

    

select * from teacher where tname like '刘%'; 
 select count(*) from teacher where tname like '刘%';

                5、查询学过“王燕”教师课的同窗的学号、姓名

                         思考:首先查询出王燕教师传授的课程的编号

                        第一种方法

select  t.* ,s.cno,s.score from student t, score s
 where s.cno in    
   (
     select distinct cno from course c,teacher t
     where c.tno = 
      (
     select tno from teacher where tname='王燕'
      ) 
   ) 
   and t.sno = s.sno
;

                

    ☞  第二种方法

    

select * from student st 
    where st.sno  in
     (
       select distinct sno from score s join course c
         on s.cno=c.cno
         join teacher t on c.tno=t.tno 
         where tname='王燕'
     )  
;

                  6、查询学过“c001”并且也学过编号“c002”课程的同窗的学号、姓名

    

--通过连接的方法实现
select * from score s
 join score a on s.sno = a.sno 
 join student st on st.sno = s.sno
 where s.cno='C001' and a.cno = 'C002'
 and st.sno = s.sno
 ;

               7、查询课程编号‘COO2’的绩成比课程编号为'C001'的绩成低的生学的全部息信。

                     呃,是不是有种似曾相识的感到呢,和第一题没有区分嘛,不过我们用采子查询的

                     方法来实现。

    

select * from student t
 join score a on t.sno = a.sno
 join score b on t.sno = b.sno
 where a.cno = 'C002' 
     and b.cno ='C001'
     and a.score <= b.score
;

             

   哈哈用使连接的方法看起来更加简略吧!

                    8、查询全部课程绩成都小于60分的生学的学号等息信

                      先来看看一种经常误以为是确正的查询吧!小生是在网上找的题库

                      案答什么的感到感到有些题问啊,还是自己斟酌吧

                      错误的查询:

    

select st.*,s.score from student st
 join score s on st.sno=s.sno
 join course c on s.cno=c.cno
 where s.score <60

                   

    很轻易的可以道知这个查询只要有小于60分的课程都市查到,这其实不符合目题的要求

                     下一种查询方法:

                           思考全部的课程小于60,就是不存在某个生学的某门课程大于60分

select t.*  from student t
  where  
     not  exists
     (
        select * from score s 
         where s.score >60.9  and t.sno = s.sno
     )
     and t.sno in
      (
         select sno from score
      )   
 ;

             

    9、查询没有学完全部课程的生学的息信

                          

                           思考::

                             1、我们该应道知统共的课程数
                         2、再在score表中查询,按照sno分组、并
                               去重,加添having子句
         

select  t.sno,t.sname from student t 
 left join  score on t.sno=score.sno
 group by t.sno,t.sname
 having count(score.cno)<
  (
    select count(distinct cno) from course
  )
;

                 10、查询至少有一门课与学号为‘S001’所选的课一样的

                    思绪:首先查询出学号为S001生学所选
                                 的全部课程的息信,后之行进判断

select  t.sno,t.sname ,score.cno from student t 
left join score on t.sno= score.sno
where score.cno in
  (
    select distinct cno from score where sno='S001'
  )  
  and t.sno <>'S001'
;

              第一阶段的训练就到这儿了。

 

文章结束给大家分享下程序员的一些笑话语录: 面试官:熟悉哪种语言
应聘者:JAVA
面试官:知道什么叫类么
应聘者:我这人实在,工作努力,不知道什么叫累
面试官:知道什么是包?
应聘者:我这人实在 平常不带包 也不用公司准备了
面试官:知道什么是接口吗?
应聘者:我这个人工作认真。从来不找借口偷懒
面试官:知道什么是继承么
应聘者:我是孤儿没什么可以继承的
面试官:知道什么叫对象么?
应聘者:知道,不过我工作努力,上进心强,暂时还没有打算找对象。
面试官:知道多态么?
应聘者:知道,我很保守的。我认为让心爱的女人为了自已一时的快乐去堕胎是不道德的行为!请问这和C#有什么关系??

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值