子查询练习

create table empployee_demo(empno     number(4) not null primary key,  --员工编号,主键ename     varchar2(10) not null unique,    --员工名,唯一键job       varchar2(9),                     --职位、工作mgr       number(4),                       --经理编号hiredate  date default sysdate,            --入职日期,默认约束sal       number(7,2) check(sal>=500 and sal<=10000),   --工资comm      number(7,2),                     --资金deptno    number(2)                        --部门编号
)--department
DEPTNO NUMBER(2)    --部门编号
DNAME  VARCHAR2(14) Y             --部门名字
LOC    VARCHAR2(13) Y          --部门位置--salgrade
grade
losal
hisal --39. 查询部门名称为SALES和ACCOUNTING的员工信息
select * from employee
where deptno in
(select deptno from department
where dname ='SALES' or dname ='ACCOUNTING')--40. 查询不是经理的员工的信息(使用in 或 not in来做)
select * from employee
where job not in ('MANAGER');--41. 查询工资比10号部门员工中任意一个低的员工信息
select * from employee
where sal < any(select sal from employee where deptno=10);--42. 查询工资比10号部门都要低的员工信息
select * from employee
where sal < all(select sal from employee where deptno=10);select * from employee
where sal < (select min(sal) from employee where deptno=10);
--43. 查询出部门名称,部门员工数,部门平均工资,部门最低工资雇员的姓名,及工资等级
select d.dname 部门名,emp.cou 部门员工数,emp.avgsal 平均工资,e.ename 最低工资员工名,s.grade 工资等级
from department d,employee e,salgrade s,(select deptno,avg(sal) avgsal,count(empno) cou,min(sal) minsal from employee
group by deptno) emp
where d.deptno = emp.deptno and e.sal = emp.minsal and e.sal between s.losal and s.hisal--44.找出与入职时间最早的员工在同一个部门的员工信息以及所在部门名称
select * from employee eleft join department d on e.deptno = d.deptno
where e.deptno = ( select deptno from employeewhere hiredate = (select min(hiredate) from employee))--45. 列出最低薪金大于1500的各种工作及此从事此工作的全部雇员人数
select count(empno),job
from employee
where job in (select job from employeegroup by jobhaving min(sal)>1500)
group by job  --46. 求出在'SALES'部门工作的员工姓名,假设不知道销售部的部门编号
select ename from employee
where deptno =(select deptno from department
where dname='SALES')--47. 列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,和工资等级
select e.*,s.grade,boss.ename 上级领导
from employee e,department d,salgrade s,employee boss
where e.deptno = d.deptno and e.sal between losal and hisal and e.mgr = boss.empno
and e.sal > (select avg(sal) from employee)--48. 列出与员工SCOTT从事相同工作的所有员工及部门名称
select ename,e.job,dname from employee e,department d,(select deptno,job from employee where ename ='SCOTT') e1
where e.deptno = d.deptno and e.job = e1.job and e.deptno=e1.deptno and ename <>'SCOTT'--49. 查询和SMITH部门相同, 岗位相同的人
select * from employee
where deptno = (select deptno from employee where ename='SMITH')
and job = (select job from employee where ename = 'SMITH')--50. 和ALLEN同部门,工资高于MARTIN的雇员有哪些
select * from employee
where deptno = (select deptno from employee where ename='ALLEN') and
sal> (select sal from employee where ename = 'MARTIN')--51. 比blake工资高的雇员有哪些?
select * from employee
where sal> (select sal from employee where ename = 'BLAKE')--52. 高于30部门最高工资的雇员有哪些?
select * from employee
where sal > (select max(sal) from employee where deptno=30)--53. 查询scott.emp表中所有的经理的信息(此操作子查询会返回多行记录)
select * from employee
where empno in (select distinct mgr from employee where mgr is not null);--54. 工资高于本部门平均工资的人(拿上游工资的人)有哪些?
select deptno,ename,sal
from employee e
where sal>(select avg(sal) from employee where deptno=e.deptno);--55. 工作和部门与SMITH相同,工资高于JAMES的雇员有哪些?
select * from employee
where deptno = (select deptno from employee where ename='SMITH')
and job = (select job from employee where ename = 'SMITH') and sal > (select sal from employee where ename='JAMES')  select job,deptno,sal
from employee
where (job,deptno)=(select job,deptno from employee where ename='SMITH') and sal>(select sal from employee where ename='JAMES');--56.列出每个部门工作的员工数量,平均工资和平均服务年限
select deptno 部门,count(empno) 员工数量,avg(sal) 平均工资,avg(extract(year from sysdate)-extract(year from hiredate)) 平均服务年限 from employee
group by deptno
order by deptno--57. 列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金
select ename,sal from employee
where sal in (select sal from employee where deptno = 30) and deptno!=30;--58.列出薪金大于部门30中员工的薪金的所有员工的姓名和薪金
select ename,sal from employee
where sal > all(select sal from employee where deptno = 30) and deptno!=30;/*
59. 分页查询,每页显示5条记录,按工资升序排序使用employee表,利用子查询和 rownum伪列编写一个分页查询语句,每页显示5条记录,
如第1页:第1~5条记录,第2页:第6~10条记录,.......<a href="....?pageNo=1">第一页</a>
<a href="....?pageNo=?">上一页</a>
<a href="....?pageNo=?">下一页</a>
<a href="....?pageNo=max_value">最后一页</a>rownum伪列是为查询结果集的行分配行号,是先有了结果后才分配的行号
如果通过主键排序rownum的行号是有序的,但如果使其字段排序则行号无序
*/
select * from(select t.*,rownum rn from (select * from employee order by sal ) t --每条数据都有了个伪列where rownum<=10 --到10行结束
) where rn>=5 --5行开始--60. 有如下表结构,删除表中重复的数据,重复数据只保留一行:表:emp_dup(id number, name varchar2(10));
--    自行插入一些重复数据
delete from  emp_dup a
where  a.rowid>(select min(b.rowid) from  emp_dup b where  a.id = b.id and a.name = b.name
);
commit;

