SQL练习题    
 
 注:查询列表不建议用 “*”
1.列出至少有一个雇员的所有部门;
a.
select * from dept where deptno in(select distinct deptno from emp);
b.
(oracle11gCBO 新特性 in(多个值)会智能过滤掉重复字段,通过执行计划验证);
select * from dept where deptno in (select deptno from emp group by deptno having count(deptno)>=1);
c.
select * from dept a where exists (select null from emp b where a.deptno=b.deptno);
 
2.列出薪金比smith多的所有雇员
select ename from emp where sal>(select sal from emp where ename='SMITH');
 
3.列出雇员的姓名及其直接上级姓名(注:列出执行计划原由于显示结果不同,结果显示第二个执行计划要优秀一点,第一个执行计划走了全表扫描也符合常理,第二个走的唯一索引,索引>堆表;测试表较小没有参考价值仅得出结果不同,有利于以后做查询优化)
a.
select e.ename,p.ename from emp e,emp p where  p.empno(+)=e.mgr;
select p.ename,e.ename from emp p left join emp e on e.empno=p.mgr;

  

执行计划:
Execution Plan
----------------------------------------------------------
Plan hash value: 2341341676 ---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 280 | 7 (15)| 00:00:01 |
|* 1 | HASH JOIN OUTER | | 14 | 280 | 7 (15)| 00:00:01 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 140 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| EMP | 14 | 140 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 1 - access("E"."EMPNO"(+)="P"."MGR") Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
15 consistent gets
0 physical reads
0 redo size
823 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
14 rows processed

  

 
 
b.
select e.ename,(select ename from emp p where  p.empno=e.mgr)as BoosName from emp e;

  

执行计划:
Execution Plan
----------------------------------------------------------
Plan hash value: 4000517069 --------------------------------------------------------------------------------
------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
| --------------------------------------------------------------------------------
------ | 0 | SELECT STATEMENT | | 14 | 140 | 3 (0)| 00:0
0:01 | | 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 10 | 1 (0)| 00:0
0:01 | |* 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 0 (0)| 00:0
0:01 | | 3 | TABLE ACCESS FULL | EMP | 14 | 140 | 3 (0)| 00:0
0:01 | --------------------------------------------------------------------------------
------ Predicate Information (identified by operation id):
--------------------------------------------------- 2 - access("P"."EMPNO"=:B1) Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
17 consistent gets
0 physical reads
0 redo size
849 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
14 rows processed

  

 
 
 
4.列出入职日期早于其直接上级的所有雇员
a.
select p.ename from emp e,emp p where e.empno=p.mgr and p.hiredate<e.hiredate;

  

b.
select e.ename from emp e where e.hiredate<(select  hiredate from emp p where p.empno=e.mgr);

  

 
5、列出部门名称和这些部门的雇员,同时列出那些没有雇员的部门
a.
select a.dname,b.ename from dept a,emp b where  a.deptno=b.deptno(+);

  

 
b.
select a.dname,b.ename from dept a left join emp b on  a.deptno=b.deptno

  

 
6、列出所有“CLERK”(办事员)的姓名及其部门名称
select b.ename,a.dname from dept a,emp b where  a.deptno=b.deptno and b.job='CLERK';

  

 
7、列出各种工作类别的最低薪金,显示最低薪金大于1500的记录
select min(sal)as minsal,job from emp group by job having  min(sal)>1500;

  

 
--8、列出从事“SALESMAN”(销售)工作的雇员的姓名,假定不知道销售部的部门编号
select ename from emp where deptno=(select deptno from  dept where dname='SALES');

  

 
9、列出薪金高于公司平均水平的所有雇员

select ename from emp where sal>(select avg(sal) from  emp);

  

 
10、列出与“SCOTT”从事相同工作的所有雇员
select ename from emp where job=(select job from emp  where ename='SCOTT');

  

 
11、列出某些雇员的姓名和薪金,条件是他们的薪金等于部门30中任何一个雇员的薪金
select ename,sal from emp where sal in (select sal from  emp where deptno=30);

  

 
12、列出某些雇员的姓名和薪金,条件是他们的薪金高于部门30中所有雇员的薪金
select ename,sal from emp where sal > (select max(sal)  from emp where deptno=30);

  

 
13、列出每个部门的信息以及该部门中雇员的数量
select a.deptno,a.dname,a.loc,b.ss from dept a,(select  deptno,count(ename) ss from emp group by deptno) b
where a.deptno=b.deptno;

  

 
14、列出所有雇员的雇员名称、部门名称和薪金
select a.ename,b.dname,a.sal from emp a,dept b where  a.deptno=b.deptno(+);

  

 
15、列出从事同一种工作但属于不同部门的雇员的不同组合
select a.ename,b.ename,a.job,b.job,a.deptno,b.deptno from  emp a,emp b
where a.job=b.job and a.deptno<>b.deptno;

  

16、列出分配有雇员数量的所有部门的详细信息,即使是分配有0个雇员
a.
select a.deptno,a.dname,a.loc,nvl(b.ss,0) from dept a,
(select deptno,count(ename) ss from emp group by deptno) b where a.deptno=b.deptno(+);

  

