oracle exits用法

exists表示()內子查詢語句返回結果不爲空說明where條件成立就會執行主sql語句,若是爲空就表示where條件不成立,sql語句就不會執行。not exists和exists相反,子查詢語句結果爲空,則表示where條件成立,執行sql語句。負責不執行。sql

以前在學oracle數據庫的時候,接觸過exists,作過幾個簡單的例子,,如數據庫

1.若是部門名稱中含有字母A,則查詢全部員工信息(使用exists) select * from emp where exists (select * from dept where dname like '%A%' and deptno = emp.deptno) temp and deptno=temp.deptno;oracle

結果爲:code

EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM     DEPTNO

7369 SMITH      CLERK           7902 17-12月-80            800                    20
  7499 ALLEN      SALESMAN        7698 20-2月 -81           1600        300         30
  7521 WARD       SALESMAN        7698 22-2月 -81           1250        500         30
  7566 JONES      MANAGER         7839 02-4月 -81           2975                    20
  7654 MARTIN     SALESMAN        7698 28-9月 -81           1250       1400         30
  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
  7844 TURNER     SALESMAN        7698 08-9月 -81           1500          0         30
  7876 ADAMS      CLERK           7788 23-5月 -87           1100                    20

 EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM     DEPTNO

7900 JAMES      CLERK           7698 03-12月-81            950                    30
  7902 FORD       ANALYST         7566 03-12月-81           3000                    20
  7934 MILLER     CLERK           7782 23-1月 -82           1300                    10

已選擇14行。select

2.若是有平均工資不小於1500的部門信息則查詢全部部門信息(使用not exists) select * from dept where not exists (select deptno from emp where deptno = emp.deptno group by deptno having avg(sal) < 1500) and exists (select * from emp where emp.deptno = deptno);sql語句

相關文章
相關標籤/搜索