黑马程序员c语言教程:子查询简介由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“黑马程序员c语言教程”。
--为什么要用子查询先求scott的工资
select sal from emp
where ename='SCOTT'
select *
from emp
where sal > 3000
//===>
select *
from emp
where sal >(select sal from emp
where ename='SCOTT')
/
子查询的场景
对于一个问题,1步不能求解,需要多步
where ename='SCOTT')子查询的本质 select语言的嵌套
子查询知识架构 合理的书写风格子查询的()不要丢掉子查询和主查询可以不是同一张表,只要子查询返回的结果,主查询能用就行...可以在主查询的什么地方放一个子查询
select a, b, c,(select *......)//ok //在select后面的子查询 必须是单行子查询
from tab1, tab2,(select **)//ok
where...(select *...)//ok
order by...//err
group by...//err
havning...(select ****)// 子查询的分类
按照子查询返回的条目数,分为: 单行子查询和多行子查询
--单行子查询只能用单行比较操作符(=)
--多行子查询只能用多行比较操作符(in any all)
按照子查询和主查询的执行顺序来分
一般子子查询 子查询把结果返回给主查询....一般
相关子查询 主查询把select列中的参数传递给 子查询子查询返回空值的问题子查询中一般不使用order by,但是Top-N问题,子查询必须要用order by
eg: 求工资的前三名 分页....M
子查询结果集一般不排序 ,Top-N问题除外
解释3-eg 查询部门名称是SALES 的员工信息
分析思路: 因为SALES在部门表中,,查找部门表,才能获取部门编号,.....===>员工信息
//子查询
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'
SQL优化
select *
select a, b, c, from....//效率高
// 多表查询 效率高
解释4-1
select empno, ename, sysdate, 'ddddd',(select dname from dept where deptno=10)十号部门名称
from emp
select empno, ename, sysdate, 'ddddd',(select dname from dept)十号部门名称
from emp
第 1 行出现错误:
ORA-01427: 单行子查询返回多个行
解释4-2
---from是一个集合,tab表 本身就是I个集合select a, b, c, from(select *......)//ok //在select后面的子查询 必须是单行子查询
查询员工名字 编号
select *
from
___________________
select *
from
(select empno, ename from emp)
解释5-1
--单行子查询只能用单行比较操作符(=)ppt
select *
from emp
where sal >(select sal from emp
where ename='SCOTT')
解释5-2 多行子查询 多行比较操作符
in
--查询部门名称为 SALES 和 ACCOUNTING 的员工信息
--查询部门名称为 不是 SALES 和 ACCOUNTING 的员工信息
select *
from emp
where deptno in(select deptno from dept where dname='SALES' or dname='ACCOUNTING')
select *
from emp
where deptno not in(select deptno from dept where dname='SALES' or dname='ACCOUNTING')
all any
--查询薪水 比30号部门 所有员工薪高的员工信息 all
select *
from emp
where sal >(select max(sal)from emp where deptno = 30)
select *
from emp
where sal > all(select sal from emp where deptno = 30)
--大于这个集合的最大值 就可以
--查询薪水 比30号部门 任意一个员工薪高的员工信息
select *
from emp
where sal >(select min(sal)from emp where deptno = 30)
select *
from emp
where sal > any(select sal from emp where deptno = 30)
--大于这个集合的最小值 就可以
--any/all的运算 和 前面的逻辑比较运算 相互关联
解释6 有关子查询中的空值 not in(10, 20, null)--查询不是经理的员工信息
--思路 先按照: 查询是经理的员工信息
分析1 是经理的员工信息 1
select *
from emp
where empno in(select mgr from emp)
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----------------------------------------------------------------------------------
7566 JONES MANAGER 7839 02-4月-81 2975 20
7698 BLAKE MANAGER 7839 01-5月-81 2850 30
7782 CLARK MANAGER 7839 09-6月-81 2450 10
7788 SCOTT ANALYST 7566 19-4月-87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20 2
select *
from emp
where empno not in(select mgr from emp)
select *
from emp
where empno not in(select mgr from emp where mgr is not null)
//------------面试强化
1 笛卡尔积 部门表 笛卡尔积产生结果: 行数 两个表相乘列数: 行数相加 原因条件等值连接select ****from tab1, tab2where tab1.a = tab2.a1 select count(e.ename)2 from......
sql语言的类型数据语言实现数据的crud DML语句 (Data Manipulation Language) 数据库操作语言insert update delete select DDL语言 data definition Lanaguagecreate table......
9.通过子查询建表 通过子查询建表的例子SQL>CREATE TABLE emp_41 AS SELECT id, last_name, userid, start_date FROM s_emp WHERE dept_id = 41; SQL> CREATE TABLE A as s......
大小写控制函数 大小写控制函数 字符控制函数数字函数ROUND 函数TRUNC 函数MOD 函数......
--sql structured query language --DML--Data Manipulation Language--数据操作语言 query information (SELECT), add new rows (INSERT), modify existing rows (UPDATE)......