转载于:https://www.cnblogs.com/kexing/p/10913329.html

oracle学习笔记(十三) 查询练习(三) 子查询查询相关推荐

  1. Oracle学习笔记十三 触发器

    2019独角兽企业重金招聘Python工程师标准>>> 简介 触发器是当特定事件出现时自动执行的存储过程,特定事件可以是执行更新的DML语句和DDL语句,触发器不能被显式调用. 触发 ...

  2. Oracle学习笔记(七)——分组统计查询

    Oracle学习笔记(七)--分组统计查询 基础统计函数的使用 分组统计操作的实现,结合多表查询使用分组统计 常用统计函数 COUNT(*|[DISTINCT]字段) MAX(字段,日期或数字) MI ...

  3. Oracle学习笔记---(一)

    Oracle学习笔记---(一) 一 1.Oracle简介     Oracle是以高级结构化查询语言(SQL)为基础的大型关系数据库:是一个对象关系数据库管理系统(ORDBMS).它提供了关系数据库 ...

  4. oracle学习笔记 Oracle体系结构概述

    oracle学习笔记 Oracle体系结构概述 从这节开始,开始讲oracle数据库体系结构. 首先从总体上,从概述上把oracle整体的体系结构讲一下, 然后接下来的时间我们会一块一块的将oracl ...

  5. Oracle学习笔记 字符集概述

    Oracle 学习笔记 字符集概述 这节课开始讲oracle里面的字符集 偏重于原理和简单的一些判断以及实现 字符集它涉及到很多的东西 比如建库和操作系统环境 这节课把字符集的原理性的东西以及常见的操 ...

  6. oracle update单引号,Oracle学习笔记:update的字段中包括单引号

    平时update的时候直接更改字段内的值,例如: update table_temp set name = 'Hider' where id = 100; 但更新后的值中包括单引号,则不能按以上方式进 ...

  7. oracle学习笔记 参数文件及数据库的启动和关闭

    oracle学习笔记 参数文件及数据库的启动和关闭 我们这节课把oracle的参数文件以及oracle的启动关闭讲一下 一)参数文件作用 先看oracle的参数文件 它由来已久了 我们知道oracle ...

  8. oracle:oracle学习笔记(四)循环、光标、异常、瀑布模型

    oracle学习笔记:循环.光标.异常 文章目录 打印Hello World 定义基本变量 引用型变量(单行)` my_name emp.ename%type ` 记录型变量(多行) `emp_rec ...

  9. 在大量数据迁移期间oracle学习笔记

    在数据迁移期间oracle学习笔记 0主键(自增) 1用户代码 2区域代码 3承保公司代码 4理赔编码 5投保确认码 6案件状态 7案件进展 8重开案件进展 9转换后案件状态 需求分析: 1.根据上述 ...

  10. oracle 权限问题9017,[数据库]oracle学习笔记(一)用户管理_星空网

    oracle学习笔记(一)用户管理 2014-04-13 0 1 --oracle学习第一天 2 --连接 @后面连接数据库实例,具体连接到那个数据库 3 conn scott/tiger@MYORA ...

最新文章

  1. DDD助力平台能力复用
  2. IDEA 运行run 为灰色解决办法
  3. 转载:Django之Form组件
  4. 1079. 延迟的回文数 (20)
  5. html css 基础(标签选择,分页,行和块元素)
  6. AI读懂两千年前文字,登上Nature封面,惊艳历史学家
  7. linux内核五大部分,Linux内核的五大模块
  8. [转载]Linux shell中的竖线(|)——管道符号
  9. 冒泡排序详解(C++)
  10. 根据ip地址制作html,根据ip掩码计算可用ip
  11. 【面试】网易游戏社招一面总结
  12. 表格中计算机设置,如何在excel表格中设置下拉菜单?一招教你搞定!
  13. 国际学术期刊排名按照姓氏字母排吗?
  14. 小黄鸡 php,Simsimi (小黄鸡) API接口(PHP)公布,小黄鸡API接口非官方PHP版本来啦...
  15. NodeJs ES6 写简单爬虫 爬小说网站《我当方士那些年》
  16. ERP系统的备份考虑
  17. 【软件工程】软件工程系统开发——系统设计概述
  18. XMap 简易的Java-xml映射工具类库
  19. matlab 实现低通巴特沃斯滤波器、切比雪夫1型/2型滤波器 和 椭圆滤波器
  20. 教程:如何下载坦克世界

热门文章

  1. Java 如何设置时间_如何在Java中设置尊重用户操作系统设置的日期和时间格式
  2. 一纬度横直线等于多公里_必备 | 高中物理必修一、必修二知识点提纲,预习复习一次搞定!...
  3. java文件比较_Java 比较两个任意文件是否相同
  4. linux samba 多个目录,linux7 Samba服务配置,多个部门相应管理自己的项目目录,其他有访问权限...
  5. ctfshow-WEB-web14( 利用数据库读写功能读取网站敏感文件)
  6. Oracle循环语句
  7. mysql性能优化学习笔记
  8. Uva1343-The Rotation Game-IDA*算法
  9. Android 5.0 Screen pinning 屏幕固定功能
  10. Linux系统NFS故障现象