记得自己要敲o~~~

  1. select * from bonus;
  2. select * from salgrade;
  3. select 1+1 from dual;
  4. --笛卡尔积:两张表的乘积
  5. select * from emp,dept;
  6.  
  7. select * from emp e1,dept d1 where e1.deptno =d1.deptno;
  8.  
  9. /*
  10. 内联接:
  11. 隐式内联接:
  12. 不等值内联接:where e1.deptno <> d1.deptno
  13. 自联接:自己连接自己
  14. 等值内联接: where e1.deptno = d1.deptno;
  15. 显示内联接:
  16. select * from 表1 inner join 表2 链接条件
  17.  
  18. inner关键字可以省略
  19. */
  20. select * from emp e1,dept d1 where e1.deptno <> d1.deptno;
  21.  
  22. --查询员工编号,员工姓名,经理编号,经理的姓名
  23.  
  24. select e1.empno,e1.ename,e1.mgr,m1.ename
  25. from emp e1,emp m1 where e1.mgr = m1.empno;
  26.  
  27. --查询员工编号,员工姓名,员工的部门名称,经理的编号,经理的姓名
  28. select e1.empno,e1.ename,e1.mgr,m1.ename,d1.dname
  29. from emp e1,emp m1,dept d1 where e1.mgr = m1.empno and e1.deptno = d1.deptno;
  30.  
  31. --查询员工编号,员工姓名,员工的部门名称,经理的编号,经理的姓名,经理的部门名称
  32. select e1.empno,e1.ename,d1.dname,e1.mgr,m1.ename,d2.dname
  33. from emp e1,emp m1,dept d1,dept d2
  34. where e1.mgr = m1.empno
  35. and e1.deptno = m1.deptno and d2.deptno = m1.deptno;
  36. select * from dept;
  37. select * from emp;
  38. select * from salgrade;
  39.  
  40. --查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的部门名称
  41. select e1.empno,e1.ename,d1.dname,s1.grade,e1.mgr,m1.ename,d2.dname
  42. from emp e1, emp m1,dept d1,dept d2,salgrade s1
  43. where
  44. e1.mgr= m1.empno
  45. and e1.deptno = d1.deptno
  46. and m1.deptno = d2.deptno
  47. and e1.sal between s1.losal and s1.hisal ;
  48. --查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的部门名称,经理的工资等级
  49. select e1.empno,e1.ename,d1.dname,s1.grade,e1.mgr,m1.ename,d2.dname,s2.grade
  50. from emp e1, emp m1,dept d1,dept d2,salgrade s1,salgrade s2
  51. where
  52. e1.mgr= m1.empno
  53. and e1.deptno = d1.deptno
  54. and m1.deptno = d2.deptno
  55. and e1.sal between s1.losal and s1.hisal
  56. and m1.sal between s2.losal and s2.hisal ;
  57. --查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的部门名称,经理的工资等级
  58. --将工资登记1,2,3,4显示成中文的一级二级,三级四级
  59. select e1.empno,
  60. e1.ename,
  61. d1.dname,
  62. case s1.grade
  63. when 1 then '一级'
  64. when 2 then '二级'
  65. when 3 then '三级'
  66. when 4 then '四级'
  67. else
  68. '五级'
  69. end "等级",
  70. e1.mgr,
  71. m1.ename,
  72. d2.dname,
  73. decode(s2.grade,1,'一级',2,'二级',3,'三级',4,'四级','五级') "等级"
  74. from emp e1, emp m1,dept d1,dept d2,salgrade s1,salgrade s2
  75. where
  76. e1.mgr= m1.empno
  77. and e1.deptno = d1.deptno
  78. and m1.deptno = d2.deptno
  79. and e1.sal between s1.losal and s1.hisal
  80. and m1.sal between s2.losal and s2.hisal ;
  81.  
  82. --查询员工姓名和员工部门所处的位置
  83. select e1.ename,e1.deptno from emp e1 ,dept d1 where e1.deptno = d1.deptno;
  84.  
  85. select * from emp e1 inner join dept d1 on e1.deptno = d1.deptno;
  86.  
  87. /*
  88. 外联接:(标准,通用写法)
  89. 左外联接:left outer join 左表中所有的记录,如果右表没有对应记录,就显示空
  90. 右外连接:right outer join 右表中所有的记录,如果左表没有对应记录,就显示空
  91. outer 关键字可以省略
  92. Oracle中的外连接:(+)实际上是没有对应的记录就加上空值
  93. select * from emp e1,dept d1 where e1.deptno = d1.deptno(+);
  94.  
  95. */
  96. select * from emp e1 left outer join dept d1 on e1.deptno = d1.deptno;
  97. insert into emp(empno,ename) values(9527,'HUAAN');
  98. select * from emp e1,dept d1 where e1.deptno = d1.deptno(+);
  99. select * from emp e1 right outer join dept d1 on e1.deptno = d1.deptno;
  100.  
  101. select * from emp e1,dept d1 where e1.deptno(+) = d1.deptno;
  102.  
  103. /*
  104. 子查询:查询语句中嵌套查询语句;用来解决复杂的查询语句
  105. 查询最高工资的员工的信息
  106. 单行子查询:> >= = < <= <> !=
  107.  
  108. 多行子查询: in not in >any >all exists not exists
  109. 查询领导信息
  110. */
  111. --查询最高工资的员工信息
  112. --1.查询最高工资
  113. select max(sal) from emp ;
  114. --2.工资等于最高工资
  115. select * from emp where sal=(select max(sal) from emp);
  116.  
  117. --查询出比雇员7654的工资高,同时和7788从事相同工作的员工信息
  118. --1.雇员7654的工资 1250
  119. select sal from emp where empno=7654;
  120.  
  121. --2.7788从事的工作 ANALYST
  122. select job from emp where empno= 7788;
  123. select* from emp where
  124. (select sal from emp where empno=7654)<sal and job=(select job from emp where empno= 7788);
  125.  
  126. select * from emp where sal > (select sal from emp where empno = 7654) and job = (select job from emp where empno = 7788);
  127.  
  128. --查询每个部门最低工资的员工信息和他所在的部门信息
  129. --1.查询每个部门的最低工资,分组统计
  130. select deptno, min(sal) minsal from emp group by deptno;
  131. --2.员工工资等于他所处部门的最低工资
  132. select * from emp e1,
  133. (select deptno, min(sal) minsal from emp group by deptno)
  134. t1 where e1.deptno = t1.deptno and e1.sal = t1.minsal;
  135.  
  136. --3.查询部门相关信息
  137. select *
  138. from emp e1,
  139. (select deptno,min(sal) minsal from emp group by deptno) t1,
  140. dept d1
  141. where e1.deptno = t1.deptno and e1.sal = t1.minsal and e1.deptno = d1.deptno;
  142. /*
  143. 内联接,单行自查询,多行子查询
  144.  
  145. in
  146. not in
  147. any
  148. all
  149. exists
  150.  
  151. 通常情况下,数据库中不要出现null,最好的做法是加上not null
  152. null值并不代表不占用空间,char(100)null 100个字符
  153. */
  154. --查询领导信息
  155. --1.查询所有经理的编号
  156. select mgr from emp;
  157. select distinct mgr from emp;
  158. --2.结果
  159. select * from emp where empno in (select mgr from emp);
  160. --查询不是领导的信息
  161. select * from emp where empno not in (select mgr from emp);
  162. select * from emp where empno <>all(select mgr from emp);
  163. --正确写法
  164. select * from emp where empno not in(select mgr from emp where mgr is not null);
  165. --查询出比10号部门任意一个员工薪资高的员工信息 10 20 30
  166. select * from emp where sal > any(select sal from emp where deptno=10);
  167. --查询出比20号部门所有员工薪资高的员工信息 10 20 30
  168. --1.20号最高工资 3000
  169. select max(sal) from emp where deptno =20;
  170. --2.员工信息
  171. select * from emp;
  172. --合并
  173. select * from emp where sal > (select max(sal) from emp where deptno =20);
  174.  
  175. -----使用多行子查询完成上面这题
  176. --20号部门所有员工的薪资
  177. select sal from emp where deptno = 20;
  178. --大于集合所有的
  179. select * from emp where sal > all(select sal from emp where deptno=20);
  180.  
  181. /*
  182. exists(查询语句):存在的意思,判断一张表里的记录是否存在于另外一张表中
  183. 当作布尔值来处理
  184. 当查询语句有结果的时候,就返回true;否则false;
  185. 数据量比较大的时候是非常高效的
  186. */
  187. select * from emp where exists(select * from emp where deptno = 1234567);
  188. select * from emp where 3=4;
  189.  
  190. select * from emp where exists(select * from emp where deptno = 20);
  191.  
  192. --查询有员工的部门的信息
  193. select * from dept d1 where exists(select * from emp e1 where e1.deptno = d1.deptno );
  194.  
  195. --找到员工表中工资最高的前三名(降序排序)
  196. select * from emp order by sal desc;
  197. /*
  198. rownum : 伪列 ,系统自动生成一列,用来表示行号
  199. rownum是Oracle特有的用来表示行号,默认值/起始值是1,在每查询出结果之后,在添加1
  200. rownum最好不能做大于号判断,可以做小于号判断
  201.  
  202. sql执行顺序
  203. from .. where..group by..having..select ..rownum..order by
  204. */
  205. select rownum ,e1.* from emp e1;
  206.  
  207. --查询rownum大于2的所有记录
  208. select rownum , e1.* from emp e1 where rownum>2;--没有任何记录
  209. --查询rownum大于等于1的所有记录
  210. select rownum , e1.* from emp e1 where rownum>=1;
  211. --查询rownum < 6 的所有记录
  212. select rownum,e1.* from emp e1 where rownum < 6;
  213. --rownum 排序
  214. select rownum ,e1.* from emp e1 order by sal;
  215.  
  216. --找到员工表中工资最高的前三名
  217. select rownum ,e1.* from emp e1 order by sal desc ;
  218. --将上面的结果当作一张表处理,再查询
  219. select rownum ,t1.* from (select rownum ,e1.* from emp e1 order by sal desc) t1;
  220.  
  221. --只显示前三条记录
  222. select rownum ,
  223. t1.* from(select rownum ,e1.* from emp e1 order by sal desc) t1 where rownum<4;
  224.  
  225. --找到员工表中薪水大于本部门平均薪水的员工
  226. --1.分组统计部门平均薪水
  227. select deptno, avg(sal) avgsal from emp group by deptno;
  228. --2.员工工资 > 本部门平均工资
  229. select * from emp e1,
  230. (select deptno,avg(sal) avgsal from emp group by deptno) t1
  231. where e1.sal>t1.avgsal and e1.deptno = t1.deptno;
  232.  
  233. /*
  234. 关联子查询 , 非关联子查询
  235. */
  236. select * from emp e where
  237. sal > (select avg(sal) from emp e2 group by deptno having e.deptno=e2.deptno)
  238.  
  239. /*
  240. 统计每年入职的员工个数
  241. */
  242. select hiredate from emp;
  243. --只显示年
  244. select to_char(hiredate,'yyyy') from emp;
  245.  
  246. --分组统计
  247. select to_char(hiredate,'yyyy')yy,count(1) cc from emp group by to_char(hiredate,'yyyy');
  248.  
  249. select case yy when '' then cc end
  250. from
  251. (select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt;
  252.  
  253. select yy
  254. from
  255. (select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt;
  256.  
  257. select case yy when '' then cc end "1987"
  258. from
  259. (select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt;
  260.  
  261. select case yy when '' then cc end "1987"
  262. from
  263. (select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt;
  264.  
  265. --去除行记录中的空值
  266. select sum(case yy when '' then cc end) "1987"
  267. from
  268. (select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt;
  269.  
  270. --统计员工的总数
  271. select sum(cc) "TOTAL"
  272. from
  273. (select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy'))
  274.  
  275. --将1987 TOTAL 合并在一起
  276. select sum(cc) "TOTAL",
  277. sum(case yy when '' then cc end)"1987"
  278. from
  279. (select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt;
  280.  
  281. --显示所有年份的结果
  282. select
  283. sum(cc) "TOTAL",
  284. sum(case yy when '' then cc end) "1980",
  285. sum(case yy when '' then cc end) "1981",
  286. sum(case yy when '' then cc end) "1982",
  287. sum(case yy when '' then cc end) "1987"
  288. from
  289. (select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt;
  290.  
  291. /*
  292. rowid :伪列 每行记录所存放的真实物理地址
  293. rownum :行号 每查询出记录之后,就会添加一个行号
  294. */
  295. select rowid ,e.* from emp e;
  296.  
  297. /*
  298. rownum:分页查询
  299. 在Oracle中只能使用子查询来做分页查询
  300. */
  301. select rownum, emp.* from emp;
  302. select * from (select rownum hanghao ,emp.* from emp)tt
  303. where tt.hanghao between 6 and 10;
  304.  
  305. /*
  306. 集合运算:
  307. 并集:将两个查询结果进行合并
  308. 交集
  309. 差集
  310.  
  311. 所有的查询结果可能不是来自同一张表
  312. emp 2000年
  313. 2017年 手机 详细信息 emp2017
  314. */
  315. --工资大于1500,或者20号部门下的员工
  316. select * from emp where sal > 1500 or deptno = 20;
  317.  
  318. --并集运算: union union all
  319. /*
  320. union:去除重复的,并且排序
  321. union all:不会去除重复的
  322. */
  323. select * from emp where sal>1500
  324. union
  325. select * from emp where deptno = 20;
  326.  
  327. select * from emp where sal > 1500
  328. union all
  329. select * from emp where deptno = 20;
  330.  
  331. /*
  332. 交集运算: intersect
  333.  
  334. */
  335. --工资大于1500,并且20号部门下的员工
  336.  
  337. select * from emp where sal>1500
  338. intersect
  339. select * from emp where deptno = 20;
  340.  
  341. /*
  342. 差集运算: 两个结果相减
  343. */
  344. --1981年入职员工(不包括总裁和经理)
  345. --1981年入职员工
  346. select * from emp where to_char(hiredate,'yyyy')= 1981;
  347. --总裁和经理
  348. select * from emp where job = 'PRESIDENT' or job = 'MANAGER';
  349.  
  350. select * from emp where to_char(hiredate,'yyyy')= 1981
  351. minus
  352. select * from emp where job = 'PRESIDENT' or job = 'MANAGER';
  353.  
  354. /*
  355. 集合运算中的注意事项
  356. 1.列的类型要一致
  357. 2.按照顺序写
  358. 3.列的数量要一致,如果不足,用空值填充
  359. */
  360. select ename,sal from emp where sal > 1500
  361. union
  362. 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. 微信网页动画---swiper.animate.css

    项目需要,自己写了个demo <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  2. [转载]strtok函数和strtok_r函数

    1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct person{     char name[25];     char sex[1 ...

  3. 网页制作中最有用的免费Ajax和JavaScript代码库

    网上看到的一篇小文,挺有用的,收藏在这. 本文中,我整理了12个免费的Ajax和JavaScript代码库,可以帮助Web开发人员将应用程序提升到一个新水平. Ajax Instant Messeng ...

  4. 2017萧山第5场(2016 Pacific Northwest - Division 1)

    B:Buggy Robot [题意] 一个n*m的地图(1≤n, m≤50),有一个入口和一个出口.给定一个命令序列(上,下,左,右),如果碰到障碍或者边际就忽略.问至少加入或删除多少个的命令,使得能 ...

  5. sklearn进行拟合

    # codind:utf-8 from sklearn.linear_model import SGDRegressor,LinearRegression,Ridge from sklearn.pre ...

  6. sklearn_k邻近分类_KNeighborsClassifier

    # coding:utf-8 import numpy as np import matplotlib.pyplot as plt from sklearn.neighbors import KNei ...

  7. html5 构造网页的新方式

    从 html 诞生至今,我们构建 html 页面的使用 html 元素好像并没有太多的进步.在构建 html 页面中,用的最多的是 div 标签.但是应用 div 标签构建 html 页面有一个问题, ...

  8. 阿里云CentOS下安装jdk

    首先需要下载jdk: 由于oracle上的下载页面有跳转,直接用wget下载下来的只是html页面.可以用下面的命令: wget --no-cookies --no-check-certificate ...

  9. [Openwrt 扩展下篇] Openwrt搭建私有云Owncloud 9

    网上很多资料讲用Linux打造owncloud构建私有云 ,花了些时间研究了下,我将之前的需求打造成了Openwrt下的Owncloud 9.其实网上还有Seafile.大家对比来看下知乎的评论,其实 ...

  10. 差分约束系统+spfa(B - World Exhibition HDU - 3592 )

    题目链接:https://cn.vjudge.net/contest/276233#problem/B 思路和上一个一样,不过注意点有两个,第一,对dis数组进行初始化的时候,应该初始化成ox3f3f ...