码迷,mamicode.com
首页 > 数据库 > 详细

Oracle 查询练习

时间:2019-09-11 09:24:21      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:dep   osal   card   manager   man   直接   order   内容   like   

  非常经典的一些日常醒脑练习内容!!
                     如有更高效的写法欢迎赐教!    



1
1.已知Oracle的Scott用户中提供了三个测试数据库表,名称分别为dept,emp和salgrade。使用SQL语言完成以下操作 2 1)试用SQL语言完成下列查询(单表查询): 3 a)查询20号部门的所有员工信息: 4 select * from emp e where e.deptno=20 5 b)查询奖金(COMM)高于工资(SAL)的员工信息: 6 select * from emp where comm>sal;  7 c)查询奖金高于工资的20%的员工信息: 8 select * from emp where comm>sal*0.2 9 d)查询10号部门中工种为MANAGER和20号部门中工种为CLERK的员工的信息: 10 select * from emp e   11 where (e.deptno=10 and e.job=MANAGER 12 or (e.deptno=20 and e.job=CLERK) ;  13 e)查询所有工种不是MANAGER和CLERK,且工资大于或等于2000的员工的详细信息: 14 select * from emp  15 where job not in(MANAGER,CLERKand sal>=2000 16 f)查询没有奖金或奖金低于100的员工信息: 17 select * from emp where comm is null or comm<100 18 g)查询员工工龄大于或等于10年的员工信息: 19 select * from emp where (sysdate-hiredate)/365>=10;   20 h)查询员工信息,要求以首字母大写的方式显示所有员工的姓名: 21 第一种写法:  22 select initcap(ename) from emp; 23 24 第二种写法:  25 select upper(substr(ename,1,1))||lower(substr(ename,2)) from emp;  26 i)查询在2月份入职的所有员工信息: 27 select * from emp where to_char(hiredate,MM)=02 28 j)显示所有员工的姓名、入职的年份和月份,按入职日期所在的月份排序,若月份相同则按入职的年份排序: 29 select ename,to_char(hiredate,yyyyyear,to_char(hiredate,MM) 30 month 31 from emp  32 order by month,year; 33 k)查询JONES员工及所有其直接、间接下属员工的信息: 34 select e.* from emp e  35 start with ename=JONES  36 connect by prior empno=mgr;  37 l)查询SCOTT员工及其直接、间接上级员工的信息: 38 select e.* from emp e  39 start with ename=SCOTT  40 connect by prior mgr=empno;  41 2)试用SQL语言完成下列查询(多表查询): 42 a)查询从事同一种工作但不属于同一部门的员工信息: 43 select a.ename,a.job,a.deptno,b.ename,b.job,b.deptno  44 from emp a,emp b  45 where a.job=b.job and a.deptno<>b.deptno;  46 b)查询各个部门的详细信息以及部门人数、部门平均工资: 47 select d.deptno,count(e.empno),avg(e.sal),d.dname,d.loc  48 from emp e ,dept d  49 where e.deptno=d.deptno  50 group by d.deptno,d.dname,d.loc;  51 3)试用SQL语言完成下列查询(嵌套子查询): 52 a)查询10号部门员工以及领导的信息: 53 select * from emp where empno in 54 select mgr from emp where deptno=10or deptno=10 55 b)查询工资为某个部门平均工资的员工信息: 56 select * from emp  57 where sal in(select avg(sal) from emp group by deptno);  58 c)查询工资高于本部门平均工资的员工的信息:  59 select * from emp e1  60 where sal >(select avg(sal) from emp e2 where e2.deptno=e1.deptno);  61 d)查询工资高于本部门平均工资的员工的信息及其部门的平均工资: 62 select e.*,a.avgsal  63 from emp e,  64 (select deptno,avg(sal) as avgsal from emp group by deptno) a where a.deptno=e.deptno and e.sal>a.avgsal;  65 4)试用SQL语言完成下列查询(聚合函数): 66 a)统计各个工种的人数与平均工资: 67 select count(*),e.job,avg(e.sal) from emp e  68 group by e.job; 69 b)统计每个部门中各个工种的人数与平均工资: 70 select deptno,job,count(empno),avg(sal) from emp e  71 group by e.deptno,e.job; 72 c)查询所有员工入职以来的工作期限,用“******日”的形式表示。  73 select e.ename,floor((sysdate-e.hiredate)/365)|| || floor(mod((sysdate-e.hiredate),365)/30) || || floor(mod(mod((sysdate-e.hiredate),365),30))|| from emp e; 74 d)查询人数最多的部门信息: 75 select * from dept  76 where deptno in  77 (select deptno from  78 (select count(*count,deptno from emp group by deptno)  79 where count in 80 (select max(countfrom  81 (select count(*count ,deptno from emp group by  deptno) 82 ) 83 );  84 e)以树状结构查询所有员工与领导之间的层次关系: 85 select substr(sys_connect_by_path(ename,->),3),level  86 from emp  87 start with mgr is null  88 connect by prior empno=mgr;  89 f)部门平均薪水最高的部门编号: 90 第一种方法: 91 select * from 92 select avg(sal) avgsal,deptno   93 from emp group by deptno order by avgsal desc)   94 where rownum=1 95    96 第二种方法: 97 select deptno,avg(sal) from emp group by deptno having avg(sal)=( 98 select max(avg(sal)) avgsal  99 from emp group by deptno)  100 g)部门平均薪水最高的部门名称: 101 select d.* from dept d where deptno  102 in(select deptno from emp group by deptno having avg(sal)= 103 (select max(avg(sal)) avgsal from emp group by deptno))  104 h)平均薪水最低的部门的部门名称: 105 select d.* from dept d where deptno  106 in(select deptno from emp group by deptno having avg(sal)= 107 (select min(avg(sal)) avgsal from emp group by deptno))  108 i)平均薪水等级最低的部门的部门名称: 109 select d.dname from dept d  110 where d.deptno in (select a.deptno from  111 (select e.deptno from emp e,salgrade s  112 where (e.sal between s.losal and s.hisal)  113 group by e.deptno order by avg(s.grade)) a  114 where rownum=1);  115 j)部门经理人中,薪水最低的部门名称: 116 select dname from dept where deptno=  117 (select deptno from  118 (select deptno from emp where job=MANAGER group by deptno 119 order by min(sal)) where rownum=1);  120 k)比普通员工的最高薪水还要高的经理人名称: 121 select ename from emp where sal>  122 (select max(sal) from emp where job not in  123 (MANAGER,PRESIDENT)) and job=MANAGER or job=PRESIDENT; 124 5)试用SQL语言完成下列查询(嵌套子查询): 125 a)查询所有员工工资都大于1000的部门的信息: 126 select * from dept where deptno in  127 (select deptno from emp  128 where deptno not in  129 (select distinct deptno from emp where sal<1000));  130 b)查询所有员工工资都大于1000的部门的信息及其员工信息: 131 select * from emp e join dept d  132 on d.deptno  133 in (select deptno from emp  134 where deptno not in  135 (select distinct deptno from emp where sal<1000))  136 and d.deptno=e.deptno;  137 c)查询所有员工工资都在900~3000之间的部门的信息:  138 select * from dept  139 where deptno not in 140 (select deptno from emp  141 where sal not between 900 and 3000);  142 d)查询所有工资都在900~3000之间的员工所在部门的员工信息: 143 select * from emp a  144 where a.deptno in  145 (select distinct e.deptno from emp e  146 where e.sal between 900 and 3000);  147 e)查询每个员工的领导所在部门的信息: 148 select d.* from dept d  149 where d.deptno in  150 (select distinct e2.deptno from emp e1,emp e2  151 where e1.empno=e2.mgr);  152 f)查询30号部门中工资排序前3名的员工信息: 153 select * from  154 (select sal from emp where deptno=30 order by sal desc) e 155 where rownum<4 156 g)查询工作等级为2级,1985年以后入职的工作地点为DALLAS的员工编号、姓名和工资: 157 select e.ename,e.empno,e.sal from emp e,salgrade s,dept d  158 where (e.sal between s.losal and s.hisal)  159 and (s.grade=2)   160 and to_char(e.hiredate,yyyy)>1985  161 and e.deptno=d.deptno  162 and d.loc=DALLAS163 6)用SQL语句完成下列操作: 164 a)将各部门员工的工资修改为该员工所在部门平均工资加1000: 165 update emp e set sal=  166 1000+(select avg(sal) from emp where deptno=e.deptno);  167 b)删除重复部门,只留下一项:  168 delete from dept d  169 where rowid<>  170 (select min(rowid) from dept where dname=d.dname and d.loc=loc); 171 c)更新员工工资为他的主管的工资,奖金: 172 第一种方法: 173 update emp e set sal=(select sal from emp where empno=e.mgr), comm=(select comm from emp where empno=e.mgr)  174 第二种方法: 175 update emp e set (sal,comm)=(select sal,comm from emp whereempno=e.mgr); 176 177 2.(可选题)某大学图书馆为了更好管理图书,使用Oracle数据库建立了三个表: 178 CARD 借书卡表: CNO(卡号),NAME (姓名),CLASS (班级); 179 BOOKS 图书表: BNO(书号),BNAME (书名), AUTHOR (作者),PRICE (单价),QUANTITY (库存册数); 180 BORROW 借书记录表: CNO (借书卡号),BNO (书号),RDATE (还书日期); 181 备注:限定每人每种书只能借一本;库存册数随借书、还书而改变。 182 1)试用SQL语言完成下列操作: 183 a)写出建立BORROW表的SQL语句,要求定义主码完整性约束和引用完整性约束: 184 CREATE TABLE BORROW( 185 CNO NUMBER REFERENCES CARD(CNO), 186 BNO NUMBER REFERENCES BOOKS(BNO), 187 RDATE date, 188 PRIMARY KEY(CNO,BNO) 189 ); 190 b)假定在建BOOKS表时没有定义主码,写出为BOOKS表追加定义主码的语句: 191 ALTER TABLE BOOKS ADD PRIMARY KEY(BNO) ;  192 c)将CARD 表的NAME最大列宽增加到10个字符(假定原为6个字符): 193 ALTER TABLE CARD MODIFY NAME varchar2(10) ; 194 d)为该表增加1列NAME(系名),可变长,最大20个字符: 195 ALTER TABLE CARD ADD 系名 varchar2(20) ; 196 2)试用SQL语言完成下列查询: 197 a)找出借书超过5本的读者,输出借书卡号及所借图书册数: 198 SELECT CNO, COUNT(*) FROM BORROW GROUP BY CNO HAVING COUNT(*)>5; 199 b)查询借阅了"水浒"一书的读者,输出姓名及班级: 200 SELECT NAME, CLASS FROM CARD WHERE CNO IN (SELECT CNO FROM BORROW BW, BOOKS BK WHERE BW.BNO=BK.BNO AND BK.BNAME=水浒) ; 201 c)查询过期未还图书,输出借阅者(卡号)、书号及还书日期: 202 SELECT * FROM BORROW WHERE RDATE<SYSDATE; 203 d)查询书名包括"网络"关键词的图书,输出书号、书名、作者: 204 SELECT BNO,BNAME,AUTHOR FROM BOOKS WHERE BNAME LIKE %网络%; 205 e)查询现有图书中价格最高的图书,输出书名及作者: 206 SELECT BNAME,AUTHOR FROM BOOKS WHERE PRICE=(SELECT MAX(PRICE) FROM BOOKS) ; 207 f)查询当前借了"计算方法"但没有借"计算方法习题集"的读者,输出其借书卡号,并按卡号降序排序输出: 208 SELECT a.CNO FROM BORROW a,BOOKS b WHERE a.BNO=b.BNO AND b.BNAME=计算方法 AND a.CNO NOT INSELECT aa.CNO FROM BORROW aa,BOOKS bb WHERE aa.BNO=bb.BNO AND bb.BNAME=计算方法习题集)ORDER BY a.CNO DESC; 209 g)查询当前同时借有"计算方法"和"组合数学"两本书的读者,输出其借书卡号,并按卡号升序排序输出: 210 SELECT DISTINCT a.CNO FROM BORROW a,BOOKS b WHERE a.BNO=b.BNO AND b.BNAME IN(计算方法,组合数学) ORDER BY a.CNO; 211 3)试用SQL语言完成下列操作: 212 a)将"C01"班同学所借图书的还期都延长一周: 213 UPDATE BORROW SET RDATE=RDATE+7 WHERE CNO IN (SELECT DISTINCT CNO FROM CARD WHERE CLASS=C01); 214 b)从BOOKS表中删除当前无人借阅的图书记录: 215 DELETE FROM BOOKS WHERE BNO NOT IN(SELECT DISTINCT BK.BNO FROM BORROW BR, BOOKS BK WHERE BR.BNO=BK.BNO); 216 4)试用SQL语言完成下列操作: 217 a)如果经常按书名查询图书信息,请建立合适的索引: 218 CREATE INDEX IDX_BOOKS_BNAME ON BOOKS(BNAME) ; 219 b)在BORROW表上建立一个触发器,完成如下功能:如果读者借阅的书名是"数据库技术及应用",就将该读者的借阅记录保存在BORROW_SAVE表中(注ORROW_SAVE表结构同BORROW表): 220 CREATE TRIGGER TR_SAVE ON BORROWFOR INSERT,UPDATEASIF @@ROWCOUNT>0 INSERT BORROW_SAVE SELECT i.*FROM INSERTED i,BOOKS b WHERE i.BNO=b.BNO AND b.BNAME=N数据库技术及应用; 221 c)建立一个视图,显示"力01"班学生的借书信息(只要求显示姓名和书名): 222 CREATE VIEW V_VIEWASSELECT a.NAME,b.BNAMEFROM BORROW ab,CARD a,BOOKS bWHEREab.CNO=a.CNO AND ab.BNO=b.BNO AND a.CLASS=N力01;

 

Oracle 查询练习

标签:dep   osal   card   manager   man   直接   order   内容   like   

原文地址:https://www.cnblogs.com/arebirth/p/oracledayonce.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!