SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
SELECT *
FROM emp
WHERE sal > (SELECT sal FROM emp WHERE ename = 'JONES');
SELECT ename
FROM emp
WHERE sal = (SELECT MIN (sal) FROM emp);
SELECT ename, job
FROM emp
WHERE job =
(SELECT job
FROM emp
WHERE empno = 7369)
AND sal >
(SELECT sal
FROM emp
WHERE empno = 7876);
SELECT ename, job, sal
FROM emp
WHERE sal =
(SELECT MIN(sal)
FROM emp);
SELECT deptno, MIN(sal)
FROM emp
GROUP BY deptno
HAVING MIN(sal) >
(SELECT MIN(sal)
FROM emp
WHERE deptno = 20);
SELECT deptno, COUNT(empno)
FROM emp
GROUP BY deptno
HAVING COUNT(empno) >
(SELECT AVG(COUNT(empno))
FROM emp
GROUP BY deptno);
SELECT ename, hiredate
FROM emp
WHERE hiredate = (SELECT MIN (hiredate) FROM emp);
SELECT ename, sal, dname
FROM emp, dept
WHERE emp.deptno = dept.deptno(+)
AND loc = 'CHICAGO'
AND sal > (SELECT sal FROM emp WHERE ename = 'SMITH');
SELECT ename, hiredate
FROM emp
WHERE hiredate < (SELECT MIN (hiredate) FROM emp WHERE deptno = 20);
SELECT d.deptno, dname, COUNT (d.deptno)
FROM emp e, dept d
WHERE e.deptno = d.deptno(+)
GROUP BY d.deptno, dname
HAVING COUNT (d.deptno) > (SELECT AVG (COUNT (deptno)) FROM emp GROUP BY deptno);
SELECT empno, ename, sal
FROM emp
WHERE sal IN -- 不能用 =
(SELECT MIN(sal)
FROM emp
GROUP BY deptno);
SELECT ename, sal
FROM emp
WHERE empno IN (SELECT DISTINCT mgr
FROM emp);
SELECT ename, sal
FROM emp
WHERE empno = ANY (SELECT mgr
FROM emp);
SELECT empno, ename, job, sal
FROM emp
WHERE sal > ANY (SELECT sal
FROM emp
WHERE deptno = 10)
AND deptno <> 10;
SELECT empno, ename,job, sal
FROM emp
WHERE sal > ALL (SELECT sal
FROM emp
WHERE deptno= 10)
AND deptno <> 10;
SELECT empno, ename,job, sal
FROM emp
WHERE sal < ALL (SELECT sal
FROM emp
WHERE deptno= 10)
AND deptno <> 10;
SELECT empno, ename,job, sal
FROM emp
WHERE sal = ALL (SELECT sal
FROM emp
WHERE deptno= 10)
AND deptno <> 10;
SELECT ename, hiredate
FROM emp
WHERE hiredate > ANY (SELECT hiredate FROM emp WHERE deptno = 10)
AND deptno != 10;
SELECT ename, hiredate
FROM emp
WHERE hiredate > ALL (SELECT hiredate FROM emp WHERE deptno = 10)
AND deptno != 10;
SELECT ename, job
FROM emp
WHERE job = ANY (SELECT job FROM emp WHERE deptno = 10)
AND deptno != 10;
SELECT ename, deptno, job, hiredate
FROM emp
WHERE (deptno, job) IN
(SELECT deptno,job -- 与上面一一对应
FROM emp
WHERE to_char(hiredate,'YYYY')='1981')
AND to_char(hiredate,'YYYY')<>'1981';
SELECT ename, deptno, job, hiredate
FROM emp
WHERE (deptno IN (SELECT deptno
FROM emp
WHERE to_char(hiredate,'YYYY')='1981')
OR job IN (SELECT job
FROM emp
WHERE to_char(hiredate,'YYYY')='1981'))
AND to_char(hiredate,'YYYY')<>'1981';
SELECT ename, job
FROM emp
WHERE (mgr, job) IN
(SELECT mgr,job
FROM emp
WHERE deptno = 10 AND mgr IS NOT NULL)
AND deptno != 10;
SELECT ename, job
FROM emp
WHERE (mgr IN
(SELECT mgr
FROM emp
WHERE deptno = 10 AND mgr IS NOT NULL)
OR job IN
(SELECT job
FROM emp
WHERE deptno = 10))
AND deptno != 10;
SELECT ename
FROM emp
WHERE empno NOT IN
(SELECT mgr
FROM emp
WHERE mgr IS NOT NULL);
SELECT a.ename, a.sal, a.deptno, b.salavg
FROM emp a, (SELECT deptno, avg(sal) salavg
FROM emp
GROUP BY deptno) b
WHERE a.deptno = b.deptno
AND a.sal > b.salavg;
SELECT ename, e.job, dname, salavg
FROM emp e, dept d, (SELECT job, AVG (sal) salavg FROM emp GROUP BY job) j
WHERE e.deptno = d.deptno(+)
AND e.job = j.job
AND e.sal > j.salavg;
SELECT ename, job
FROM emp
WHERE (job, mgr) IN (SELECT job, mgr FROM emp WHERE ename IN ('SCOTT', 'BLAKE'))
AND ename NOT IN ('SCOTT', 'BLAKE');
SELECT ename
FROM emp
WHERE empno NOT IN (SELECT DISTINCT mgr FROM emp WHERE mgr IS NOT NULL);
SELECT rownum,empno, ename, job
FROM emp
ORDER BY empno;
SELECT [列名], ROWNUM
FROM (SELECT [列名]
FROM 表名
ORDER BY Top-N操作的列 ASC|DESC)
WHERE ROWNUM <= N;
SELECT ename, hiredate
FROM emp
WHERE ROWNUM <= 5
ORDER BY hiredate;
SELECT ename, hiredate
FROM emp, dept
WHERE emp.deptno = dept.deptno(+)
AND loc = 'CHICAGO'
AND ROWNUM <= 2
ORDER BY hiredate;
SELECT empno, ename, sal
FROM emp
WHERE sal > (SELECT sal FROM emp WHERE empno = 7782)
AND job = (SELECT job FROM emp WHERE empno = 7369);
SELECT ename, sal
FROM emp
WHERE sal = (SELECT MAX (sal) FROM emp);
SELECT emp.deptno, dname, MIN (sal)
FROM emp, dept
WHERE emp.deptno = dept.deptno(+)
HAVING MIN (sal) > (SELECT MIN (sal) FROM emp WHERE deptno = 10)
GROUP BY emp.deptno, dname;
SELECT empno, ename, sal
FROM emp
WHERE (deptno, sal) IN (SELECT deptno, MIN (sal) FROM emp GROUP BY deptno);
SELECT ename, sal
FROM emp
WHERE mgr = (SELECT empno FROM emp WHERE ename = 'KING');
SELECT ename, sal, hiredate
FROM emp
WHERE hiredate > (SELECT hiredate FROM emp WHERE ename = 'SMITH');
SELECT ename
FROM emp
WHERE deptno IN (SELECT deptno FROM dept WHERE loc = 'NEW YORK');
SELECT ename, hiredate
FROM emp
WHERE deptno = (SELECT deptno FROM emp WHERE ename = 'SMITH')
AND ename != 'SMITH';
SELECT empno, ename
FROM emp
WHERE sal > (SELECT AVG (sal) FROM emp);
SELECT ename, sal
FROM emp
WHERE mgr = (SELECT empno FROM emp WHERE ename = 'KING');
SELECT ename, job
FROM emp
WHERE deptno = (SELECT deptno FROM dept WHERE dname = 'RESEARCH');
SELECT deptno, AVG (sal)
FROM emp
HAVING AVG (sal) > (SELECT AVG (sal) FROM emp WHERE deptno = 20)
GROUP BY deptno;
SELECT ename, sal, ROUND (da.salavg) 部门平均工资, ROUND (sal - da.salavg) 差额
FROM emp, (SELECT deptno, AVG (sal) salavg FROM emp GROUP BY deptno) da
WHERE emp.deptno = da.deptno(+)
AND emp.sal > da.salavg;
SELECT deptno
FROM emp
GROUP BY deptno
HAVING COUNT (empno) > 0;
SELECT *
FROM emp
WHERE sal > (SELECT sal FROM emp WHERE ename = 'SMITH');
SELECT w.ename, w.hiredate, w.mgr, m.empno, m.ename, m.hiredate
FROM emp w, emp m
WHERE w.mgr = m.empno(+)
AND w.hiredate < m.hiredate;
SELECT w.ename, w.mgr, m.empno, m.ename
FROM emp w, emp m
WHERE w.mgr = m.empno(+);
SELECT dname, COUNT (empno)
FROM emp e, dept d
WHERE e.deptno = d.deptno(+)
GROUP BY dname;
SELECT *
FROM emp
WHERE (deptno, sal) IN (SELECT deptno, MAX (sal) FROM emp GROUP BY deptno);
SELECT ename, sal
FROM emp
WHERE deptno = (SELECT deptno FROM emp WHERE empno = 7369);
SELECT ename
FROM emp
WHERE deptno IN (SELECT deptno FROM emp WHERE ename LIKE '%W%');
SELECT ename, sal
FROM emp
WHERE sal > (SELECT AVG (sal) FROM emp);
SELECT ename, sal, salavg
FROM emp e, (SELECT deptno, AVG (sal) salavg FROM emp GROUP BY deptno) ds
WHERE e.deptno = ds.deptno(+)
AND sal > salavg;
SELECT sal, ename
FROM emp
WHERE (mgr, sal) IN (SELECT mgr, MIN (sal) FROM emp GROUP BY mgr);
SELECT ename, hiredate
FROM emp
WHERE hiredate >
(SELECT hiredate FROM emp WHERE sal = (SELECT MAX (sal) FROM emp));
SELECT AVG (sal), dname
FROM emp e, dept d
WHERE e.deptno = d.deptno(+)
GROUP BY dname
HAVING AVG (sal) = (SELECT MAX (AVG (sal)) FROM emp GROUP BY deptno);