where
单行单列
SELECT AVG(sal) from emp;
SELECT * FROM EMP
WHERE sal <(SELECT AVG(sal) from emp);
单行多列
SELECT * FROM emp
where (job,sal) = (SELECT job,sal FROM emp WHERE ename = 'SCOTT') AND ename <> 'SCOTT';
多行单列
select * from emp where sal in(
SELECT sal FROM emp Where job = 'MANAGER'
);
not in 有空则无返回结果
ANY 和in一样
select * from emp where sal =ANY(
SELECT sal FROM emp Where job = 'MANAGER'
);
>ANY比子查询的最小值大
select * from emp where sal >ANY(
SELECT sal FROM emp Where job = 'MANAGER'
);
<ANY比子查询的最大值小 >ALL比子查询最大值大
select * from emp where sal >ALL(
SELECT sal FROM emp Where job = 'MANAGER'
); <ALL比子查询最小值小
select * from emp where sal <ALL(
SELECT sal FROM emp Where job = 'MANAGER'
);
having
在having中单行单列,统计运算:
SELECT AVG(sal) FROM emp; SELECT job,count(ename),AVG(SAL) FROM emp
Group by job
having AVG(SAL)>(SELECT AVG(sal) FROM emp)
select
子查询在select中
SELECT e.ename,e.empno,e.job
,(select d.dname from dept d where d.deptno=e.deptno) dname
from emp e;
from
FROM
SELECT d.dname,d.loc,temp.count
FROM dept d,(SELECT deptno,count(empno) count FROM emp
Group by deptno) temp where d.deptno = temp.deptno(+);