记得自己要敲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数据库多表查询,子查询,集合运算的更多相关文章

  1. Oracle 数据库基本操作——表操作:查询

    目录: 1.基本查询 2.多表查询 3.多行查询 4.集合查询 2.连接 3.嵌套查询 1.基本查询 语法: select column|others{,columnName|others} from ...

  2. Oracle 数据库基础学习 (六) 子查询

    子查询在一个select中出现多个嵌套查询语句 1.在where子句中使用子查询(一般返回"单行单列" "单行多列" "多行单列"(可以提供 ...

  3. Database学习 - mysql 数据库 多表/复合/子 查询

    多表查询 多表查询,基本规则,通过两表有关联字段的进行条件匹配查询 内连接查询 方式一: SELECT 查看字段名[,查看字段名] FROM 一表名,二表名 WHERE 一/二表.字段 = 一/二表. ...

  4. Oracle(2)之多表查询&子查询&集合运算

    多表查询 笛卡尔积 同时查询多张表时,每张表的每条数据都要和其它表的每条数据做组合.如下栗子,我们发现产生的总记录数是 56 条,还发现 emp 表是 14 条,dept 表是 4 条,56 条正是 ...

  5. oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by

    select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...

  6. 关于oracle数据库 跨表查询建立 视图的方法

    工作中很多时候都会遇到需要将两个不同的表空间甚至数据库的表进行联合查询或者建立视图的情况. 不同的表空间查询我们可以通过在将要查询的表前面加上 表空间的对应的用户名来实现,如有两个表空间分别对应两个用 ...

  7. oracle数据库单表查询

    今天给大家分享的是关于数据库的单表查询,像单表查询/多表查询/分组查询/子查询,这些方法的使用在实际项目过程中会经常用到,作为一名合格的测试人员如果不会数据库那肯定是不行的,行走江湖可能随时会面临被侮 ...

  8. [sql Server]除非另外还指定了TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效

    今天遇到一个奇怪的问题,项目突然要从mysql切换到sql server数据库,包含order by 子句的嵌套子查询报错. 示例:select top 10 name,age,sex from ( ...

  9. 【知识库】-数据库_MySQL之基本数据查询:子查询、分组查询、模糊查询

    简书作者:seay 文章出处: 关系数据库SQL之基本数据查询:子查询.分组查询.模糊查询 回顾:[知识库]-数据库_MySQL常用SQL语句语法大全示例 Learn [已经过测试校验] 一.简单查询 ...

  10. Python-select 关键字 多表查询 子查询

    sql 最核心的查询语句!!!! 增删改 单表查询 select语句的完整写法 关键字的书写顺序 执行顺序 多表查询 笛卡尔积 内连接 左外连接 右外连接 全外连接 通过合并左外连接和右外连接 子查询 ...

随机推荐

  1. 提高PowerShell脚本效率的五个常用方法

    PowerShell脚本运行慢怎么办?影响到正常企业流程正常运转怎么办?本文利用例子和数据给大家带来让PowerShell运行更快的五个常用方法.本人拙见,希望能够给大家带来一点启发~ 1. 善用命令 ...

  2. Android 实现两个list分别出现(在某一时刻只出现一个控件)

    第一种方法: 在.xml文件中将这两个List分别放入不同的布局管理器中,比如说 <RelativeLayout android:layout_width="match_parent& ...

  3. <eq>标签

    链接:http://document.thinkphp.cn/manual_3_2.html#taglib <eq name="menu.id" value="1& ...

  4. Oracle新建数据库,并导入dmp文件

    1:安装Oracle及新建数据库 Oracle 11g安装图解 http://www.cnblogs.com/qianyaoyuan/archive/2013/05/05/3060471.html h ...

  5. APScheduler API -- apscheduler.triggers.interval

    apscheduler.triggers.interval API Trigger alias for add_job(): interval class apscheduler.triggers.i ...

  6. flask基础之请求处理核心机制(五)

    前言 总结一下flask框架的请求处理流程. 系列文章 flask基础之安装和使用入门(一) flask基础之jijia2模板使用基础(二) flask基础之jijia2模板语言进阶(三) flask ...

  7. select()函数用法三之poll函数

    poll是Linux中的字符设备驱动中有一个函数,Linux 2.5.44版本后被epoll取代,作用是把当前的文件指针挂到等待队列,和select实现功能差不多. poll()函数:这个函数是某些U ...

  8. Python Challenge 第 5 关攻略:peak

    # -*- coding: utf-8 -*- # @Time : 2018/9/26 14:03 # @Author : cxa # @File : pickledemo.py # @Softwar ...

  9. 关于Java中final关键字的详细介绍

    Java中的final关键字非常重要,它可以应用于类.方法以及变量.这篇文章中我将带你看看什么是final关键字?将变量,方法和类声明为final代表了什么?使用final的好处是什么?最后也有一些使 ...

  10. 使用qshell备份七牛云存储文件

    qshell是利用七牛文档上公开的API实现的一个方便开发者测试和使用七牛API服务的命令行工具.我们可以利用它来将七牛云上存储的文件备份到本地. 它提供Mac OSX, Linux, Windows ...