标签:
SQL> -- 查询工资比scott用户高的所有的用户信息
SQL> select sal f
2 ;
*
第 2 行出现错误:
ORA-00923: 未找到要求的 FROM 关键字
SQL> select sal
2 from emp
3 where ename=‘SCOTT‘;
SAL
----------
3000
SQL> select * from
2 emp
3 where sal > 3000;
EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7839 KING PRESIDENT 17-11月-81
5000 10
SQL> set linesize 200;
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
SQL> select * from
2 emp where sal > (
3 select sal from emp where ename = ‘SCOTT‘
4 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
SQL> /*
SQL> 子查询解决什么问题: 一个查询查询不到结果的时候,可以使用子查询来丰富查询的条件
SQL> 子查询的格式: 用一个小括号包含,然后在里面写sql语句
SQL> 子查询的注意事项:
SQL> 1. 一定要有小括号
SQL> 2. 一定要注意你的书写格式
SQL> 3. 子查询可以放在select,from ,where having,order by 后面
SQL> 4. 子查询一定不能放在group by的后面
SQL> 5. 子查询和主查询可以不是同一张表,只要子查询的结果在主查询中能用就可以
SQL> 5. 子查询中一般都不排序,但是在TOP-N中需要排序
SQL> 7. 一般是先执行子查询操作,在执行主查询操作,但是在相关子查询中先执行主查询,在执行子查询
SQL> 8. 对于子查询的结果,如果是单行只能用单行操作符,如果是多行,只能用多行操作符
SQL> 9. 自查中的null处理
SQL> *、
SQL> */
SQL> -- 子查询放在select后面
SQL> select ename,job,(select sal from emp where empno=7566) 子查询 from emp;
ENAME JOB 子查询
-------------------- ------------------ ----------
SMITH CLERK 2975
ALLEN SALESMAN 2975
WARD SALESMAN 2975
JONES MANAGER 2975
MARTIN SALESMAN 2975
BLAKE MANAGER 2975
CLARK MANAGER 2975
SCOTT ANALYST 2975
KING PRESIDENT 2975
TURNER SALESMAN 2975
ADAMS CLERK 2975
ENAME JOB 子查询
-------------------- ------------------ ----------
JAMES CLERK 2975
FORD ANALYST 2975
MILLER CLERK 2975
已选择14行。
SQL> select ename,job,(select sal from emp) 子查询 from emp;
select ename,job,(select sal from emp) 子查询 from emp
*
第 1 行出现错误:
ORA-01427: 单行子查询返回多个行
SQL> -- 放在select后面的时候结果只能是单行的
SQL> desc emp;
名称 是否为空? 类型
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL> desc dept;
名称 是否为空? 类型
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
SQL> -- from后面放置子查询 重点要掌握的
SQL> -- 查询员工的姓名 工资 job
SQL> select ename,sal,job from emp;
ENAME SAL JOB
-------------------- ---------- ------------------
SMITH 800 CLERK
ALLEN 1600 SALESMAN
WARD 1250 SALESMAN
JONES 2975 MANAGER
MARTIN 1250 SALESMAN
BLAKE 2850 MANAGER
CLARK 2450 MANAGER
SCOTT 3000 ANALYST
KING 5000 PRESIDENT
TURNER 1500 SALESMAN
ADAMS 1100 CLERK
ENAME SAL JOB
-------------------- ---------- ------------------
JAMES 950 CLERK
FORD 3000 ANALYST
MILLER 1300 CLERK
已选择14行。
SQL> select * from (
2 select ename,job,sal from emp
3 );
ENAME JOB SAL
-------------------- ------------------ ----------
SMITH CLERK 800
ALLEN SALESMAN 1600
WARD SALESMAN 1250
JONES MANAGER 2975
MARTIN SALESMAN 1250
BLAKE MANAGER 2850
CLARK MANAGER 2450
SCOTT ANALYST 3000
KING PRESIDENT 5000
TURNER SALESMAN 1500
ADAMS CLERK 1100
ENAME JOB SAL
-------------------- ------------------ ----------
JAMES CLERK 950
FORD ANALYST 3000
MILLER CLERK 1300
已选择14行。
SQL> -- where 后面跟子查询
SQL> select * from
2 emp where sal > (
3 select sal from emp where ename = (
4 select ename from emp where empno = 7566;
select sal from emp where ename = (
*
第 3 行出现错误:
ORA-00921: 意外的 SQL 命令结尾
SQL> )
SP2-0042: 未知命令 ")" - 其余行忽略。
SQL> ); select * from
SP2-0734: 未知的命令开头 "); select ..." - 忽略了剩余的行。
SQL> emp where sal > (
SP2-0734: 未知的命令开头 "emp where ..." - 忽略了剩余的行。
SQL> select sal from emp where ename = (
2 select ename from emp where empno = 7566
3 )
4 );
)
*
第 4 行出现错误:
ORA-00933: SQL 命令未正确结束
SQL> select * from
2 emp where sal > (
3 select sal from emp where ename = (
4 select ename from emp where empno = 7566
5 )
6 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
SQL> -- 一定要注意的地方: 子查询不能过多的去嵌套,一般嵌套三层,加多了就影响性能
SQL> -- order by 后面跟子查询
SQL> -- 一般在order
SQL> -- 一般在Order by 后面不跟子查询
SQL> select * from emp order by (select count(*) from emp where ename=‘SCOTT‘);
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
7934 MILLER CLERK 7782 23-1月 -82 1300 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
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
已选择14行。
SQL> select * from emp order by (select empno from emp where ename=‘SCOTT‘);
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
7934 MILLER CLERK 7782 23-1月 -82 1300 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
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
已选择14行。
SQL> select * from emp order by (select empno from emp where ename=‘SCOTT‘) desc;
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
7934 MILLER CLERK 7782 23-1月 -82 1300 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
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
已选择14行。
SQL> select * from emp order by (select count(*) from emp where ename=‘SCOTT‘) desc;
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
7934 MILLER CLERK 7782 23-1月 -82 1300 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
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
已选择14行。
SQL> -- order by 后面不能跟子查询,语法上是可以跟的,但是跟了不起作用
SQL> -- group by 后面不能跟子查询
SQL> -- 5. 子查询和主查询可以不是同一张表,只要子查询的结果在主查询中能用就可以
SQL> -- 查询部门名称为SAL的所有的员工信息
SQL> desc dept;
名称 是否为空? 类型
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
SQL> desc emp;
名称 是否为空? 类型
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL> select *
2 from emp
3 where deptno = (
4 select deptno from dept where dname=‘SAL‘
5 );
未选定行
SQL> select * from dept;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> 4
SP2-0226: 行号 无效
SQL> select *
2 from emp
3 where deptno=(
4 select deptno from dept where dname=‘SALES‘
5 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7900 JAMES CLERK 7698 03-12月-81 950 30
已选择6行。
SQL> select rownum,empno,sal from emp;
ROWNUM EMPNO SAL
---------- ---------- ----------
1 7369 800
2 7499 1600
3 7521 1250
4 7566 2975
5 7654 1250
6 7698 2850
7 7782 2450
8 7788 3000
9 7839 5000
10 7844 1500
11 7876 1100
ROWNUM EMPNO SAL
---------- ---------- ----------
12 7900 950
13 7902 3000
14 7934 1300
已选择14行。
SQL> desc emp;
名称 是否为空? 类型
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL> select rownum,sal,empno from emp order by sal;
ROWNUM SAL EMPNO
---------- ---------- ----------
1 800 7369
12 950 7900
11 1100 7876
3 1250 7521
5 1250 7654
14 1300 7934
10 1500 7844
2 1600 7499
7 2450 7782
6 2850 7698
4 2975 7566
ROWNUM SAL EMPNO
---------- ---------- ----------
8 3000 7788
13 3000 7902
9 5000 7839
已选择14行。
SQL> -- 行号: 一连串,不间断的数列
SQL> --8. 对于子查询的结果,如果是单行只能用单行操作符,如果是多行,只能用多行操作符
SQL> -- 单行操作符
SQL> -- 单行操作符: ><= <= !=
SQL> -- 查询部门号不等于30的所有员工信息
SQL> select * from
2 emp where deptno != 30;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
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
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择8行。
SQL> select *
2 from emp
3 where deptno != (
4 select deptno from emp where deptno = 30
5 );
select deptno from emp where deptno = 30
*
第 4 行出现错误:
ORA-01427: 单行子查询返回多个行
SQL> select deptno from emp;
DEPTNO
----------
20
30
30
20
30
30
10
20
10
30
20
DEPTNO
----------
30
20
10
已选择14行。
SQL> select empno,ename from emp where deptno=30;
EMPNO ENAME
---------- --------------------
7499 ALLEN
7521 WARD
7654 MARTIN
7698 BLAKE
7844 TURNER
7900 JAMES
已选择6行。
SQL> -- 查询部门号不为30的员工信息
SQL> select * from (select * from emp where deptno != 30);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
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
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择8行。
SQL> select * from (select * from emp where deptno >< 30);
select * from (select * from emp where deptno >< 30)
*
第 1 行出现错误:
ORA-00936: 缺失表达式
SQL> select * from (select * from emp where deptno <> 30);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
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
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择8行。
SQL> -- <> 用来表示不等于
SQL> -- 多行操作符 int not in any all
SQL> -- 查询和SCOTT用户和CLARK用户共同部门的所有员工
SQL> select * from emp where deptno in(
2 select deptno from emp where ename=‘SCOTT‘ or ename=‘CLARK‘
3 );
)
*
第 3 行出现错误:
ORA-00911: 无效字符
SQL> select * from emp where deptno in(
2 select deptno from emp where ename=‘SCOTT‘ or ename=‘CLARK‘
3 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7839 KING PRESIDENT 17-11月-81 5000 10
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7369 SMITH CLERK 7902 17-12月-80 800 20
已选择8行。
SQL> select * from emp where deptno not in(
2 select deptno from emp where ename=‘SCOTT‘ or ename=‘CLARK‘
3 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7900 JAMES CLERK 7698 03-12月-81 950 30
已选择6行。
SQL> -- 查询比30号部门员工工资高的员工信息
SQL>
SQL> select * from emp where sal > any(
2 select sal from emp where deptno = 30
3 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
已选择12行。
SQL>
SQL> select * from emp where sal > (
2 select min(sal) from emp where deptno = 30
3 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
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
7902 FORD ANALYST 7566 03-12月-81 3000 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择12行。
SQL> -- 查找比30号部门所有员工工资都高的员工信息
SQL> select * from emp where sal > (
2 select max(sal) from emp where deptno = 30
3 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
SQL> select * from emp where sal > all(
2 select sal from emp where deptno = 30
3 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
SQL> -- 9. 子查中的null处理
SQL> -- 查询所有部门领导的信息
SQL> desc emp;
名称 是否为空? 类型
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL> select * from
2 emp
3 where empno in(
4 select mgr from emp
5 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7902 FORD ANALYST 7566 03-12月-81 3000 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7839 KING PRESIDENT 17-11月-81 5000 10
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
已选择6行。
SQL> -- 查询所有不是领导的员工
SQL> select *
2 from emp
3 where empno not in(
4 select mgr from emp
5 );
未选定行
SQL> select mgr from emp where mgr is not null;
MGR
----------
7902
7698
7698
7839
7698
7839
7839
7566
7698
7788
7698
MGR
----------
7566
7782
已选择13行。
SQL> select mgr,ename from emp where mgr is not null;
MGR ENAME
---------- --------------------
7902 SMITH
7698 ALLEN
7698 WARD
7839 JONES
7698 MARTIN
7839 BLAKE
7839 CLARK
7566 SCOTT
7698 TURNER
7788 ADAMS
7698 JAMES
MGR ENAME
---------- --------------------
7566 FORD
7782 MILLER
已选择13行。
SQL> select *
2 from emp
3 where empno not in(
4 select mgr,ename from emp where mgr is not null
5 );
select mgr,ename from emp where mgr is not null
*
第 4 行出现错误:
ORA-00913: 值过多
SQL> select *
2 from emp
3 where empno not in(
4 select mgr from emp where mgr is not null
5 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7369 SMITH CLERK 7902 17-12月-80 800 20
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
已选择8行。
SQL> spool off;
标签:
原文地址:http://www.cnblogs.com/877599949yan/p/5290746.html