3----
单行子查询---仅仅返回一条记录
多行子查询---返回多行记录,2行及其以上
select empno,ename,sal,(select job from emp where empno=7839) 第四列
from emp;
select deptno,avg(sal)
from emp
group by deptno
having avg(sal) > (select max(sal)
from emp
where deptno=30);
-- 这having不能用where替代,有avg
select *
from(select empno,ename,sal from emp);
-- from 中加子查询的,用的很多
-- 括号里面,相当于一个新的结果集
*******************************************************
SQL> select empno,ename,sal,(select job from emp where empno=7839) 第四列
2 from emp;
EMPNO ENAME SAL 第四列
---------- ---------- ---------- ---------
7369 SMITH 800 PRESIDENT
7499 ALLEN 1600 PRESIDENT
7521 WARD 1250 PRESIDENT
7566 JONES 2975 PRESIDENT
7654 MARTIN 1250 PRESIDENT
7698 BLAKE 2850 PRESIDENT
7782 CLARK 2450 PRESIDENT
7788 SCOTT 3000 PRESIDENT
7839 KING 5000 PRESIDENT
7844 TURNER 1500 PRESIDENT
7876 ADAMS 1100 PRESIDENT
EMPNO ENAME SAL 第四列
---------- ---------- ---------- ---------
7900 JAMES 950 PRESIDENT
7902 FORD 3000 PRESIDENT
7934 MILLER 1300 PRESIDENT
已选择14行。
SQL> select deptno,avg(sal)
2 from emp
3 group by deptno
4 having avg(sal) > (select max(sal)
5 from emp
6 where deptno=30);
DEPTNO AVG(SAL)
---------- ----------
10 2916.66667
SQL> select *
2 from(select empno,ename,sal from emp);
EMPNO ENAME SAL
---------- ---------- ----------
7369 SMITH 800
7499 ALLEN 1600
7521 WARD 1250
7566 JONES 2975
7654 MARTIN 1250
7698 BLAKE 2850
7782 CLARK 2450
7788 SCOTT 3000
7839 KING 5000
7844 TURNER 1500
7876 ADAMS 1100
EMPNO ENAME SAL
---------- ---------- ----------
7900 JAMES 950
7902 FORD 3000
7934 MILLER 1300
已选择14行。
SQL> select empno,ename,sal from emp;
EMPNO ENAME SAL
---------- ---------- ----------
7369 SMITH 800
7499 ALLEN 1600
7521 WARD 1250
7566 JONES 2975
7654 MARTIN 1250
7698 BLAKE 2850
7782 CLARK 2450
7788 SCOTT 3000
7839 KING 5000
7844 TURNER 1500
7876 ADAMS 1100
EMPNO ENAME SAL
---------- ---------- ----------
7900 JAMES 950
7902 FORD 3000
7934 MILLER 1300
已选择14行。
SQL>
可以使用子查询的位置:select,where,having,from
select 后面的子查询语句必须是单行子查询语句
select empno,ename,sal,(select job from emp where empno=7839) 第四列 from emp;
having子查询语句

from子查询语句


可以使用子查询的位置,group by后不可使用子查询,having where from select后面可以。
(3)可以使用子查询的位置:where,select,having,from :
where后面的子查询中不能有分组函数
having后面的子查询中可以有分组函数
select后面的子查询得到的结果必须是单条记录
from后面的子查询是把子查询的内容当作一个新表
可以使用子查询的位置
select empno,(select deptno from emp where empno='1') --select后面的子查询只能是返回单行记录
from emp;
where后面的子查询中不能有分组函数
having后面的子查询中可以有分组函数
select后面的子查询得到的结果必须是单条记录
from后面的子查询得到的结果必须是多条记录
直接跳过where的子查询还真行
select 子查询只能有一行,不行的话就把它变成一行
可以使用子查询的位置:where select having from
1、WHERE
select * from emp where sal>(select sal from emp where ename='SCOTT')
2、SELECT
select empno,ename,sal,(select job from emp where empno=7839) 第四列 from emp;
3、HAVING
select deptno,avg(sal) from emp group by deptno having avg(sal)>(select max(sal) from emp where deptno=30);
4、FROM
select * from (select empno,ename,sal from emp);
having和from的区别 having后可以跟组函数,from后面不可以
单行子查询 多行子查询??
select子查询必须是单行子查询,单行子查询只能返回一行记录
from后面的子查询是把子查询的内容当作一个新表
select *
from emp
where sal>(select sal
from emp
where ename='SCOTT');
select * from emp where sal>(select sal from emp where ename='SCOTT'); -- where字句后的子查询
2. select empno,ename,sal, (select job from emp where empno=7838) 第四列
from emp --在select子句后的子查询必须是单行只查询(只返回一条记录)
3.select deptno,avg(sal)
from emp
group by deptno
having avg(sal) >(select max(sal) from emp where deptno=30);
4.select *
from (select empno,ename,sal from emp);
SELECT 语句后的子查询必须是单行子查询

大于30号部门平均工资的部门

having 语句后面放置子查询,查询所有工资的平均值大于部门是30号的最大值,不能用where
因为有聚合函数
select 子查询必须是单行
select语句后面的子查询必须是一个单行子查询
【语句只返回一条记录就是单行子查询,返回多条记录就是多行子查询】
select 后面的子查询只能是单行子查询(单行子查询是指返回一条记录的子查询)
--where 子查询
select *
from emp
where sal > (select sal
from emp
where ename = 'SCOTT' );
--select 子查询
select ename,(select job from emp where empno=7839)
from emp
--from 子查询
select ename
from (select * from emp)
--having 子查询
select deptno,avg(sal)
from emp
group by deptno
having avg(sal)>(select avg(sal) from emp where deptno=20)
使用子查询,where ,having,select(单行子查询,返回一个结果),from(查询语句,看成是一张表),group by 不能使用子查询