oracle高级查询(实例基于scott用户四张表)
oracle高级查询(实例基于scott用户四张表)
分组查询
多表查询
子查询
综合实例
=======================================================================
scott用户的四张表(emp,dept,bonus,salgrade)
没有这四张表的可参考http://blog.csdn.net/love_legain/article/details/54311040进行创建
-------------------------------------------
desc emp
名称 空值 类型
-------- -------- ------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
---------------------------------------------
desc dept
名称 空值 类型
------ -------- ------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
------------------------------------------------
desc salgrade
名称 空值 类型
----- -- ------
GRADE NUMBER
LOSAL NUMBER
HISAL NUMBER
------------------------------------------------
desc bonus
名称 空值 类型
----- -- ------------
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
SAL NUMBER
COMM NUMBER
=============================分组查询==================================
①分组函数的概念
分组函数作用于一组数据,并对一组数据返回一个值
②分组函数的使用
--select AVG(sal),sum(sal) from emp;
-- select max(sal),min(sal) from emp;
--select count(*) from emp;
--select count (distinct DEPTNO) from emp;
wm_concat:行转列
select deptno,wm_concat(ename) from emp group by deptno--11gr2和12C上已经摒弃了wm_concat函数
在分组函数中使用nvl函数:nvl函数使分组函数无法忽略空值
select count(*),count(NVL(comm,0)) from emp;
③使用group by子句数据分组
select deptno,avg(sal) from emp group by deptno;
注意:在select列表中所有未包含在组函数中的列都应该包含在group by子句中
包含在group by子句中的列不必包含在select列表中
④使用having子句过滤分组结果集
不能再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子句
⑥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);
sql*plus的报表功能
================================多表查询================================
①什么是多表查询
从多个表中获取数据
②笛卡尔积
③等值连接
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 e.ename 员工姓名,b.ename 老板姓名 from emp e,emp b where e.mgr=b.empno;
自连接存在的问题:不适合操作大表
⑦解决方法:层次查询
select level,empno,ename,sal,mgr from emp connect by prior empno=mgr start with mgr is null order by 1;
==============================子查询===================================
①子查询概述
--查询比scott工资高的员工信息
select * from emp where sal>(select sal from emp where ename='scott');
②子查询的使用
可以使用子查询的位置:where,select,having,from
主查询和子查询可以不是同一张表
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分析问题中,必须对子查询排序
--rownum 行号 伪列
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 和子查询返回的所有值比较
操作符(单行)
= equal to
> greater than
>=greater than or equal to
<less than
<= less than or equal to
<>not equal to
select * from emp where job=(select job from emp where empno=7566) and sal >(select sal from emp where empno=7782);
select * from emp where sal=(select min(sal) from emp);
--查询最低工资大于20号部门最低工资的部门号和部门的最低工资
select deptno,min(sal) from emp group by deptno having min(sal) > (select min(sal) from emp where deptno=20);
--查询部门名称是SALES和ACCOUNTING的员工信息
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 empno not in (select mgr from emp where mgr is not null);
===================================综合实例=============================
实例一
分页查询显示员工信息:显示员工号,姓名,月薪
-每页显示四条记录
-显示第二页的员工
-按照月薪降序排列
select r,emp,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;
--oracle分页通过子查询实现
实例二
找到员工表中薪水大于本部门平均薪水的员工
select e.empno,e.name,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;
实例三
按部门统计员工人数,按照如下格式输出(员工的入职年份已知)
total 1980 1981 1982 1987
14 1 10 1 2
select count(*) total,
sum(decode(to_char(hiredate,'YYYY'),'1980',1,0)) "1980",
sum(decode(to_char(hiredate,'YYYY'),'1981',1,0)) "1981",
sum(decode(to_char(hiredate,'YYYY'),'1982',1,0)) "1982"
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"
from dual;
oracle高级查询(实例基于scott用户四张表)的更多相关文章
- Oracle导入SQL脚本执行 scott 用户下的表删除了
执行 .sql 文件时,应在 sqlplus 或 cmd 中执行,速度比plsql 中的command window 中书许多, scott 用户下的表删除了 可以执行如下 @D:\app\Admi ...
- oracle同一个数据库实例不同的用户之间的表、序列授权操作
1.背景:用户jtuser中有jtproduct中表A,B的同义词,在用户jtuser中向表A,B插入数据,提示“权限不够” 2.将A,B表授权给jtuser用户 $ sqlplus / as sys ...
- Oracle基本的增删改查语句--本人使用scott用户中的表
--感觉有用点个赞^v^ 1 --创建表空间 create tablespace mykebai datafile 'c:\mykebai.dbf' --数据问价存放位置 size 100m --数据 ...
- Scott用户的四张表:
Scott用户的四张表: 转载:http://www.cnblogs.com/mchina/archive/2012/09/06/2649951.html 在Oracle的学习之中,重点使用的是SQL ...
- Oracle记录(三) Scott用户的表结构
在Oracle的学习之中,重点使用的是SQL语句,而所有的SQL语句都要在scott用户下完成,这个用户下一共有四张表,可以使用: SELECT * FROM tab; 查看所有的数据表的名称,如果现 ...
- Oracle笔记(三) Scott用户的表结构
在Oracle的学习之中,重点使用的是SQL语句,而所有的SQL语句都要在scott用户下完成,这个用户下一共有四张表,可以使用: SELECT * FROM tab; 查看所有的数据表的名称,如果现 ...
- 同一个数据库实例,不同用户下多表创建视图,Hibernate完毕ORM映射,Spring整合,后台实现
1.同一个数据库实例.同用户,多表创建视图 2.同一个数据库实例,不同用户下.多表创建视图 3.同一个数据库,不同数据库实例,多表创建视图 4.不同类型数据库,多表创建视图 1.同一个数据库实例.同用 ...
- Oracle中把一张表查询结果插入到另一张表中
1. 新增一个表,通过另一个表的结构和数据 create table XTHAME.tab1 as select * from DSKNOW.COMBDVERSION 2. 如果表存在: inse ...
- 记录一则FGA审计“A用户对B用户某张表的更新操作”需求
环境:Oracle 11.2.0.4 我这里测试A用户为JINGYU,要审计的表为B用户SCOTT下的EMP表.通过FGA来实现. 1.添加审计策略 2.测试审计效果 3.控制审计策略 1.添加审计策 ...
随机推荐
- 咦,好像可以自己做个webapi框架了-IRouteHandler的使用
当我们学习到一定程度的时候,我们会想要去深入了解代码底层的东西,也更想拥有一个属于自己的框架,当然,博主也正是如此.本文可能成为编写一个webapi框架的开端.有研究MVC框架的朋友会发现,mvc框架 ...
- ASP.NET MVC5(三):表单和HTML辅助方法
表单的使用 Action和Method特性 Action特性用以告知浏览器信息发往何处,因此,Action特性后面需要包含一个Url地址.这里的Url地址可以是相对的,也可以是绝对的.如下Form标签 ...
- Redis数据类型之Set
前言:set类似于数学上面的集合概念,包含的元素无序,不能重复,能进行交.并.差操作. 一.内部原理 set数据结构,也是随着元素数目的多少而变化.当set中添加 ...
- PHP获取远程文件的几种方式
1.fopen() 2.file_get_contents() 3.fsocket() 4.curl()
- module.exports,exports,export和export default,import与require区别与联系【原创】
还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...
- ubuntu忽然不能登录,输入密码正确一直返回登录界面
问题描述 由于配置eclipse命令启动,我修改了 /etc/environment 文件的内容,用命令 shutdown -r -now 重启后,输入密码正确一直返回登录界面. 查了下网上资料:系统 ...
- URLWRITE视图重写技术
UrlRewrite就是地址重写,用户得到的全部都是经过处理后的URL地址,类似于Apache的mod_rewrite.将我们的动态网页地址转化为静态的地址,如html.shtml,还可以隐藏网页的真 ...
- JS 事件派发器EventDispatcher
在Java和AS中经常用到EventDispatcher,写了一个JS版本的. addListener :添加事件监听器 removeListener:移除事件监听器 dispatchEvent:派发 ...
- php中json对象数据的输出转化
php中json对象数据的输出转化 public function get_my_now_citys(){ $datas=$this->_post('datas'); //前台js脚本传递给后端 ...
- 用php+mysql+ajax实现淘宝客服或阿里旺旺聊天功能 之 前台页面
首先来看一下我已经实现的效果图: 消费者页面:(本篇随笔) (1)会显示店主的头像 (2)当前用户发送信息显示在右侧,接受的信息,显示在左侧 店主或客服页面:(下一篇随笔) (1)在左侧有一个列表 , ...