Oracle 查詢練習題
阿新 • • 發佈:2018-11-09
**練習題** 1.各科前三 select * from sc sc1 where (select count(*) from sc sc2 where sc1.cid=sc2.cid and sc2.score>sc1.score )<=2 內層課程號= 外層課程號 內層分數比外層分數 大的 少於2個 2.分頁查詢 select * from ( select rownum rn, empno ,ename ,job ,sal from (select e.empno ,e.ename ,e.job ,e.sal from emp e order by nvl(sal,0) desc ) ) t where t.rn between 5 and 10 3.行列轉換 select place, sum(decode(sales.name, 'A', sales.sales)) A, sum(decode(sales.name, 'B', sales.sales)) B, sum(decode(sales.name, 'C', sales.sales)) C from sales group by place 查出來之後分組 4.選修相同課程的同學 select sid from stu where sid <> (select sid from stu where sname = '小張') and sid in ( (select sid from record r1 where r1.cid in (select cid from record --小張的 選課的 所有cid where sid = (select sid from stu where sname = '小張') ) group by sid --count (*) 統計 別人 包含的小張的課 的數量 having count(*) = (select count(*) from record --等於小張的課程數 where sid = (select sid from stu where sname = '小張'))) ) and sid in (select sid from record --外層套 課程數=小張的sid count分組 group by sid having count(cid) = (select count(*) from record --找小張的課程數 where sid = (select sid from stu where sname = '小張') ) ) 5.成績分級 select e.empno,e.ename, e.job ,e.sal ,e.hiredate, initcap( decode (sign(sal-801),-1,'e', decode (sign(sal-1601),-1,'d', decode(sign(sal-2501),-1,'c', decode(sign(sal-2801),-1,'b', decode(sign(sal-3001),-1,'a', decode(sign(sal-5001),-1,'s') ) ) ) ) ) ) "Grade" from emp e; 子查詢 6.修改張三教授的課程的成績為對應課程的平均成績 update sc sc1 set score = (select avg(score ) from sc where cid = sc1.cid) where cid in (select cid from course where tid in (select tid from teacher where tname ='張三')) 7.相同姓名性別 人數 select sname,ssex,(select count(*) from student s2 where s2.sname=s.sname and s2.ssex=s.ssex) 人數 from student s where (select count(*) from student s2 where s2.sname=s.sname and s2.ssex=s.ssex)>1 8.不同課程成績相同的學生 select * from sc sc1 where (select count(*) from sc sc2 where sc1.score=sc2.score and sc1.cid<>sc2.cid --直接排除原記錄 group by score )>0