《卸甲笔记》-子查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《卸甲笔记》-子查询相关的知识,希望对你有一定的参考价值。

1查询公司中工资最低的雇员的完整信息

Oracle

SQL> select *

  2  from emp

  3  where sal=(

  4  select MIN(sal) from emp);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7369 SMITH      CLERK         7902 17-DEC-80                   800                   20

PPAS

scott=# select *

scott-# from emp

scott-# where sal=(

scott(# select MIN(sal) from emp);

 empno | ename |  job  | mgr  |      hiredate      |  sal   | comm | deptno

-------+-------+-------+------+--------------------+--------+------+--------

  7369 | SMITH | CLERK | 7902 | 17-DEC-80 00:00:00 | 800.00 |      |     20

(1 row)

2子查询返回的结果就当它是一个数字,即直接判断此数字

Oracle

SQL> select *

  2   from emp

  3  where sal=800;

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7369 SMITH      CLERK         7902 17-DEC-80                   800                   20

PPAS

scott=# select * 

scott-# from emp

scott-# where sal=800;

 empno | ename |  job  | mgr  |      hiredate      |  sal   | comm | deptno

-------+-------+-------+------+--------------------+--------+------+--------

  7369 | SMITH | CLERK | 7902 | 17-DEC-80 00:00:00 | 800.00 |      |     20

(1 row)

在WHERE子句中使用子查询

子查询返回单行单列数据

3查询出基本工资比ALLEN低的全部雇员信息

Oracle

SQL> select *

  2  from emp

  3   where sal<(

  4  select sal

  5  from emp

  6  where ename=‘ALLEN‘);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7369 SMITH      CLERK         7902 17-DEC-80                   800                   20

      7521 WARD       SALESMAN        7698 22-FEB-81        1250     500            30

      7654 MARTIN     SALESMAN         7698 28-SEP-81        1250    1400            30

      7844 TURNER     SALESMAN         7698 08-SEP-81        1500       0            30

      7876 ADAMS      CLERK        7788 23-MAY-87                1100                   20

      7900 JAMES      CLERK         7698 03-DEC-81                   950                   30

      7934 MILLER     CLERK          7782 23-JAN-82        1300                   10

 

7 rows selected.

PPAS

scott=# select *

scott-# from emp

scott-# where sal<(

scott(# select sal

scott(# from emp

scott(# where ename=‘ALLEN‘);

 empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+----------+------+--------------------+---------+---------+--------

  7369 | SMITH  | CLERK    | 7902 | 17-DEC-80 00:00:00 |  800.00 |         |     20

  7521 | WARD   | SALESMAN | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

  7654 | MARTIN | SALESMAN | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

  7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

  7876 | ADAMS  | CLERK    | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

  7900 | JAMES  | CLERK    | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

  7934 | MILLER | CLERK    | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

(7 rows)

4查询基本工资高于公司平均薪金的全部雇员信息

Oracle

SQL> select *

  2  from emp

  3   where sal>(

  4  select AVG(sal)

  5  from emp);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7566 JONES      MANAGER           7839 02-APR-81        2975                   20

      7698 BLAKE      MANAGER           7839 01-MAY-81                2850                   30

      7782 CLARK      MANAGER           7839 09-JUN-81        2450                   10

      7788 SCOTT      ANALYST              7566 19-APR-87        3000                   20

      7839 KING       PRESIDENT     17-NOV-81              5000                   10

      7902 FORD       ANALYST              7566 03-DEC-81                3000                   20

 

6 rows selected.

PPAS

scott=# select *

scott-# from emp

scott-# where sal>(

scott(# select AVG(sal)

scott(# from emp);

 empno | ename |    job    | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+-----------+------+--------------------+---------+------+--------

  7566 | JONES | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |      |     20

  7698 | BLAKE | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |      |     30

  7782 | CLARK | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |      |     10

  7788 | SCOTT | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

  7839 | KING  | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |      |     10

  7902 | FORD  | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |      |     20

(6 rows)

5查找出与ALLEN从事同一工作,并且基本工资傲宇雇员编号为7521的全部雇员信息

Oracle

SQL> select *

  2  from emp

  3  where job=(

  4  select job

  5  from emp

  6  where ename=‘ALLEN‘)

  7  AND

  8  sal>(

  9  select sal

 10  from emp

 11  where empno=7521);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7499 ALLEN      SALESMAN           7698 20-FEB-81        1600     300            30

      7844 TURNER     SALESMAN         7698 08-SEP-81        1500       0            30

PPAS

scott=# select *

scott-# from emp

scott-# where job=(

scott(# select job

scott(# from emp

scott(# where ename=‘ALLEN‘)

scott-# AND

scott-# sal>(

scott(# select sal

scott(# from emp

scott(# where empno=7521);

 empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm  | deptno

-------+--------+----------+------+--------------------+---------+--------+--------

  7499 | ALLEN  | SALESMAN | 7698 | 20-FEB-81 00:00:00 | 1600.00 | 300.00 |     30

  7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |   0.00 |     30

(2 rows)

子查询返回单行多列数据

6查询与SCOTT从事同一工作且工资相同的雇员信息

Oracle

SQL> select *

  2  from emp

  3  where(job,sal)=(

  4  select job,sal

  5  from emp

  6  where ename=‘SCOTT‘)AND ename <>‘SCOTT‘;

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7902 FORD       ANALYST              7566 03-DEC-81                3000                   20

PPAS

scott=# select *     

scott-# from emp

scott-# where(job,sal)=(

scott(# select job,sal

scott(# from emp

scott(# where ename=‘SCOTT‘)AND ename <>‘SCOTT‘;

 empno | ename |   job   | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+---------+------+--------------------+---------+------+--------

  7902 | FORD  | ANALYST | 7566 | 03-DEC-81 00:00:00 | 3000.00 |      |     20

(1 row)

7查询与雇员7566从事同一工作且领导相同的全部雇员信息

Oracle

SQL> select * from emp

  2  where(job,mgr)=(

  3  select job,mgr

  4  from emp

  5  where empno=7566)

  6  and empno<>7566;

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7698 BLAKE      MANAGER           7839 01-MAY-81                2850                   30

      7782 CLARK      MANAGER           7839 09-JUN-81        2450                   10

PPAS

scott=# select * from emp

scott-# where(job,mgr)=(

scott(# select job,mgr

scott(# from emp

scott(# where empno=7566)

scott-# and empno<>7566;

 empno | ename |   job   | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+---------+------+--------------------+---------+------+--------

  7698 | BLAKE | MANAGER | 7839 | 01-MAY-81 00:00:00 | 2850.00 |      |     30

  7782 | CLARK | MANAGER | 7839 | 09-JUN-81 00:00:00 | 2450.00 |      |     10

(2 rows)

8查询与ALLEN从事同一工作且在同一年雇佣的全部雇员信息(包括ALLEN)

Oracle

SQL> select *

  2  from emp

  3  where(job,TO_CHAR(hiredate,‘yyyy‘))=(

  4  select job,TO_CHAR(hiredate,‘yyyy‘)

  5  from emp

  6  where ename=‘ALLEN‘);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7499 ALLEN      SALESMAN           7698 20-FEB-81        1600     300            30

      7521 WARD       SALESMAN        7698 22-FEB-81        1250     500            30

      7654 MARTIN     SALESMAN         7698 28-SEP-81        1250    1400            30

      7844 TURNER     SALESMAN         7698 08-SEP-81        1500       0            30

PPAS

scott=# select *

scott-# from emp

scott-# where(job,TO_CHAR(hiredate,‘yyyy‘))=(

scott(# select job,TO_CHAR(hiredate,‘yyyy‘)

scott(# from emp

scott(# where ename=‘ALLEN‘);

 empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+----------+------+--------------------+---------+---------+--------

  7499 | ALLEN  | SALESMAN | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

  7521 | WARD   | SALESMAN | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

  7654 | MARTIN | SALESMAN | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

  7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

(4 rows)

子查询返回多行单列数据

IN操作符

9查询出与每个部门中最低工资相同的全部雇员信息

Oracle

SQL> select *

  2  from emp

  3  where sal IN(

  4  select MIN(sal)

  5  from emp

  6  GROUP BY deptno);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7369 SMITH      CLERK         7902 17-DEC-80                   800                   20

      7900 JAMES      CLERK         7698 03-DEC-81                   950                   30

      7934 MILLER     CLERK          7782 23-JAN-82        1300                   10

PPAS

scott=# select *

scott-# from emp

scott-# where sal IN(

scott(# select MIN(sal)

scott(# from emp

scott(# GROUP BY deptno);

 empno | ename  |  job  | mgr  |      hiredate      |   sal   | comm | deptno

-------+--------+-------+------+--------------------+---------+------+--------

  7369 | SMITH  | CLERK | 7902 | 17-DEC-80 00:00:00 |  800.00 |      |     20

  7900 | JAMES  | CLERK | 7698 | 03-DEC-81 00:00:00 |  950.00 |      |     30

  7934 | MILLER | CLERK | 7782 | 23-JAN-82 00:00:00 | 1300.00 |      |     10

(3 rows)

10查询出不与每个部门中最低工资相同的全部雇员信息

Oracle

SQL> select *

  2  from emp

  3  where sal NOT IN(

  4  select MIN(sal)

  5  from emp

  6  GROUP BY deptno);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7902 FORD       ANALYST              7566 03-DEC-81                3000                   20

      7788 SCOTT      ANALYST              7566 19-APR-87        3000                   20

      7698 BLAKE      MANAGER           7839 01-MAY-81                2850                   30

      7782 CLARK      MANAGER           7839 09-JUN-81        2450                   10

      7839 KING       PRESIDENT     17-NOV-81              5000                   10

      7654 MARTIN     SALESMAN         7698 28-SEP-81        1250    1400            30

      7521 WARD       SALESMAN        7698 22-FEB-81        1250     500            30

      7499 ALLEN      SALESMAN           7698 20-FEB-81        1600     300            30

      7876 ADAMS      CLERK        7788 23-MAY-87                1100                   20

      7566 JONES      MANAGER           7839 02-APR-81        2975                   20

      7844 TURNER     SALESMAN         7698 08-SEP-81        1500       0            30

 

11 rows selected.

PPAS

scott=# select *

scott-# from emp

scott-# where sal NOT IN(

scott(# select MIN(sal)

scott(# from emp

scott(# GROUP BY deptno);

 empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

  7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

  7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

  7566 | JONES  | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |         |     20

  7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

  7698 | BLAKE  | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

  7782 | CLARK  | MANAGER   | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

  7788 | SCOTT  | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |         |     20

  7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

  7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

  7876 | ADAMS  | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

  7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

(11 rows)

如果使用IN操作符在指定的查询范围中存在一个null,则将不会有任何的查询结果返回

11观察null对NOT NI操作的影响

Oracle

SQL> select e.ename

  2  from emp e

  3  where e.empno NOT IN(

  4  select m.mgr

  5  from emp m);

 

no rows selected

PPAS

scott=# select e.ename

scott-# from emp e

scott-# where e.empno NOT IN(

scott(# select m.mgr

scott(# from emp m);

 ename

-------

(0 rows)

ANY操作符

12列出每个部门经理的工资

Oracle

SQL>  select MIN(sal)

  2  from emp

  3  where job=‘MANAGER‘

  4  GROUP BY deptno;

 

  MIN(SAL)

----------

      2850

      2975

      2450

PPAS

scott=# select MIN(sal)

scott-# from emp

scott-# where job=‘MANAGER‘

scott-# GROUP BY deptno;

   min  

---------

 2975.00

 2850.00

 2450.00

(3 rows)

13使用=ANY操作符完成查询

Oracle

SQL> select * from emp

  2  where sal=ANY(

  3  select MIN(sal)

  4  from emp

  5  where job=‘MANAGER‘

  6  GROUP BY deptno);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7566 JONES      MANAGER           7839 02-APR-81        2975                   20

      7698 BLAKE      MANAGER           7839 01-MAY-81                2850                   30

      7782 CLARK      MANAGER           7839 09-JUN-81        2450                   10

PPAS

scott=# select * from emp

scott-# where sal=ANY(

scott(# select MIN(sal)

scott(# from emp

scott(# where job=‘MANAGER‘

scott(# GROUP BY deptno);

 empno | ename |   job   | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+---------+------+--------------------+---------+------+--------

  7566 | JONES | MANAGER | 7839 | 02-APR-81 00:00:00 | 2975.00 |      |     20

  7698 | BLAKE | MANAGER | 7839 | 01-MAY-81 00:00:00 | 2850.00 |      |     30

  7782 | CLARK | MANAGER | 7839 | 09-JUN-81 00:00:00 | 2450.00 |      |     10

(3 rows)

14使用>ANY操作符完成查询

Oracle

SQL> select * from emp

  2  where sal>ANY(

  3  select MIN(sal)

  4  from emp

  5  where job=‘MANAGER‘

  6  GROUP BY deptno);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7839 KING       PRESIDENT     17-NOV-81              5000                   10

      7902 FORD       ANALYST              7566 03-DEC-81                3000                   20

      7788 SCOTT      ANALYST              7566 19-APR-87        3000                   20

      7566 JONES      MANAGER           7839 02-APR-81        2975                   20

      7698 BLAKE      MANAGER           7839 01-MAY-81                2850                   30

PPAS

scott=# select * from emp

scott-# where sal >ANY(

scott-# ^C

scott=# select * from emp

scott-# where sal>ANY(

scott(# select MIN(sal)

scott(# from emp

scott(# where job=‘MANAGER‘

scott(# GROUP BY deptno);

 empno | ename |    job    | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+-----------+------+--------------------+---------+------+--------

  7566 | JONES | MANAGER   | 7839 | 02-APR-81 00:00:00 | 2975.00 |      |     20

  7698 | BLAKE | MANAGER   | 7839 | 01-MAY-81 00:00:00 | 2850.00 |      |     30

  7788 | SCOTT | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

  7839 | KING  | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |      |     10

  7902 | FORD  | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |      |     20

(5 rows)

15使用<ANY操作符完成查询

Oracle

SQL> select * from emp

  2  where sal<ANY(

  3  select MIN(sal)

  4  from emp

  5  where job=‘MANAGER‘

  6  GROUP BY deptno);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7369 SMITH      CLERK         7902 17-DEC-80                   800                   20

      7900 JAMES      CLERK         7698 03-DEC-81                   950                   30

      7876 ADAMS      CLERK        7788 23-MAY-87                1100                   20

      7521 WARD       SALESMAN        7698 22-FEB-81        1250     500            30

      7654 MARTIN     SALESMAN         7698 28-SEP-81        1250    1400            30

      7934 MILLER     CLERK          7782 23-JAN-82        1300                   10

      7844 TURNER     SALESMAN         7698 08-SEP-81        1500       0            30

      7499 ALLEN      SALESMAN           7698 20-FEB-81        1600     300            30

      7782 CLARK      MANAGER           7839 09-JUN-81        2450                   10

      7698 BLAKE      MANAGER           7839 01-MAY-81                2850                   30

 

10 rows selected.

PPAS

scott=# select * from emp

scott-# where sal<ANY(

scott(# select MIN(sal)

scott(# from emp

scott(# where job=‘MANAGER‘

scott(# GROUP BY deptno);

 empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+----------+------+--------------------+---------+---------+--------

  7369 | SMITH  | CLERK    | 7902 | 17-DEC-80 00:00:00 |  800.00 |         |     20

  7499 | ALLEN  | SALESMAN | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

  7521 | WARD   | SALESMAN | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

  7654 | MARTIN | SALESMAN | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

  7698 | BLAKE  | MANAGER  | 7839 | 01-MAY-81 00:00:00 | 2850.00 |         |     30

  7782 | CLARK  | MANAGER  | 7839 | 09-JUN-81 00:00:00 | 2450.00 |         |     10

  7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

  7876 | ADAMS  | CLERK    | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

  7900 | JAMES  | CLERK    | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

  7934 | MILLER | CLERK    | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

(10 rows)

ALL操作符

16使用<>ALL操作符完成查询

Oracle

SQL> select * from emp

  2  where sal<>ALL(

  3  select MIN(sal)

  4  from emp

  5  where job=‘MANAGER‘

  6  GROUP BY deptno);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7369 SMITH      CLERK         7902 17-DEC-80                   800                   20

      7902 FORD       ANALYST              7566 03-DEC-81                3000                   20

      7788 SCOTT      ANALYST              7566 19-APR-87        3000                   20

      7839 KING       PRESIDENT     17-NOV-81              5000                   10

      7654 MARTIN     SALESMAN         7698 28-SEP-81        1250    1400            30

      7521 WARD       SALESMAN        7698 22-FEB-81        1250     500            30

      7499 ALLEN      SALESMAN           7698 20-FEB-81        1600     300            30

      7876 ADAMS      CLERK        7788 23-MAY-87                1100                   20

      7900 JAMES      CLERK         7698 03-DEC-81                   950                   30

      7934 MILLER     CLERK          7782 23-JAN-82       1300                   10

      7844 TURNER     SALESMAN         7698 08-SEP-81        1500       0            30

 

11 rows selected.

PPAS

scott=# select * from emp

scott-# where sal<>ALL(

scott(# select MIN(sal)

scott(# from emp

scott(# where job=‘MANAGER‘

scott(# GROUP BY deptno);

 empno | ename  |    job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+-----------+------+--------------------+---------+---------+--------

  7369 | SMITH  | CLERK     | 7902 | 17-DEC-80 00:00:00 |  800.00 |         |     20

  7499 | ALLEN  | SALESMAN  | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

  7521 | WARD   | SALESMAN  | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

  7654 | MARTIN | SALESMAN  | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

  7788 | SCOTT  | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |         |     20

  7839 | KING   | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |         |     10

  7844 | TURNER | SALESMAN  | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

  7876 | ADAMS  | CLERK     | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

  7900 | JAMES  | CLERK     | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

  7902 | FORD   | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |         |     20

  7934 | MILLER | CLERK     | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

(11 rows)

17使用>ALL操作符完成查询

Oracle

SQL> select * from emp

  2  where sal>ALL(

  3  select MIN(sal)

  4  from emp

  5  where job=‘MANAGER‘

  6  GROUP BY deptno);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7788 SCOTT      ANALYST              7566 19-APR-87        3000                   20

      7902 FORD       ANALYST              7566 03-DEC-81                3000                   20

      7839 KING       PRESIDENT     17-NOV-81              5000                   10

PPAS

scott=# select * from emp

scott-# where sal>ALL(

scott(# select MIN(sal)

scott(# from emp

scott(# where job=‘MANAGER‘

scott(# GROUP BY deptno);

 empno | ename |    job    | mgr  |      hiredate      |   sal   | comm | deptno

-------+-------+-----------+------+--------------------+---------+------+--------

  7788 | SCOTT | ANALYST   | 7566 | 19-APR-87 00:00:00 | 3000.00 |      |     20

  7839 | KING  | PRESIDENT |      | 17-NOV-81 00:00:00 | 5000.00 |      |     10

  7902 | FORD  | ANALYST   | 7566 | 03-DEC-81 00:00:00 | 3000.00 |      |     20

(3 rows)

18使用<ALL操作符完成查询

Oracle

SQL> select * from emp

  2  where sal<ALL(

  3  select MIN(sal)

  4  from emp

  5  where job=‘MANAGER‘

  6  GROUP BY deptno);

 

     EMPNO ENAME      JOB            MGR HIREDATE                 SAL     COMM    DEPTNO

---------- ---------- ---------- ---------- ------------ ---------- ---------- ----------

      7499 ALLEN      SALESMAN           7698 20-FEB-81        1600     300            30

      7844 TURNER     SALESMAN         7698 08-SEP-81        1500       0            30

      7934 MILLER     CLERK          7782 23-JAN-82        1300                   10

      7521 WARD       SALESMAN        7698 22-FEB-81        1250     500            30

      7654 MARTIN     SALESMAN         7698 28-SEP-81        1250    1400            30

      7876 ADAMS      CLERK        7788 23-MAY-87                1100                   20

      7900 JAMES      CLERK         7698 03-DEC-81                   950                   30

      7369 SMITH      CLERK         7902 17-DEC-80                   800                   20

 

8 rows selected.

PPAS

scott=# select * from emp

scott-# where sal<ALL(

scott(# select MIN(sal)

scott(# from emp

scott(# where job=‘MANAGER‘

scott(# GROUP BY deptno);

 empno | ename  |   job    | mgr  |      hiredate      |   sal   |  comm   | deptno

-------+--------+----------+------+--------------------+---------+---------+--------

  7369 | SMITH  | CLERK    | 7902 | 17-DEC-80 00:00:00 |  800.00 |         |     20

  7499 | ALLEN  | SALESMAN | 7698 | 20-FEB-81 00:00:00 | 1600.00 |  300.00 |     30

  7521 | WARD   | SALESMAN | 7698 | 22-FEB-81 00:00:00 | 1250.00 |  500.00 |     30

  7654 | MARTIN | SALESMAN | 7698 | 28-SEP-81 00:00:00 | 1250.00 | 1400.00 |     30

  7844 | TURNER | SALESMAN | 7698 | 08-SEP-81 00:00:00 | 1500.00 |    0.00 |     30

  7876 | ADAMS  | CLERK    | 7788 | 23-MAY-87 00:00:00 | 1100.00 |         |     20

  7900 | JAMES  | CLERK    | 7698 | 03-DEC-81 00:00:00 |  950.00 |         |     30

  7934 | MILLER | CLERK    | 7782 | 23-JAN-82 00:00:00 | 1300.00 |         |     10

(8 rows)

空数据判断

19验证EXISTS结构

Oracle

SQL> select * from emp

  2  where exists(

  3  select * from emp where empno=9999);

 

no rows selected

PPAS

scott=# select * from emp

scott-# where exists(

scott(# select * from emp where empno=9999);

 empno | ename | job | mgr | hiredate | sal | comm | deptno

-------+-------+-----+-----+----------+-----+------+--------

以上是关于《卸甲笔记》-子查询的主要内容,如果未能解决你的问题,请参考以下文章

《卸甲笔记》-分组统计查询

《卸甲笔记》-限定查询与排序显示

《卸甲笔记》-多表查询之二

MySQL学习笔记-子查询和连接

MYSQL初级学习笔记六:子查询!(视频序号:初级_37-41)

12W学习笔记——独立子查询,更新,删除,建立视图