Oracle数据库多表查询,子查询,集合运算
记得自己要敲o~~~
select * from bonus;
select * from salgrade;
select 1+1 from dual;
--笛卡尔积:两张表的乘积
select * from emp,dept; select * from emp e1,dept d1 where e1.deptno =d1.deptno; /*
内联接:
隐式内联接:
不等值内联接:where e1.deptno <> d1.deptno
自联接:自己连接自己
等值内联接: where e1.deptno = d1.deptno;
显示内联接:
select * from 表1 inner join 表2 链接条件 inner关键字可以省略
*/
select * from emp e1,dept d1 where e1.deptno <> d1.deptno; --查询员工编号,员工姓名,经理编号,经理的姓名 select e1.empno,e1.ename,e1.mgr,m1.ename
from emp e1,emp m1 where e1.mgr = m1.empno; --查询员工编号,员工姓名,员工的部门名称,经理的编号,经理的姓名
select e1.empno,e1.ename,e1.mgr,m1.ename,d1.dname
from emp e1,emp m1,dept d1 where e1.mgr = m1.empno and e1.deptno = d1.deptno; --查询员工编号,员工姓名,员工的部门名称,经理的编号,经理的姓名,经理的部门名称
select e1.empno,e1.ename,d1.dname,e1.mgr,m1.ename,d2.dname
from emp e1,emp m1,dept d1,dept d2
where e1.mgr = m1.empno
and e1.deptno = m1.deptno and d2.deptno = m1.deptno;
select * from dept;
select * from emp;
select * from salgrade; --查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的部门名称
select e1.empno,e1.ename,d1.dname,s1.grade,e1.mgr,m1.ename,d2.dname
from emp e1, emp m1,dept d1,dept d2,salgrade s1
where
e1.mgr= m1.empno
and e1.deptno = d1.deptno
and m1.deptno = d2.deptno
and e1.sal between s1.losal and s1.hisal ;
--查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的部门名称,经理的工资等级
select e1.empno,e1.ename,d1.dname,s1.grade,e1.mgr,m1.ename,d2.dname,s2.grade
from emp e1, emp m1,dept d1,dept d2,salgrade s1,salgrade s2
where
e1.mgr= m1.empno
and e1.deptno = d1.deptno
and m1.deptno = d2.deptno
and e1.sal between s1.losal and s1.hisal
and m1.sal between s2.losal and s2.hisal ;
--查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的部门名称,经理的工资等级
--将工资登记1,2,3,4显示成中文的一级二级,三级四级
select e1.empno,
e1.ename,
d1.dname,
case s1.grade
when 1 then '一级'
when 2 then '二级'
when 3 then '三级'
when 4 then '四级'
else
'五级'
end "等级",
e1.mgr,
m1.ename,
d2.dname,
decode(s2.grade,1,'一级',2,'二级',3,'三级',4,'四级','五级') "等级"
from emp e1, emp m1,dept d1,dept d2,salgrade s1,salgrade s2
where
e1.mgr= m1.empno
and e1.deptno = d1.deptno
and m1.deptno = d2.deptno
and e1.sal between s1.losal and s1.hisal
and m1.sal between s2.losal and s2.hisal ; --查询员工姓名和员工部门所处的位置
select e1.ename,e1.deptno from emp e1 ,dept d1 where e1.deptno = d1.deptno; select * from emp e1 inner join dept d1 on e1.deptno = d1.deptno; /*
外联接:(标准,通用写法)
左外联接:left outer join 左表中所有的记录,如果右表没有对应记录,就显示空
右外连接:right outer join 右表中所有的记录,如果左表没有对应记录,就显示空
outer 关键字可以省略
Oracle中的外连接:(+)实际上是没有对应的记录就加上空值
select * from emp e1,dept d1 where e1.deptno = d1.deptno(+); */
select * from emp e1 left outer join dept d1 on e1.deptno = d1.deptno;
insert into emp(empno,ename) values(9527,'HUAAN');
select * from emp e1,dept d1 where e1.deptno = d1.deptno(+);
select * from emp e1 right outer join dept d1 on e1.deptno = d1.deptno; select * from emp e1,dept d1 where e1.deptno(+) = d1.deptno; /*
子查询:查询语句中嵌套查询语句;用来解决复杂的查询语句
查询最高工资的员工的信息
单行子查询:> >= = < <= <> != 多行子查询: in not in >any >all exists not exists
查询领导信息
*/
--查询最高工资的员工信息
--1.查询最高工资
select max(sal) from emp ;
--2.工资等于最高工资
select * from emp where sal=(select max(sal) from emp); --查询出比雇员7654的工资高,同时和7788从事相同工作的员工信息
--1.雇员7654的工资 1250
select sal from emp where empno=7654; --2.7788从事的工作 ANALYST
select job from emp where empno= 7788;
select* from emp where
(select sal from emp where empno=7654)<sal and job=(select job from emp where empno= 7788); select * from emp where sal > (select sal from emp where empno = 7654) and job = (select job from emp where empno = 7788); --查询每个部门最低工资的员工信息和他所在的部门信息
--1.查询每个部门的最低工资,分组统计
select deptno, min(sal) minsal from emp group by deptno;
--2.员工工资等于他所处部门的最低工资
select * from emp e1,
(select deptno, min(sal) minsal from emp group by deptno)
t1 where e1.deptno = t1.deptno and e1.sal = t1.minsal; --3.查询部门相关信息
select *
from emp e1,
(select deptno,min(sal) minsal from emp group by deptno) t1,
dept d1
where e1.deptno = t1.deptno and e1.sal = t1.minsal and e1.deptno = d1.deptno;
/*
内联接,单行自查询,多行子查询 in
not in
any
all
exists 通常情况下,数据库中不要出现null,最好的做法是加上not null
null值并不代表不占用空间,char(100)null 100个字符
*/
--查询领导信息
--1.查询所有经理的编号
select mgr from emp;
select distinct mgr from emp;
--2.结果
select * from emp where empno in (select mgr from emp);
--查询不是领导的信息
select * from emp where empno not in (select mgr from emp);
select * from emp where empno <>all(select mgr from emp);
--正确写法
select * from emp where empno not in(select mgr from emp where mgr is not null);
--查询出比10号部门任意一个员工薪资高的员工信息 10 20 30
select * from emp where sal > any(select sal from emp where deptno=10);
--查询出比20号部门所有员工薪资高的员工信息 10 20 30
--1.20号最高工资 3000
select max(sal) from emp where deptno =20;
--2.员工信息
select * from emp;
--合并
select * from emp where sal > (select max(sal) from emp where deptno =20); -----使用多行子查询完成上面这题
--20号部门所有员工的薪资
select sal from emp where deptno = 20;
--大于集合所有的
select * from emp where sal > all(select sal from emp where deptno=20); /*
exists(查询语句):存在的意思,判断一张表里的记录是否存在于另外一张表中
当作布尔值来处理
当查询语句有结果的时候,就返回true;否则false;
数据量比较大的时候是非常高效的
*/
select * from emp where exists(select * from emp where deptno = 1234567);
select * from emp where 3=4; select * from emp where exists(select * from emp where deptno = 20); --查询有员工的部门的信息
select * from dept d1 where exists(select * from emp e1 where e1.deptno = d1.deptno ); --找到员工表中工资最高的前三名(降序排序)
select * from emp order by sal desc;
/*
rownum : 伪列 ,系统自动生成一列,用来表示行号
rownum是Oracle特有的用来表示行号,默认值/起始值是1,在每查询出结果之后,在添加1
rownum最好不能做大于号判断,可以做小于号判断 sql执行顺序
from .. where..group by..having..select ..rownum..order by
*/
select rownum ,e1.* from emp e1; --查询rownum大于2的所有记录
select rownum , e1.* from emp e1 where rownum>2;--没有任何记录
--查询rownum大于等于1的所有记录
select rownum , e1.* from emp e1 where rownum>=1;
--查询rownum < 6 的所有记录
select rownum,e1.* from emp e1 where rownum < 6;
--rownum 排序
select rownum ,e1.* from emp e1 order by sal; --找到员工表中工资最高的前三名
select rownum ,e1.* from emp e1 order by sal desc ;
--将上面的结果当作一张表处理,再查询
select rownum ,t1.* from (select rownum ,e1.* from emp e1 order by sal desc) t1; --只显示前三条记录
select rownum ,
t1.* from(select rownum ,e1.* from emp e1 order by sal desc) t1 where rownum<4; --找到员工表中薪水大于本部门平均薪水的员工
--1.分组统计部门平均薪水
select deptno, avg(sal) avgsal from emp group by deptno;
--2.员工工资 > 本部门平均工资
select * from emp e1,
(select deptno,avg(sal) avgsal from emp group by deptno) t1
where e1.sal>t1.avgsal and e1.deptno = t1.deptno; /*
关联子查询 , 非关联子查询
*/
select * from emp e where
sal > (select avg(sal) from emp e2 group by deptno having e.deptno=e2.deptno) /*
统计每年入职的员工个数
*/
select hiredate from emp;
--只显示年
select to_char(hiredate,'yyyy') from emp; --分组统计
select to_char(hiredate,'yyyy')yy,count(1) cc from emp group by to_char(hiredate,'yyyy'); select case yy when '' then cc end
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; select yy
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; select case yy when '' then cc end "1987"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; select case yy when '' then cc end "1987"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; --去除行记录中的空值
select sum(case yy when '' then cc end) "1987"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; --统计员工的总数
select sum(cc) "TOTAL"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) --将1987 和TOTAL 合并在一起
select sum(cc) "TOTAL",
sum(case yy when '' then cc end)"1987"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; --显示所有年份的结果
select
sum(cc) "TOTAL",
sum(case yy when '' then cc end) "1980",
sum(case yy when '' then cc end) "1981",
sum(case yy when '' then cc end) "1982",
sum(case yy when '' then cc end) "1987"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; /*
rowid :伪列 每行记录所存放的真实物理地址
rownum :行号 每查询出记录之后,就会添加一个行号
*/
select rowid ,e.* from emp e; /*
rownum:分页查询
在Oracle中只能使用子查询来做分页查询
*/
select rownum, emp.* from emp;
select * from (select rownum hanghao ,emp.* from emp)tt
where tt.hanghao between 6 and 10; /*
集合运算:
并集:将两个查询结果进行合并
交集
差集 所有的查询结果可能不是来自同一张表
emp 2000年
2017年 手机 详细信息 emp2017
*/
--工资大于1500,或者20号部门下的员工
select * from emp where sal > 1500 or deptno = 20; --并集运算: union union all
/*
union:去除重复的,并且排序
union all:不会去除重复的
*/
select * from emp where sal>1500
union
select * from emp where deptno = 20; select * from emp where sal > 1500
union all
select * from emp where deptno = 20; /*
交集运算: intersect */
--工资大于1500,并且20号部门下的员工 select * from emp where sal>1500
intersect
select * from emp where deptno = 20; /*
差集运算: 两个结果相减
*/
--1981年入职员工(不包括总裁和经理)
--1981年入职员工
select * from emp where to_char(hiredate,'yyyy')= 1981;
--总裁和经理
select * from emp where job = 'PRESIDENT' or job = 'MANAGER'; select * from emp where to_char(hiredate,'yyyy')= 1981
minus
select * from emp where job = 'PRESIDENT' or job = 'MANAGER'; /*
集合运算中的注意事项
1.列的类型要一致
2.按照顺序写
3.列的数量要一致,如果不足,用空值填充
*/
select ename,sal from emp where sal > 1500
union
select ename,sal from emp where deptno = 20;
Oracle数据库多表查询,子查询,集合运算的更多相关文章
- Oracle 数据库基本操作——表操作:查询
目录: 1.基本查询 2.多表查询 3.多行查询 4.集合查询 2.连接 3.嵌套查询 1.基本查询 语法: select column|others{,columnName|others} from ...
- Oracle 数据库基础学习 (六) 子查询
子查询在一个select中出现多个嵌套查询语句 1.在where子句中使用子查询(一般返回"单行单列" "单行多列" "多行单列"(可以提供 ...
- Database学习 - mysql 数据库 多表/复合/子 查询
多表查询 多表查询,基本规则,通过两表有关联字段的进行条件匹配查询 内连接查询 方式一: SELECT 查看字段名[,查看字段名] FROM 一表名,二表名 WHERE 一/二表.字段 = 一/二表. ...
- Oracle(2)之多表查询&子查询&集合运算
多表查询 笛卡尔积 同时查询多张表时,每张表的每条数据都要和其它表的每条数据做组合.如下栗子,我们发现产生的总记录数是 56 条,还发现 emp 表是 14 条,dept 表是 4 条,56 条正是 ...
- oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by
select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...
- 关于oracle数据库 跨表查询建立 视图的方法
工作中很多时候都会遇到需要将两个不同的表空间甚至数据库的表进行联合查询或者建立视图的情况. 不同的表空间查询我们可以通过在将要查询的表前面加上 表空间的对应的用户名来实现,如有两个表空间分别对应两个用 ...
- oracle数据库单表查询
今天给大家分享的是关于数据库的单表查询,像单表查询/多表查询/分组查询/子查询,这些方法的使用在实际项目过程中会经常用到,作为一名合格的测试人员如果不会数据库那肯定是不行的,行走江湖可能随时会面临被侮 ...
- [sql Server]除非另外还指定了TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效
今天遇到一个奇怪的问题,项目突然要从mysql切换到sql server数据库,包含order by 子句的嵌套子查询报错. 示例:select top 10 name,age,sex from ( ...
- 【知识库】-数据库_MySQL之基本数据查询:子查询、分组查询、模糊查询
简书作者:seay 文章出处: 关系数据库SQL之基本数据查询:子查询.分组查询.模糊查询 回顾:[知识库]-数据库_MySQL常用SQL语句语法大全示例 Learn [已经过测试校验] 一.简单查询 ...
- Python-select 关键字 多表查询 子查询
sql 最核心的查询语句!!!! 增删改 单表查询 select语句的完整写法 关键字的书写顺序 执行顺序 多表查询 笛卡尔积 内连接 左外连接 右外连接 全外连接 通过合并左外连接和右外连接 子查询 ...
随机推荐
- 经典幻灯片插件Swiper
照着写的demo,搞清楚什么叫分页器Pagination,什么叫nav,搞清楚DOM结构,container,wrapper之类的,就能写了.效果掉渣天! <!DOCTYPE html> ...
- [转载]strtok函数和strtok_r函数
1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct person{ char name[25]; char sex[1 ...
- soj1166. Computer Transformat(dp + 大数相加)
1166. Computer Transformat Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description A sequenc ...
- jquery bxslider幻灯片样式改造
找了很多jquery的幻灯片,都觉得不是很好,最后发现bxslider兼容性最好,移动设备支持手动翻动. 但是官方提供的显示效果真的很难看,让人难以接受.最后只能自己DIY了. bxslider官方样 ...
- tensorflow中的卷积和池化层(一)
在官方tutorial的帮助下,我们已经使用了最简单的CNN用于Mnist的问题,而其实在这个过程中,主要的问题在于如何设置CNN网络,这和Caffe等框架的原理是一样的,但是tf的设置似乎更加简洁. ...
- 守卫者的挑战(guard)
problem Pro 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过.“我,Nizem,是黑魔法圣殿的守卫者.如果你能通过 ...
- 爬虫笔记之JS检测浏览器开发者工具是否打开
在某些情况下我们需要检测当前用户是否打开了浏览器开发者工具,比如前端爬虫检测,如果检测到用户打开了控制台就认为是潜在的爬虫用户,再通过其它策略对其进行处理.本篇文章主要讲述几种前端JS检测开发者工具是 ...
- 下拉框 select
1.select 用来做什么? select 用于实现下来下拉列表,其 html 结构是这样的: <select name="city" id="city" ...
- php中global和$GLOBALS最浅显易懂的解释
官方文档: global指对变量的引用或者叫指针,$GLOBALS则是变量本身: $var1 = 1; $var2 = 2; function fun(){ $GLOBALS['var2'] = &a ...
- AopProxyUtils.getSingletonTarget(Ljava/lang/Object;)Ljava/lang/Object;大坑
这个问题太坑了,试了好多个版本,都是依赖冲突导致的, https://blog.csdn.net/qq_15003505/article/details/78430595 最后找到这一篇博客解决了,就 ...