Oracle數據庫之SQL語句練習

發表于:2013-05-31來源:Csdn作者:一個小菜仔點擊數: 標簽:oracle
由于被拉去進行建模比賽,小生的sql練習計劃直接被延后了三天。趁著今晚沒課(逃了)將這一套 題集進行一下練習,鞏固下之前的學習。需要說明的是,練習所針對的數據都是來自上一盤文章中的

  由于被拉去進行建模比賽,小生的sql練習計劃直接被延后了三天。趁著今晚沒課(逃了)將這一套

  題集進行一下練習,鞏固下之前的學習。需要說明的是,練習所針對的數據都是來自上一盤文章中的

  http://blog.csdn.net/kiritor/article/details/8790214。數據不多,也就是學生、課程的相關信息。

  1、查詢“C001”課程比“C002”課程成績高的所有學生的信息。

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

  [sql] view plaincopyprint?

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

  ;

  ☞ 使用相關子查詢方式實現

  [sql] view plaincopyprint?

  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

  ;

  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分的所有學生的信息,包括其平均成績

  ☞ 第一種方式

  [sql] view plaincopyprint?

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

  ;

  ☞ 第二種方式

  [sql] view plaincopyprint?

  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

  ;

  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、查詢所有同學的姓名、學號、總成績、選課數

  [sql] view plaincopyprint?

  /*思路:可以知道的是選課數、總成績可以通過

  子查詢中的內置函數查詢出來的*/

  --首先查詢出總成績與選課數

  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

  ;

  /*思路:可以知道的是選課數、總成績可以通過

  子查詢中的內置函數查詢出來的*/

  --首先查詢出總成績與選課數

  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、查詢姓為“劉”的老師的信息

  [sql] view plaincopyprint?

  select * from teacher where tname like '劉%';

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

  select * from teacher where tname like '劉%';

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

  5、查詢學過“王燕”老師課的同學的學號、姓名

  思考:首先查詢出王燕老師教授的課程的編號

  ☞ 第一種方式

  [sql] view plaincopyprint?

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

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

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97