oracle之DQL
一、单表查询
语法:select * from table where 条件 group by 分组 having 过滤分组 order by 排序
--查询平均工资低于2000的部门的最大工资和平均工资
select deptno, max(sal), avg(sal)
from emp
group by deptno
having avg(sal) < 2000
order by deptno asc;
模糊查询,_表示占位一个字节,%填充剩下的位数
--查询员工姓名以A开头,并且第三个字母为L的员工信息
select * from emp where ename like 'A_L%';
分页查询
--查询员工表中第三条到第五条数据
select *
from (select emp.*, rownum as rn from emp where rownum <= 5)
where rn > 2;
二、嵌套子查询
单行子查询
子查询返回的值是单行单列的
--查询薪资高于平均工资的员工信息
select * from emp where sal > (select avg(sal) from emp);
多列子查询
多列子查询中的条件与查询结果必须依次对应,否则会报错
--查询与smith部门和岗位完全相同的员工
select *
from emp
where (deptno, job) = (select deptno, job from emp where ename = 'SMITH');
多行子查询
多行子查询就是嵌套在其他sql语句中返回多行数据,常用的条件是in,not in,any,all等,any和all必须和比较运算符一起使用
>all = >max | >any = >min | <all = <min | <any = <max
--查询比部门30所有员工的工资都要高的员工信息
select * from emp where sal > all (select sal from emp where deptno = 30); --查询比部门10中员工的工资高的员工信息
select * from emp where sal > any (select sal from emp where deptno = 10); --查询部门10和部门20的员工信息
select * from emp where deptno in (10,20);
in和exists,在9i时代,exists适合用于字表查询量大的情况,in适用于父表查询量大的时候,在9i之后,ORACLE优化器有个查询转换器,很多SQL虽然写法不同,但是ORACLE优化器会根据既定规则进行查询重写,重写为优化器觉得效率最高的SQL,所以可能SQL写法不同,但是执行计划却是完全一样的。
相关子查询
相关子查询依赖于外部的条件,不能单独执行,而非相关子查询可以单独执行
--查询在dept表中存在的部门员工信息
select ename,sal,deptno from emp where exists (select 1 from dept
where dept.deptno = emp.deptno)
三、多表查询
内连接
内连接也叫连接,是最早的一种连接。还可以被称为普通连接或者自然连接,内连接是从结果表中删除与其他被连接表中没有匹配行的所有行,所以内连接可能会丢失信息。
--内连接的两种写法,只返回符合条件的结果
select * from emp inner join dept on emp.deptno = dept.deptno;
select * from emp, dept where emp.deptno = dept.deptno;
内连接只要写上连接条件就不会产生笛卡尔积
外连接
左外连接、右外连接、满外连接
左外连接以左边的表的为基础,右表分别与左表中的数据进行匹配,右表中的数据可能有空值;
而右外连接正好相反,左表中可能有空值;
满外连接左右两边都可能有空值。
--左外连接的两种写法:
select * from emp, dept where emp.deptno = dept.deptno(+);
select * from emp left outer join dept on emp.deptno = dept.deptno;

--右外连接的两种写法
select * from emp ,dept where emp.deptno(+) = dept.deptno;
select * from emp right join dept on emp.deptno = dept.deptno;

