十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
SQL> select * from emp where job = (select job from emp where ename='FORD');
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
7951 EASON ANALYST 7566 01-12月-17 3000
20
7788 SCOTT ANALYST 7566 19-4月 -87 3000
20
7902 FORD ANALYST 7566 03-12月-81 3000
20
SQL> select deptno, dname, loc, (select count(empno) from emp where emp.deptno = dept.deptno) count from dept;
DEPTNO DNAME LOC COUNT
10 ACCOUNTING NEW YORK 3
20 RESEARCH DALLAS 6
30 SALES CHICAGO 6
40 OPERATIONS BOSTON 0
SQL> select deptno, avg(sal) from emp group by deptno having avg(sal) > (select max(sal) from emp where deptno = 30);
DEPTNO AVG(SAL)
10 2916.66667
SQL> select ename, job, sal from emp, (select deptno, avg(sal) avgsal from emp group by deptno) dept where emp.deptno = dept.deptno and sal > avgsal;
ENAME JOB SAL
EASON ANALYST 3000
ALLEN SALESMAN 1600
JONES MANAGER 2975
BLAKE MANAGER 2850
SCOTT ANALYST 3000
KING PRESIDENT 5000
FORD ANALYST 3000
已选择7行。
SQL> select * from emp where job = (select job from emp where empno = 7521) and sal > (select sal from emp where empno = 7934);
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300
30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0
30
SQL> select empno, ename, sal from emp where sal < (select avg(sal) from emp);
EMPNO ENAME SAL
7369 G_EASON 800
7499 ALLEN 1600
7521 WARD 1250
7654 MARTIN 1250
7844 TURNER 1500
7876 ADAMS 1100
7900 JAMES 950
7934 MILLER 1300
已选择8行。
SQL> select ename, sal, deptno from emp e where sal > (select avg(sal) from emp where deptno = e.deptno);
ENAME SAL DEPTNO
EASON 3000 20
ALLEN 1600 30
JONES 2975 20
BLAKE 2850 30
SCOTT 3000 20
KING 5000 10
FORD 3000 20
已选择7行。
SQL> select * from emp where deptno = (select deptno from dept where dname='ACCOUNTING');
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
7782 CLARK MANAGER 7839 09-6月 -81 2450
10
7839 KING PRESIDENT 17-11月-81 5000
10
7934 MILLER CLERK 7782 23-1月 -82 1300
10
操作符 | 含义 |
---|---|
= | 等于 |
> | 大于 |
>= | 大于等于 |
< | 小于 |
<= | 小于等于 |
<> | 不等于 |
SQL> select ename, sal, deptno from emp where deptno = (select deptno from emp where ename='JAMES') AND ename <> 'JAMES';
ENAME SAL DEPTNO-
ALLEN 1600 30
WARD 1250 30
MARTIN 1250 30
BLAKE 2850 30
SQL> select ename, job, sal from emp where sal >= (select avg(sal) from emp);
ENAME JOB SAL
EASON ANALYST 3000
JONES MANAGER 2975
BLAKE MANAGER 2850
CLARK MANAGER 2450
SCOTT ANALYST 3000
KING PRESIDENT 5000
FORD ANALYST 3000
已选择7行。
SQL> select * from emp where deptno <> (select deptno from dept where dname= 'SALES');
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
7951 EASON ANALYST 7566 01-12月-17 3000
20
......
7902 FORD ANALYST 7566 03-12月-81 3000
20
7934 MILLER CLERK 7782 23-1月 -82 1300
10
已选择9行。
SQL> select ename, job, sal from emp where sal = (select max(sal) from emp group by deptno);
select ename, job, sal from emp where sal = (select max(sal) from emp group by deptno)
第 1 行出现错误:
ORA-01427: 单行子查询返回多个行
SQL> select max(sal) from emp group by deptno;
MAX(SAL)
2850
3000
5000
运算符 | 含义 |
---|---|
IN | 等于列表中的任何一个 |
ALL | 和子查询返回的所有值进行比较 |
ANY | 和子查询返回的任一值进行比较 |
SQL> select * from emp where deptno in (select deptno from dept where loc='NEW YORK' or loc = 'CHICAGO');
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
7934 MILLER CLERK 7782 23-1月 -82 1300
10
......
7698 BLAKE MANAGER 7839 01-5月 -81 2850
30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400
30
已选择9行。
SQL> select ename, sal, deptno from emp where sal > all(select sal from emp where deptno = 30);
ENAME SAL DEPTNO
JONES 2975 20
EASON 3000 20
FORD 3000 20
SCOTT 3000 20
KING 5000 10
SQL> select ename, sal, deptno from emp where sal > any (select sal from emp where deptno = 10);
ENAME SAL DEPTNO
KING 5000 10
EASON 3000 20
......
ALLEN 1600 30
TURNER 1500 30
已选择9行。
SQL> select avg(sal) from emp group by (select deptno from emp);
select avg(sal) from emp group by (select deptno from emp)
第 1 行出现错误:
ORA-22818: 这里不允许出现子查询表达式
SQL> select rownum, empno, ename, sal from (select * from emp order by sal desc) where rownum <= 5;
ROWNUM EMPNO ENAME SAL
1 7839 KING 5000
2 7951 EASON 3000
3 7902 FORD 3000
4 7788 SCOTT 3000
5 7566 JONES 2975
SQL> select ename, job from emp where job = (select job from emp where ename = 'Ruby');
未选定行
SQL> select mgr from emp;
MGR
7566
null
......
7839
7566
7698
已选择15行。
SQL> select * from emp where empno not in (select mgr from emp);
未选定行
SQL> select * from emp where empno not in (select mgr from emp where mgr is not null);
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0
30
7951 EASON ANALYST 7566 01-12月-17 3000
20
......
7934 MILLER CLERK 7782 23-1月 -82 1300
10
7900 JAMES CLERK 7698 03-12月-81 950
30
已选择9行。