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
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【125-Valid Palindrome(回文字验证)】
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- 【LeetCode-面试算法经典-Java实现】【120-Triangle(三角形)】
- iOS开发-Instruments性能调优
- C语言的局部变量和外部变量
- poj1195 二维线段树
- Wireshark基本介绍和学习TCP三次握手
- 创业失败:100多万血本耗尽 工厂名存实亡
- 去大公司还是去小公司工作——要进大公司的核心部门(提升视野,锻炼技能),远离没真本事的小公司,要自我驱动 good
- leetcode 6 ZigZag Conversion
- [LeetCode] Triangle,
- 菜鸟学前端之遍寻名师之混乱的笔记
- kindeditor远程图片没问题,上传本地图片报:上传目录不存在
- LeetCode Roman to Integer
- LeetCode Roman to Integer
- coredata VS sqlist
- ds18b20驱动第四天
- Codeforces Round #316 (Div. 2)
- BZOJ2792 : [Poi2012]Well
- Codeforces Round #316 (Div. 2)-A. Elections-模拟水题22