--满外连接的写法
select * from emp full outer join dept on emp.deptno = dept.deptno;
重点关注:WHERE子句中的连接顺序.
重点关注 ORACLE采用自下而上的顺序解析WHERE子句,根据这个原理,表之间的连接必须写在其他WHERE条件之前, 那些可以过滤掉最大数量记录的条件必须写在WHERE子句的末尾.
--列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,公司的工资等级
select a.ename, a.sal, dname, b.ename, grade
from emp a, emp b, dept d, salgrade s
where a.deptno = d.deptno(+)
and a.mgr = b.empno
and a.sal between losal and hisal
and a.sal > (select avg(sal) from emp);
oracle之DQL的更多相关文章
- oracle(5)--DQL查询语句
DQL 数据查询语句(data query language) 1.查询条件符号: < , > , = , <= , >= , != , < > ...
- Oracle查询DQL脚本记录
--查询列 Select t.sname,t.ssex,t.class from student t --t 别名; Select *from student t; --* 代表查询表内所有数据 '; ...
- Oracle的DQL
基本查询: 链接语句: sqlplus scott/tiger@192.168.56.101:1521/orcl SQL> --清屏 SQL> host cls (host clear) ...
- oracle学习笔记(四) DQL数据查询语言和TCL 事务控制语言
DML 数据管理语言 Data manage language insert, update, delete以及select语句,不过,有人也把select单独出来,作为DQL 数据查询语言 data ...
- Oracle——DQL、DML、DDL、DCL
1.DQL:数据查询语言 基本结构:由select.from.where组成 子句组成的查询块: SELECT <字段名表> FROM <表或视图名> WHE ...
- Oracle数据库基本操作(三) —— DQL相关内容说明及应用
本文所使用的查询表来源于oracle数据中scott用户中的emp员工表和dept部门表. 一.基本语法 SQL语句的编写顺序: select 输出的列 from 表名 where 条件 group ...
- Oracle DQL查询语言整理
select * from t_hq_ryxx; select nianl, xingm from t_hq_ryxx; select nianl as 年龄, xingm as 姓名 from t_ ...
- [原创]关于ORACLE的使用入门
Oracle===============================数据库:Oracle------>甲骨文(Oracle) 49+%DB2---------->IBM 49+%Sq ...
- Oracle的SQL基础
1.了解SQL的种类 (1)DDL 数据定义语言:定义数据库中数据要如何存储的,包括对数据库对象的创建(create)修改(alter)删除(drop)的操作,这些对象主要有数据库,数据表,视图,索引 ...
随机推荐
- FLASK实现上传下载功能
#!-*-coding=utf-8-*- # from flask import Flask # # app = Flask(__name__) # # # @app.route('/') # def ...
- 使用electron构建跨平台Node.js桌面应用
最近,把团队内经常使用的一个基于Node.js制作的小工具给做成了可视化操作的桌面软件,使用的是electron,这里简单分享一下使用electron的一些经验和心得. 一.如何使用electron把 ...
- Sort函数的用法
快速排序sort的用法:(适用于int float double char ...) 记得加头文件! 记得加头文件! 记得加头文件! 头文件: #include <algorithm> ...
- 学习C++从入门到精通的的十本最经典书籍
原文:http://blog.csdn.net/a_302/article/details/17558369 最近想学C++,找了一下网上推荐的书籍,转载过来给大家分享 转载自http://c.chi ...
- 如何有效防止API的重放攻击(转自阿里云)
API重放攻击(Replay Attacks)又称重播攻击.回放攻击,这种攻击会不断恶意或欺诈性地重复一个有效的API请求.攻击者利用网络监听或者其他方式盗取API请求,进行一定的处理后,再把它重新发 ...
- 给Docker武士们的正式邀请,赶紧收哦!
亲爱的Docker武士,Docker大师们喊你来参加Docker的定期聚啦~收好时间.地点,快来相见.切磋Docker吧!5月17日,微软上海港汇办公室,我们与你不见不散! 点击阅读原文,或直接进入注 ...
- Python 爬虫 根据属性值关键字搜索标签
# <div class='\"name\"'>客如云</div> company_name = soup.find_all('div',class_=re ...
- 【Leetcode】【Medium】Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Python学习---线程锁/信号量/条件变量同步/线程池1221
线程锁 问题现象: 多线程情况下,CPU遇到阻塞会进行线程的切换,所以导致执行了tmp-=1的值还未赋值给num=tmp,另一个线程2又开始了tmp -=1,所以导致最后的值重复赋值给了num,所以出 ...
- Android Studio添加取消代码注释快捷键
经常需要注释,取消注释代码 Ctrl + / 对每段代码前面添加或者取消 // Ctrl + Shift + / 对代码添加 或取消 /* */ Ctrl + B 查找定义 C ...