子查询练习

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 e
left join department d on e.deptno = d.deptno
where e.deptno = ( select deptno from employee
where hiredate = (select min(hiredate) from employee)) --45. 列出最低薪金大于1500的各种工作及此从事此工作的全部雇员人数
select count(empno),job
from employee
where job in (select job from employee
group by job
having 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;

oracle学习笔记(十三) 查询练习(三) 子查询查询的更多相关文章

  1. 【Oracle】曾经的Oracle学习笔记(4-7)多表联合查询,子查询,动态条件查询

    一.多表联合查询 二.子查询 三.动态条件查询 LESSON 4 Displaying Data from Multiple Tables------------------------------- ...

  2. Oracle学习笔记(十三)

    十四.触发器(监听数据操作的工具) 1.什么是触发器? 数据库触发器是一个与表相关联的.存储的PL/SQL程序 作用: 每当一个特定的数据操作语句(insert.update.delete)在指定的表 ...

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

    简介 触发器是当特定事件出现时自动执行的存储过程,特定事件可以是执行更新的DML语句和DDL语句,触发器不能被显式调用.   触发器的功能: 1.自动生成数据 2.自定义复杂的安全权限 3.提供审计和 ...

  4. Oracle学习笔记三 SQL命令

    SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)  

  5. oracle学习笔记第一天

    oracle学习笔记第一天 --oracle学习的第一天 --一.几个基础的关键字   1.select select (挑选) 挑选出显示的--列--(可以多列,用“,”隔开,*表示所有列),为一条 ...

  6. Oracle学习笔记之四sp1,Oracle 11g的常用函数

    从Oracle学习笔记之四,SQL语言入门中摘出来的,独立成一章节 3.1 字符类函数 ASCII(c)和CHR(i)    分别用于返回一个字符的ASCII码和返回给定ASCII值所对应的字符. C ...

  7. Oracle学习笔记之四,SQL语言入门

    1. SQL语言概述 1.1 SQL语言特点 集合性,SQL可以的高层的数据结构上进行工作,工作时不是单条地处理记录,而对数据进行成组的处理. 统一性,操作任务主要包括:查询数据:插入.修改和删除数据 ...

  8. java之jvm学习笔记十三(jvm基本结构)

    java之jvm学习笔记十三(jvm基本结构) 这一节,主要来学习jvm的基本结构,也就是概述.说是概述,内容很多,而且概念量也很大,不过关于概念方面,你不用担心,我完全有信心,让概念在你的脑子里变成 ...

  9. Oracle学习笔记—数据字典和常用命令(转载)

    转载自: oracle常用数据字典和SQL语句总结 Oracle常用命令大全(很有用,做笔记) 一.Oracle数据字典 数据字典是Oracle存放有关数据库信息的地方,其用途是用来描述数据的.比如一 ...

  10. MySql学习笔记(一)之DQL常用查询

    MySql学习笔记(一)之DQL常用查询 前言:mysql是中小型的数据库软件,SQL语言分为DDL,DCL,DML,DQL四种,在这里重点讲解DQL的单表查询. 正文:在学习mysql单表查询之前, ...

随机推荐

  1. (day65、66)Vue基础、指令、实例成员、JS函数this补充、冒泡排序

    目录 一.Vue基础 (一)什么是Vue (二)为什么学习Vue (三)如何使用Vue 二.Vue指令 (一)文本指令 (二)事件指令v-on (三)属性指令v-bind (四)表单指令v-model ...

  2. celery beat - 心跳包

    celery -A 项目名 beat -l info -S django# 启动心跳任务 celery -A 项目名  worker -l info # 启动1个worker去执行

  3. 安装PS

    1:下载溜云库 2:查找PS软件,下载 3:按照教程安装

  4. Bit Manipulation-leetcode

    Bit Manipulation Find the Difference /* * Given two strings s and t which consist of only lowercase  ...

  5. 如何使用1行代码让你的C++程序控制台输出彩色log信息

    本文首发于个人博客https://kezunlin.me/post/a201e11b/,欢迎阅读最新内容! colorwheel for colored print and trace for cpp ...

  6. 隐藏Nginx软件版本号信息

    为了提高我们web服务器的安全性,我们应当尽可能的隐藏服务器的信息以防止他人通过这些信息找到漏洞侵入我们的服务器,对于Nginx而言,我们安装好Nginx后最好隐藏Nginx的版本号,以防止通过该版本 ...

  7. .net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓包

    前言:  通过Fiddler抓取浏览器请求数据,相信大家已经都会用了,我们知道Fiddler是通过在本机计算器添加一个默认的代理服务器来实现的抓包数据的,端口号为:8888. 其实当我们打开Fiddl ...

  8. C# IE环境 - 重置IE( 注册表)

    IE设置,都可以通过注册表,修改.以下是一些常用的IE设置注册表修改~ 检查证书吊销 /// <summary> /// 检查证书是否吊销 /// </summary> /// ...

  9. Masonry纯码实现UIScrollView 之上下滚动,设置UIScrollView背景图片

    参考链接:https://www.jianshu.com/p/9a158308c50b 亲测有效,很赞! 你们最想要的Demo下载地址:https://github.com/objcxiaobai/C ...

  10. PC/SC双界面读写器开发指南

    友我科技PCSC双界面读写器YW-606开发指南 1.建立资源管理器的上下文 函数ScardEstablishContext()用于建立将在其中进行设备数据库操作的资源管理器上下文(范围). 函数原型 ...