一、单表查询

语法: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的更多相关文章

  1. oracle(5)--DQL查询语句

    DQL 数据查询语句(data query language) 1.查询条件符号: < ,  > ,  = ,    <= ,  >= ,    != ,  < > ...

  2. Oracle查询DQL脚本记录

    --查询列 Select t.sname,t.ssex,t.class from student t --t 别名; Select *from student t; --* 代表查询表内所有数据 '; ...

  3. Oracle的DQL

    基本查询: 链接语句: sqlplus scott/tiger@192.168.56.101:1521/orcl SQL> --清屏 SQL> host cls (host clear) ...

  4. oracle学习笔记(四) DQL数据查询语言和TCL 事务控制语言

    DML 数据管理语言 Data manage language insert, update, delete以及select语句,不过,有人也把select单独出来,作为DQL 数据查询语言 data ...

  5. Oracle——DQL、DML、DDL、DCL

    1.DQL:数据查询语言 基本结构:由select.from.where组成 子句组成的查询块:    SELECT <字段名表>    FROM <表或视图名>    WHE ...

  6. Oracle数据库基本操作(三) —— DQL相关内容说明及应用

    本文所使用的查询表来源于oracle数据中scott用户中的emp员工表和dept部门表. 一.基本语法 SQL语句的编写顺序: select 输出的列 from 表名 where 条件 group ...

  7. Oracle DQL查询语言整理

    select * from t_hq_ryxx; select nianl, xingm from t_hq_ryxx; select nianl as 年龄, xingm as 姓名 from t_ ...

  8. [原创]关于ORACLE的使用入门

    Oracle===============================数据库:Oracle------>甲骨文(Oracle) 49+%DB2---------->IBM 49+%Sq ...

  9. Oracle的SQL基础

    1.了解SQL的种类 (1)DDL 数据定义语言:定义数据库中数据要如何存储的,包括对数据库对象的创建(create)修改(alter)删除(drop)的操作,这些对象主要有数据库,数据表,视图,索引 ...

随机推荐

  1. python----openpyxl模块

    openpyxl 模块 1.openpyxl的写 from openpyxl import Workbook wb = Workbook() # 方式一: 默认创建sheet在最后 wb1 = wb. ...

  2. 记录开发Nodejs c++ addon的一些经验(四、electron)

    如果我们要在electron里使用我们开发的addon,那么直接使用是不行的. 官方的解释是:Electron 同样也支持原生模块,但由于和官方的 Node 相比使用了不同的 V8 引擎,如果你想编译 ...

  3. 求解2的N次幂的问题(多种方法)

    #include <iostream> using namespace std; //计算2的N次幂 //一般方法,时间复杂度为2^n __int64 pow2(int n) { __in ...

  4. c# FTP操作类(转)

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...

  5. Nginx 性能参数优化

    user www www; # ginx要开启的进程数 一般等于cpu的总核数,没必要开那么多,1个nginx内存消耗10兆左右 worker_processes 4; # 为每个进程分配cpu,上例 ...

  6. 并发包交换数据Exchanger

    /** * * @描述: 用于实现两个人之间的数据交换,每个人完成一定的事务后想与对方交换数据,第一个先拿出数据的人一直等待 * 直到第二个人拿到数据 到来时,才能彼此交换数据. * @作者: Wnj ...

  7. SQL点点滴滴_查询类型和索引-转载

    当您考虑是否要对列创建索引时, 请估计在查询中使用列的方式, 下表介绍了索引对其有用的查询类型. 表中的示例基于 AdventureWorks2008R2 示例数据库, 在 SQL Server Ma ...

  8. Oracle物化视图详解

    现实工作中会有多个数据源同步到一个数据库完成数据分析的场景,这些数据可以不是实时同步的,我们一般通过定时任务抽取数据到统计分析库给应用使用. 一般的同步方式可以通过时间戳做全量和增量数据同步(存在原数 ...

  9. 解决maven工程无法创建src/main/java包名的方法

    我的maven工程不知道为什么无法创建src/main/java这样的包,我创建好的maven工程只有src/main/resources包,其他的主要包都没有,而且不能创建包,new出来的包都是一个 ...

  10. FQDN说明

    以下摘自百度百科: FQDN:(Fully Qualified Domain Name)完全合格域名/全称域名,是指主机名加上全路径,全路径中列出了序列中所有域成员.全域名可以从逻辑上准确地表示出主机 ...