Oracle数据库之SQL语句练习(2)

发表于:2013-05-31来源:Csdn作者:一个小菜仔点击数: 标签:oracle
) ) and t.sno = s.sno ; 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=王燕 ) ) an

  )

  )

  and t.sno = s.sno

  ;

  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

  ;

  ☞ 第二种方式

  [sql] view plaincopyprint?

  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='王燕'

  )

  ;

  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”课程的同学的学号、姓名

  [sql] view plaincopyprint?

  --通过连接的方式实现

  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

  ;

  --通过连接的方式实现

  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'的成绩低的学生的所有信息。

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

  方式来实现。

  [sql] view plaincopyprint?

  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

  ;

  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分的学生的学号等信息

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

  答案什么的感觉感觉有些问题啊,还是自己推敲吧

  错误的查询:

  [sql] view plaincopyprint?

  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

  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分

  [sql] view plaincopyprint?

  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

  )

  ;

  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子句

  [sql] view plaincopyprint?

  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

  )

  ;

  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’所选的课一样的

原文转自:http://blog.csdn.net/kiritor/article/details/8805310