b.
select a.deptno,dname,loc,count(empno) ss from dept a,emp  b where a.deptno=b.deptno(+)
group by a.deptno,a.dname,a.loc;

  

 
17、列出各种类别工作的最低工资
select job,min(sal) as minjobsal from emp group by job;

  

 
18、列出各个部门的MANAGER(经理)的最低薪金
select deptno,min(sal) ss from emp where job='MANAGER'  group by deptno;

  

 
19、列出按年薪排序的所有雇员的年薪
select ename,(sal*12+nvl(comm,0)) as nianxin from emp  order by nianxin;

  

20、列出薪金水平处于第四位的雇员
a.
select ename,a.tt from (select rownum  tt,ename,(sal*12+nvl(comm,0)) as nianxin from emp)a where  a.tt=4; 
b.
select * from (select ename,sal,rank()over(order by sal  desc)as nn from emp) where nn=4;

Oracle SQL部分练习题的更多相关文章

  1. ORACLE SQL语句练习题

    --1:选择部门30中的所有员工select * from emp where deptno=30--2:列出所有办事员(clerk) 的姓名.编号和部门编号select empno,ename,de ...

  2. Oracle SQL Developer 连接 MySQL

    1. 在ORACLE官网下载Oracle SQL Developer第三方数据库驱动 下载页面:http://www.oracle.com/technetwork/developer-tools/sq ...

  3. Oracle sql连接

    inner-join                    left-outer-join                 right-outer-join                 full- ...

  4. 解决Oracle SQL Developer无法连接远程服务器的问题

    在使用Oracle SQL Developer连接远程服务器的时候,出现如下的错误 在服务器本地是可以正常连接的.这个让人想起来,跟SQL Server的一些设计有些类似,服务器估计默认只在本地监听, ...

  5. Oracle sql语句执行顺序

    sql语法的分析是从右到左 一.sql语句的执行步骤: 1)词法分析,词法分析阶段是编译过程的第一个阶段.这个阶段的任务是从左到右一个字符一个字符地读入源程序,即对构成源程序的字符流进行扫描然后根据构 ...

  6. Oracle SQL explain/execution Plan

    From http://blog.csdn.net/wujiandao/article/details/6621073 1. Four ways to get execution plan(anyti ...

  7. 处理 Oracle SQL in 超过1000 的解决方案

    处理oracle sql 语句in子句中(where id in (1, 2, ..., 1000, 1001)),如果子句中超过1000项就会报错.这主要是oracle考虑性能问题做的限制.如果要解 ...

  8. Oracle sql develpoer

    Oracle SQL Developer是针对Oracle数据库的交互式开发环境(IDE)     Oracle SQL Developer简化了Oracle数据库的开发和管理. SQL Develo ...

  9. Oracle SQL Developer 添加SQLServer 和Sybase 连接

    来源于: http://blog.csdn.net/kk185800961/article/details/8602306 1. 开始只有Oracle 和access 连接 2. 打开Oracle S ...

随机推荐

  1. "Gun N' Rose" Team学习心得

    如果我比别人看得更远,只因为我站在巨人的肩膀上.                                                        ——牛顿 高级软件工程课程终于开课了!第 ...

  2. Windows 2019 激活教程

    Study From https://blog.csdn.net/cssxn/article/details/83743662 1. 原文里面提到了 几个序列号 Windows Server Data ...

  3. jira 插件介绍地址

    1. 官方的 介绍地址 http://confluence.gjingao.com/pages/viewpage.action?pageId=328170 序号 插件名称 功能概要 供应商 资源 10 ...

  4. Python模块-pymssql

    目录 工作原理 常用封装 Python默认的数据库是 SQLlite,不过它对MySql以及SQL server的支持也可以.如果想链接操作SQL server,需使用第三方包pymssql pyms ...

  5. 浅谈Java中的深克隆和浅克隆(阿里面试)

    在最近的秋招中,阿里和多益网络都问到了这个问题,虽然很简单,但是我还是想总结一下,感兴趣的可以看一下我的个人博客网站(Spring+MyBatis+redis+nginx+mysql)(适合菜鸟),最 ...

  6. 用call/cc合成所有的控制流结构

    用call/cc合成所有的控制流结构 来源 https://www.jianshu.com/p/e860f95cad51 call/cc 是非常.非常特殊的,因为它根本无法用 Lambda 演算定义. ...

  7. 【Treap 例题】神秘岛(island)

    神秘岛(island) 题目描述: 除了敲代码和撩妹,旅行是cxw123 的第三爱好.他来到了澳大利亚东北部的大宝礁,在这里,有一个隔绝人世的神秘岛,这个岛不同于附近其他的珊瑚岛,它的生长速度极快,甚 ...

  8. 【uoj35】 后缀排序

    http://uoj.ac/problem/35 (题目链接) 题意 如题,并且求height数组. Solution 挂一发后缀自动机构后缀数组及height数组 细节 注意基数排序和连边的时候不要 ...

  9. 四、spring boot 1.5.4 日志管理

    spring boot日志默认采用logback进行输出,你可以对logback进行定制化,方法如下: 在resources文件夹下建立logback.xml配置文件 <?xml version ...

  10. UML类图学习笔记

    http://note.youdao.com/noteshare?id=d5af220db7081dda73511fcb7b4da390