您好,登錄后才能下訂單哦!
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行。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。