oracle经典sql练习题
2011-08-19 09:04
741 查看
/*1、选择在部门30中员工的所有信息*/ select * from scott.emp where deptno = '30' /*2、列出职位为(MANAGER)的员工的编号,姓名 */ select empno, ename from scott.emp where job = 'MANAGER' /*3、找出奖金高于工资的员工*/ select * from scott.emp where comm > sal /*4、找出每个员工奖金和工资的总和 */ select ename, sal + nvl(comm, 0) from scott.emp /*5、找出部门10中的经理(MANAGER)和部门20中的普通员工(CLERK) */ select * from scott.emp where deptno = '10' and job = 'MANAGER' union select * from scott.emp where job = 'CLERK' and deptno = '20' /*6、找出部门10中既不是经理也不是普通员工,而且工资大于等于2000的员工 */ select * from scott.emp where job != 'MANAGER' and job != 'CLERK' and sal > 2000 /*7、找出有奖金的员工的不同工作 */ select distinct(job) from scott.emp where comm is not null /*8、找出没有奖金或者奖金低于500的员工*/ select * from scott.emp where comm is not null and comm > 500 /*9、显示雇员姓名,根据其服务年限,将最老的雇员排在最前面 */ select ename from scott.emp order by (months_between(sysdate, hiredate) / 12) desc select ename,hiredate from scott.emp order by hiredate /*10、找出每个月倒数第三天受雇的员工*/ select * from scott.emp where hiredate = last_day(hiredate) - 2 /*11、分别用case和decode函数列出员工所在的部门,deptno=10显示'部门10', deptno=20显示'部门20' deptno=30显示'部门30' deptno=40显示'部门40' 否则为'其他部门'*/ select ename, case deptno when 10 then '部门10' when 20 then '部门20' when 30 then '部门30' when 40 then '部门40' else '其他部门' end 工资情况 from scott.emp select ename, decode(deptno, 10, '部门10', 20, '部门20', 30, '部门30', 40, '部门40', '其他部门') 工资情况 from scott.emp /*12、分组统计各部门下工资>500的员工的平均工资*/ select avg(sal) from scott.emp where sal > 500 group by deptno /*13、统计各部门下平均工资大于500的部门*/ select deptno from scott.emp group by deptno having avg(sal) > 500 /*14、算出部门30中得到最多奖金的员工奖金 */ select max(comm) from scott.emp where deptno = 30 /*15、算出部门30中得到最多奖金的员工姓名*/ select ename from scott.emp where deptno = 30 and comm = (select max(comm) from scott.emp where deptno = 30) /*16、算出每个职位的员工数和最低工资*/ select count(ename), min(sal), job from scott.emp group by job /*17、列出员工表中每个部门的员工数,和部门no */ select count(ename), deptno from scott.emp group by deptno /*18、得到工资大于自己部门平均工资的员工信息*/ select * from scott.emp e where sal > (select avg(sal) from scott.emp where e.deptno = deptno) select * from scott.emp e1, (select avg(sal) sals, deptno from scott.emp group by deptno) e2 where sal > sals and e1.deptno = e2.deptno /*19、分组统计每个部门下,每种职位的平均奖金(也要算没奖金的人)和总工资(包括奖金) */ select avg(nvl(comm,0)), sum(sal + nvl(comm, 0)) from scott.emp group by deptno,job /*20、笛卡尔集*/ select * from scott.emp, scott.dept /*21、显示员工ID,名字,直属主管ID,名字*/ select empno, ename, mgr, (select ename from scott.emp e1 where e1.empno = e2.mgr) 直属主管名字 from scott.emp e2 /*22、DEPT表按照部门跟EMP表左关联*/ select * from scott.dept, scott.emp where scott.dept.deptno = scott.emp.deptno(+) /*23、使用此语句重复的内容不再显示了*/ select distinct (job) from scott.emp /*24、重复的内容依然显示 */ select * from scott.emp UNION ALL select * from scott.emp /*23和24题和22题是一样的 */ /*25、只显示了两个表中彼此重复的记录。*/ select * from scott.dept, scott.emp where scott.dept.deptno(+) = scott.emp.deptno /*26、只显示了两张表中的不同记录*/ select * from scott.emp union select * from scott.emp minus (select * from scott.emp intersect select * from scott.emp) (select * from scott.emp minus select * from scott.emp) union (select * from scott.emp minus select * from scott.emp) 表结构相同 先union 只能有 - /*27、列出员工表中每个部门的员工数,和部门no */ select count(ename), deptno from scott.emp group by deptno /*28、列出员工表中每个部门的员工数(员工数必须大于3),和部门名称*/ select count(deptno), deptno, (select dname from scott.dept where scott.dept.deptno = e1.deptno) from scott.emp e1 group by deptno having count(deptno)>3 /*29、找出工资比jones多的员工*/ select * from scott.emp where sal > (select sal from scott.emp where ename = 'JONES') /*30、列出所有员工的姓名和其上级的姓名 */ select ename, (select ename from scott.emp e1 where e1.empno = e2.mgr) 上级的姓名 from scott.emp e2 /*31、以职位分组,找出平均工资最高的两种职位 */ select job from scott.emp group by job having avg(sal) in (select max(sal) from scott.emp group by job ) select job from (select job, avg(sal) from scott.emp group by job order by avg(sal) desc) where rownum <= 2 最大的: select max(max_sal) from (select job, avg(sal) max_sal from scott.emp group by job) /*32、查找出不在部门20,且比部门20中任何一个人工资都高的员工姓名、部门名称*/ select ename, dname from scott.emp e1, scott.dept e2 where e1.deptno = e2.deptno and e1.deptno <> 20 and sal > (select max(sal) from scott.emp where deptno = '20') /*33、得到平均工资大于2000的工作职种 */ select job from scott.emp group by job having avg(sal) > 2000 /*34、分部门得到工资大于2000的所有员工的平均工资,并且平均工资还要大于2500 */ select avg(sal) from scott.emp where sal > 2000 group by deptno having avg(sal) > 2500 /*35、得到每个月工资总数最少的那个部门的部门编号,部门名称,部门位置 */ select deptno, dname, loc from scott.dept where deptno in (select deptno from scott.emp group by deptno having sum(sal) = (select min(sum(sal)) from scott.emp group by deptno)) select * from scott.dept /*36、分部门得到平均工资等级为2级(等级表)的部门编号 */ select deptno from scott.emp group by deptno having avg(sal) between (select losal from scott.salgrade where grade = 2) and (select hisal from scott.salgrade where grade = 2) select avg(sal) from scott.emp group by deptno select * from scott.salgrade /*37、查找出部门10和部门20中,工资最高第3名到工资第5名的员工的员工名字,部门名字,部门位置*/ select a.ename, dname, loc from (select * from (select rownum rn, deptno, empno, sal, ename from (select deptno, empno, sal, ename from scott.emp where deptno in (10, 20) and rownum <= 5 order by sal desc)) where rn between 3 and 5) a, scott.dept b where a.deptno = b.deptno select deptno, ename from (select empno, deptno, ename from (select rownum rn, deptno, empno, sal, ename from (select deptno, empno, sal, ename from scott.emp where deptno in (10, 20) order by sal desc)) where rn between 3 and 5) /*38、查找出收入(工资加上奖金),下级比自己上级还高的员工编号,员工名字,员工收入*/ select empno, ename, sal + nvl(comm, 0) from scott.emp e1 where sal + nvl(comm, 0) > (select sal + nvl(comm, 0) from scott.emp where empno = e1.mgr) select * from scott.emp select ename, sal + nvl(comm, 0) from scott.emp /*39、查找出职位和'MARTIN' 或者'SMITH'一样的员工的平均工资 */ select avg(sal) from scott.emp where job in (select job from scott.emp where ename = 'MARTIN' or ename = 'SMITH') /*40、查找出不属于任何部门的员工 */ select * from scott.emp where deptno is null select * from scott.emp where deptno not in (select deptno from scott.emp) /*41、按部门统计员工数,查处员工数最多的部门的第二名到第五名(列出部门名字,部门位置)*/ select dname, loc from (select * from (select rownum rn, deptno from (select deptno, count(*) from scott.emp group by deptno order by count(*) desc)) where rn between 2 and 5) a, scott.dept b where a.deptno = b.deptno select count(*) from scott.emp group by deptno /*42、查询出king所在部门的部门号\部门名称\部门人数 (多种方法)*/ select sc.deptno, dname, count(*) from scott.emp sc, scott.dept de where sc.deptno = ((select deptno from scott.emp where ename = 'KING')) and de.deptno = sc.deptno group by sc.deptno, dname /*43、查询出king所在部门的工作年限最大的员工名字*/ select * from scott.emp where hiredate = (select min(hiredate) from scott.emp where deptno in (select deptno from scott.emp where ename = 'KING')) and deptno = (select deptno from scott.emp where ename = 'KING') /*44、查询出工资成本最高的部门的部门号和部门名称 */ select deptno, dname from scott.dept where deptno = (select deptno from scott.emp group by deptno having sum(sal) = (select max(sum(sal)) from scott.emp group by deptno)) select * from scott.emp for update
相关文章推荐
- oracle经典sql练习题
- oracle经典sql练习题
- oracle经典sql练习题
- Oracle、PL_SQL经典练习题
- Oracle编程入门经典 第9章 掌握SQL*Plus
- 经典SQL练习题
- oracle SQL里常用的时间函数,经典推荐
- Oracle 经典练习题
- oracle心得3--多表查询@分组查询@子查询讲解与案例分析@经典练习题
- ORACLE SQL:经典查询练习
- 经典SQL练习题2----emp/dept
- Oracle SQL:经典入门级查询练习(1)
- [强烈推荐]ORACLE SQL:经典查询练手第四篇(不懂装懂,永世饭桶!)
- 【MySQL】经典数据库SQL语句编写练习题——SQL语句扫盲
- Oracle SQL:经典查询练手第三篇
- ORACLE SQL:经典查询练手第一篇
- 经典SQL练习题
- 经典SQL练习题
- Oracle学习总结(5)—— SQL语句经典案例
- SQL练习题,MySQL数据库查询语句经典例题练习,附带答案和运行效果截图以供参考