您的位置:首页 > 其它

oralce高级查询

2015-08-14 06:18 417 查看
</pre><pre name="code" class="sql">select avg(sal),sum(sal) from emp
select max(sal),min(sal) from emp
select count(*) from emp
select count(deptno) from emp
select count (distinct deptno) from emp--distinct 去重
--wm_concat 行转列
select deptno 部门号,wm_concat(ename) 部门中员工的姓名 from emp group by deptno
--统计员工的平均工资
--分组函数会自动忽略空值
--nvl函数使分组函数无法忽略空值
select count(comm),count(*),count(nvl(comm,0))from emp
select deptno,avg(sal) from emp group by deptno
select avg(sal) from emp group by deptno
--不在组函数中的列就要放在group by 中,但group by中的列不一定在select中
select deptno,job,sum(sal) from emp group by deptno,job order by deptno
--求平均工资大于2000的部门
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000
--where 字句中不能用组函数  having 可以
select deptno,avg(sal) from emp group by deptno having deptno=10
select deptno,avg(sal) from emp  where deptno=10  group by deptno--先过滤再分组,更优
--order by 可以按着列、别名、表达式、序号经行排序
select avg(sal) 平均工资 from emp group by deptno order by 平均工资
select avg(sal) 平均工资 from emp group by deptno order by 1
select max(avg(sal)) 最大平均工资 from emp group by deptno
--group by 语句增强

select deptno,job ,sum(sal) from emp group by deptno ,job
select deptno,sum(sal) from emp group by deptno
select sum(sal) from emp
select deptno,job,sum(sal) from emp group by rollup(deptno,job)
/*
group by rollup(a,b)
=group by a,b
+group by a
+group by null
*/

--笛卡尔集等于两张表的列数相加行数相乘
--为了避免使用笛卡尔全集,使用where加入有效的连接条件,连接条件个数至少表个数-1
--连接类型 等值连接、不等值连接、外连接、自连接

--等值连接
select e.empno, e.ename, e.sal, d.dname
from emp e,dept d
where e.deptno = d.deptno
--不等值连接
select e.empno,e.ename,e.sal,s.grade
from emp e,salgrade s
where e.sal between s.losal and s.hisal
--外连接
select d.deptno 部门号,d.dname 部门名称,count(e.empno)人数
from emp e,dept d
where e.deptno=d.deptno
group by d.deptno,d.dname
--外连接核心:通过外连接,把对于连接条件不成立的记录,仍然包含在最后的结果中
--左连:当连接条件不成立的时候,等号左边的表仍被包含
--右连:当连接条件不成立的时候,等号右边的表仍被包含
--右连,在等号左面加等号
select d.deptno 部门号,d.dname 部门名称,count(e.empno)人数
from emp e,dept d
where e.deptno(+)=d.deptno
group by d.deptno,d.dname

--自连接
select e.ename 员工姓名, b.ename 老板姓名
from emp e, emp b
where e.mgr = b.empno
--自连接不适合操作打表,平方关系,解决办法层次查询,只有一张表,树的深度:level,伪列
select level, empno, ename, sal
from emp
connect by prior empno = mgr
start with empno = 7839
order by level
--start with mgr is null

--(子查询)
select * from emp where sal > (select sal from emp where ename = 'SCOTT')
/*1.在 where, select,having ,from 后面可以使用子查询,group by之后不可以使用
2.子查询和主查询可以不是同一张表
3.子查询中一般不适用排序,但在TOP—N分析中,需要对子查询排序
4.一般先执行子查询,再执行主查询,但相关子查询除外
5.单行子查询只能使用单行操作符;多行子查询只能使用多行操作符
6.注意子查询中的null*/
select deptno, avg(sal)
from emp
group by deptno
having avg(sal) > (select max(sal) from emp where deptno = 30)
--查询员工号、姓名、月薪
--查询员工号、姓名、月薪、年薪
select * from (select empno,ename,sal,sal*12 annsal from emp)
--不同表
select *
from emp
where deptno = (select deptno from dept where dname = 'SALES')

select e.*
from emp e, dept d
where e.deptno = d.deptno
and d.dname = 'SALES'
--子查询排序 TOP-N
--找到员工表中工资最高前三名
select rownum, empno, ename, sal
from emp
where rownum <= 3
order by sal desc
/*行号永远按默认顺序生成,行号只能使用<,<=,不能使用>,>=*/
select rownum, empno, ename, sal
from (select * from emp order by sal desc)
where rownum <= 3
--相关子查询
--找到员工表中薪水大于本部门平均薪水的员工
select empno,ename,sal,
(select avg(sal) from emp where deptno = e.deptno) avgsal
from emp e
where sal > (select avg(sal) from emp where deptno = e.deptno)
/*单行操作符 =、>、>=、<、<=、<>
多行操作符 in、 any 、all、*/
--单行子查询
select *
from emp
where job = (select job from emp where empno = 7566)
and sal > (select sal from emp where  empno = 7782)

select deptno,min(sal)
from emp
group by deptno
having min(sal) > (select min(sal) from emp where deptno = 20)

--多行查询
select * from emp where deptno in (select deptno from dept where dname='SALES' or dname ='ACCOUNTING')

select e.* from emp e,dept d
where e.deptno=d.deptno and ( d.dname='SALES' or d.dname ='ACCOUNTING')

select * from emp where sal> any (select sal from emp where deptno=30)--大于最小值
select * from emp where sal> select min(sal) from emp where deptno=30

select * from emp where sal> all (select sal from emp where deptno=30)--大于最大值
select * from emp where sal> (select max(sal) from emp where deptno=30)

--null
--查询不是老板的员工
select * from emp where empno not in (select mgr from emp where mgr is not null)
--分页查询
select r, empno, ename, sal
  from (select rownum r, empno, ename, sal
          from (select rownum, empno, ename, sal from emp order by sal desc) e1
         where rownum <= 8) e2
 where r >= 5;
--找到员工表薪水大于本部门平均薪水的员工
select empno, ename,sal,(select avg(sal) from emp where deptno = e.deptno) avgsal
  from emp e
 where sal > (select avg(sal) from emp where deptno = e.deptno)--相关子查询

explain plan for --执行计划
select e.empno, e.ename,e.sal,d.avgsal
  from emp e, (select deptno, avg(sal) avgsal from emp group by deptno) d
 where e.deptno = d.deptno
   and e.sal > d.avgsal--多表查询
select * from table(dbms_xplan.display)--查看执行计划
--相关子查询更省资源

--按部门统计员工人数
select count(*) Total,
       sum(decode(to_char(hiredate, 'YYYY'), '1980', 1, 0)) "1980",
       sum(decode(to_char(hiredate, 'YYYY'), '1982', 1, 0)) "1982",
       sum(decode(to_char(hiredate, 'YYYY'), '1981', 1, 0)) "1981",
       sum(decode(to_char(hiredate, 'YYYY'), '1987', 1, 0)) "1987"
  from emp
  
select (select count(*) from emp) Total,
       (select count(*) from emp where to_char(hiredate, 'YYYY') = '1980') "1980",
       (select count(*) from emp where to_char(hiredate, 'YYYY') = '1981') "1981",
       (select count(*) from emp where to_char(hiredate, 'YYYY') = '1982') "1982",
       (select count(*) from emp where to_char(hiredate, 'YYYY') = '1987') "1987"
  from dual
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: