SQL> select * from emp; EMPNO EPTNO -------------7369 20 7499 30 7521 30 7566 20 7654 30 7698 30 7782 10 7788 20 7839

10 7844 30 7876 20 7900 30 7902 20 7934 10 ENAME JOB MGR HIREDATE SAL COMM D

---------- --------- ---------- --------- ---------- ---------- ----SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 800 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 950 3000 1300 0 1400 300 500

14 rows selected. SQL> select max(sal) from emp; MAX(SAL) ---------5000 SQL> select min(sal) from emp; MIN(SAL) ---------800 SQL> select * from emp where sal=(select min(sal) from emp); EMPNO ENAME JOB MGR HIREDATE SAL COMM D EPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- -----

----7369 SMITH 20 SQL> select * from emp where sal=(select avg(sal) from emp); no rows selected SQL> select * from emp where sal<(select avg(sal) from emp); EMPNO EPTNO -------------7369 20 7900 30 7876 20 7521 30 7654 30 7934 10 7844 30 7499 30 ENAME JOB MGR HIREDATE SAL COMM D CLERK 7902 17-DEC-80 800

---------- --------- ---------- --------- ---------- ---------- ----SMITH JAMES ADAMS WARD MARTIN MILLER TURNER ALLEN CLERK CLERK CLERK SALESMAN SALESMAN CLERK SALESMAN SALESMAN 7902 17-DEC-80 7698 03-DEC-81 7788 23-MAY-87 7698 22-FEB-81 7698 28-SEP-81 7782 23-JAN-82 7698 08-SEP-81 7698 20-FEB-81 800 950 1100 1250 1250 1300 1500 1600 0 300 500 1400

8 rows selected. SQL> select min(sal) from(select * from emp where sal<(select avg(sal) from emp) ); MIN(SAL) ---------800 SQL> select * from(select min(sal) from(select * from emp where sal<(select avg( sal) from emp))); MIN(SAL) ---------800 SQL> ed Wrote file afiedt.buf 1* select * from (select min(sal) from(select * from emp where sal<(select avg (sal) from emp))) SQL> /

MIN(SAL) ---------800 SQL> select min(sal) from (select min(sal) from(select * from emp where sal<(sel ect avg(sal) from emp))) 2 / select min(sal) from (select min(sal) from(select * from emp where sal<(select a vg(sal) from emp))) * ERROR at line 1: ORA-00904: "SAL": invalid identifier SQL> ed Wrote file afiedt.buf 1* select * from (select min(sal) from(select * from emp where sal<(select avg (sal) from emp))) SQL> / MIN(SAL) ---------800 SQL> ed Wrote file afiedt.buf 1* select * from (select max(sal) from(select * from emp where sal<(select avg (sal) from emp))) SQL> / MAX(SAL) ---------1600 SQL> ed Wrote file afiedt.buf 1* select * from (select max(sal) from(select * from emp where sal=(select avg (sal) from emp))) SQL> / MAX(SAL) ----------

SQL> select * from emp where deptno=(select deptno from emp where deptno=10);

---------. EMPNO ENAME JOB MGR HIREDATE SAL COMM D EPTNO ---------.buf 1* select * from emp where sal=(select sal from emp where deptno=10) SQL> / select * from emp where sal=(select sal from emp where deptno=10) * ERROR at line 1: ORA-01427: single-row subquery returns more than one row SQL> select max(sal) from emp.---------.---------.---------.--------7839 KING PRESIDENT 17-NOV-81 5000 10 SQL> select avg(sal) from emp . select * from emp where sal=(select avg(sal) from emp0 * ERROR at line 1: ORA-00921: unexpected end of SQL command SQL> ed Wrote file afiedt.buf 1* select * from emp where sal=(select avg(sal) from emp SQL> / select * from emp where sal=(select avg(sal) from emp * ERROR at line 1: ORA-00921: unexpected end of SQL command . AVG(SAL) ---------2073.--------.--------.select * from emp where deptno=(select deptno from emp where deptno=10) * ERROR at line 1: ORA-01427: single-row subquery returns more than one row SQL> ed Wrote file afiedt.21429 SQL> select * from emp where sal=(select avg(sal) from emp0. MAX(SAL) ---------5000 SQL> select * from emp where sal=(select max(sal) from emp where deptno=10).

SQL> ed Wrote file afiedt.buf 1* select * from emp where sal=(select avg(sal) from emp) SQL> / no rows selected SQL> ed Wrote file afiedt.--------.---------. MIN(SAL) ---------800 SQL> select deptno.min(sal) from emp group by deptno having>(select min(sal) fro m emp where deptno=20).--------.---------.---------.buf 1* select deptno.buf 1* select * from emp where sal>(select avg(sal) from emp) SQL> / EMPNO EPTNO -------------7782 10 7698 30 7566 20 7788 20 7902 20 7839 10 ENAME JOB MGR HIREDATE SAL COMM D ---------. select deptno.min(sal) from emp group by deptno having min(sal) >(select mi n(sal) from emp where deptno=20) SQL> / .min(sal) from emp group by deptno having>(select min(sal) from emp where deptno=20) * ERROR at line 1: ORA-00936: missing expression SQL> ed Wrote file afiedt.----CLARK BLAKE JONES SCOTT FORD KING MANAGER MANAGER MANAGER ANALYST ANALYST PRESIDENT 7839 09-JUN-81 7839 01-MAY-81 7839 02-APR-81 7566 19-APR-87 7566 03-DEC-81 17-NOV-81 2450 2850 2975 3000 3000 5000 6 rows selected. SQL> select min(sal) from emp where deptno=20.

5 2758. select max(avg(sal)) from emp group by avg(sal) * ERROR at line 1: ORA-00934: group function is not allowed here SQL> ed Wrote file afiedt.---------10 30 1300 950 SQL> select deptno. JOB AVG(SAL) --------.avg(sal) from emp group by job.min(sal) from emp group by deptno having min(sal)<(select min (sal) from emp where deptno=20). no rows selected SQL> select job.DEPTNO MIN(SAL) ---------.---------ANALYST CLERK MANAGER PRESIDENT SALESMAN 3000 1037.33333 5000 1400 SQL> select max(avg(sal)) from emp group by avg(sal).min(sal) from emp group by deptno.---------10 20 30 1300 800 950 SQL> select deptno.buf 1* select max(avg(sal)) from emp group by job SQL> / MAX(AVG(SAL)) ------------- . DEPTNO MIN(SAL) ---------.

min(sal) from emp * ERROR at line 1: ORA-00937: not a single-group group function SQL> ed Wrote file afiedt. select deptno. AVG(SAL) ---------2916. DEPTNO MIN(SAL) ---------.buf 1* select deptno.5000 SQL> select min(avg(sal)) from emp group by deptno.66667 SQL> select deptno. MIN(AVG(SAL)) ------------1566.min(sal) from emp group by deptno SQL> / DEPTNO MIN(SAL) ---------.66667 SQL> select avg(sal) from emp group by deptno.---------10 20 30 1300 800 950 SQL> select deptno.66667 2175 1566.min(sal) from emp.---------10 30 1300 950 .min(sal) from emp group by deptno having min(sal)>(select min (sal) from emp where deptno=20).

AVG(SAL) ---------3000 1037.---------20 800 SQL> select avg(sal) from emp group by job.5 2758.max(sal) from emp.5 2758.buf 1* select job.min(sal) from emp group by deptno having min(sal)=(select min (sal) from emp where deptno=20) SQL> / DEPTNO MIN(SAL) ---------.33333 5000 1400 SQL> select job. select job. avg(sal) from emp group by job SQL> / JOB AVG(SAL) --------.33333 5000 1400 SQL> ed Wrote file afiedt.buf 1* select deptno.SQL> ed Wrote file afiedt.---------ANALYST CLERK MANAGER PRESIDENT SALESMAN 3000 1037.max(sal) from emp * ERROR at line 1: ORA-00937: not a single-group group function .

avg(sal) from emp group by job. JOB AVG(SAL) --------.min(sal) from emp group by job having min(sal)>(select avg(sal) from emp).buf 1* select job. JOB AVG(SAL) --------.---------ANALYST MANAGER PRESIDENT 3000 2450 5000 SQL> select job.max(sal) from emp group by job SQL> / JOB MAX(SAL) --------.---------ANALYST 3000 .33333 5000 1400 SQL> select job.avg(sal) from emp group by job.5 2758.SQL> ed Wrote file afiedt.---------ANALYST CLERK MANAGER PRESIDENT SALESMAN 3000 1300 2975 5000 1600 SQL> select job.---------ANALYST CLERK MANAGER PRESIDENT SALESMAN 3000 1037. JOB MIN(SAL) --------.

buf 1* select * from emp where sal=(select max(sal) from(select job.CLERK MANAGER PRESIDENT SALESMAN 1037.avg(sal) from emp group by job).---------. no rows selected SQL> select min(sal) from(select job.33333 5000 1400 SQL> spool currently spooling to d:/subqueries.--------.avg(sal) from emp g roup by job) * ERROR at line 1: ORA-00921: unexpected end of SQL command SQL> ed Wrote file afiedt.---------.avg(sal) from emp group by job) * ERROR at line 1: ORA-00904: "SAL": invalid identifier SQL> select * from emp where sal=(select max(sal) from(select job.----SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 800 1600 1250 2975 1250 2850 2450 3000 1400 300 500 .avg(sal) from emp group by job)) SQL> / EMPNO EPTNO -------------7369 20 7499 30 7521 30 7566 20 7654 30 7698 30 7782 10 7788 20 ENAME JOB MGR HIREDATE SAL COMM D ---------. select * from emp where sal=(select max(sal) from(select job.txt SQL> select job.---------.min(sal) from emp group by job having 2 avg(sal)=(select avg(sal) from emp).5 2758.avg(sal) from emp group by job).--------. select min(sal) from(select job.

---------ANALYST CLERK MANAGER PRESIDENT SALESMAN 3000 800 2450 5000 1250 SQL> select deptno.---------10 20 30 1300 800 950 SQL> select job. JOB MIN(SAL) --------. DEPTNO MIN(SAL) ---------.---------10 2916.min(sal) from emp group by job.7839 KING 10 7844 TURNER 30 7876 ADAMS 20 7900 JAMES 30 7902 FORD 20 7934 MILLER 10 14 rows selected. DEPTNO AVG(SAL) ---------. PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 5000 1500 1100 950 3000 1300 0 SQL> select max(sal) from(select job.avg(sal) from emp group by job).avg(sal) from emp group by deptno.min(sal) from emp group by deptno.66667 .avg(sal) from emp group by job) * ERROR at line 1: ORA-00904: "SAL": invalid identifier SQL> select deptno. select max(sal) from(select job.

JOB AVG(SAL) --------.66667 SQL> select job.avg(sal) from emp group by job having avg(avs)>(select min(sal) from emp) * ERROR at line 1: ORA-00904: "AVS": invalid identifier SQL> ed Wrote file afiedt.avg(sal) from emp group by job.avg(sal) from emp group by job having avg(sal)>(select min(sal) from emp) SQL> / JOB AVG(SAL) --------.20 2175 30 1566.33333 5000 1400 SQL> select max(avg(sal)) from emp group by job.---------ANALYST CLERK MANAGER PRESIDENT SALESMAN 3000 1037. MAX(AVG(SAL)) .33333 5000 1400 SQL> ed Wrote file afiedt.5 2758.5 2758.---------ANALYST CLERK MANAGER PRESIDENT SALESMAN 3000 1037.buf 1* select job.avg(sal) from emp group by job having avg(avs)>(select min(sal) from emp) SQL> / select job.buf 1* select job.

buf 1* select job.buf 1* select job.max(avg(sal)) from emp group by job having avg(sal) * ERROR at line 1: ORA-00920: invalid relational operator SQL> ed Wrote file afiedt. SQL> / select job.max(avg(sal)) from emp group by job * ERROR at line 1: ORA-00937: not a single-group group function SQL> ed Wrote file afiedt. select job.max(avg(sal)) from emp group by job SQL> \/ SP2-0042: unknown command "\/" .max(avg(sal)) from emp group by job and group by job SQL> / select job.max(avg(sal)) from emp group by job and group by job * ERROR at line 1: ORA-00933: SQL command not properly ended SQL> edf SP2-0042: unknown command "edf" .max(avg(sal)) from emp group by job having avg(sal) SQL> / select job.max(avg(sal)) from emp group by ename * ERROR at line 1: ORA-00937: not a single-group group function .------------5000 SQL> select job.max(avg(sal)) from emp group by job * ERROR at line 1: ORA-00937: not a single-group group function SQL> ed Wrote file afiedt.rest of line ignored.max(avg(sal)) from emp group by ename SQL> / select ename. SQL> ed Wrote file afiedt.rest of line ignored.buf 1* select ename.buf 1* select job.max(avg(sal)) from emp group by job.

5 SQL> select avg(avg(sal)) from emp group by job. AVG(AVG(SAL)) ------------2639.SQL> ed Wrote file afiedt.66667 SQL> ed Wrote file afiedt. avg(avg(sal)) from emp where deptno=10 group by job * ERROR at line 1: ORA-00937: not a single-group group function SQL> select * from emp.buf 1* select avg(avg(sal)) from emp where deptno=10 group by job SQL> / AVG(AVG(SAL)) ------------2916.buf 1* select deptno. MIN(AVG(SAL)) ------------1037. EMPNO ENAME EPTNO JOB MGR HIREDATE SAL COMM D .buf 1* select max(avg(sal)) from emp group by ename SQL> / MAX(AVG(SAL)) ------------5000 SQL> select min(avg(sal)) from emp group by job.16667 SQL> ed Wrote file afiedt. avg(avg(sal)) from emp where deptno=10 group by job SQL> / select deptno.

SQL> SELECT * FROM EMP WHERE SAL BETWEEN(SELECT SAL FROM EMP WHERE ENAME='MILLER ') AND (SELECT SAL FROM EMP WHERE ENAME='JONES').---------.-------------7369 20 7499 30 7521 30 7566 20 7654 30 7698 30 7782 10 7788 20 7839 10 7844 30 7876 20 7900 30 7902 20 7934 10 ---------. no rows selected .----MILLER TURNER ALLEN CLARK BLAKE JONES CLERK SALESMAN SALESMAN MANAGER MANAGER MANAGER 7782 23-JAN-82 7698 08-SEP-81 7698 20-FEB-81 7839 09-JUN-81 7839 01-MAY-81 7839 02-APR-81 1300 1500 1600 2450 2850 2975 0 300 6 rows selected.----SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 800 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 950 3000 1300 0 1400 300 500 14 rows selected. EMPNO EPTNO -------------7934 10 7844 30 7499 30 7782 10 7698 30 7566 20 ENAME JOB MGR HIREDATE SAL COMM D ---------. SQL> SELECT * FROM EMP WHERE DEPTNO BETWEEN(SELECT DEPTNO FROM EMP WHERE ENAME=' SMITH') AND (SELECT DEPTNO FROM EMP WHERE ENAME='CLARK').--------.---------.---------.--------.--------.---------.--------.---------.---------.

--------.--------.SQL> SELECT DEPTNO FROM EMP WHERE ENAME='SMITH'.---------. EMPNO ENAME JOB MGR HIREDATE SAL COMM D EPTNO ---------.---------. DEPTNO ---------20 SQL> SELECT DEPTNO FROM EMP WHERE ENAME='CLARK'.30).---------.20. DEPTNO ---------10 SQL> SELECT * FROM EMP WHERE ENAME='CLARK'.--------7782 CLARK MANAGER 7839 09-JUN-81 2450 10 SQL> SELECT ENAME FROM EMP WHERE DEPTNO IN(10.---------. ENAME ---------SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER .

---------.----SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 800 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 950 3000 1300 0 1400 300 500 14 rows selected.14 rows selected. EMPNO EPTNO -------------7369 20 7499 30 7521 30 7566 20 7654 30 7698 30 7782 10 7788 20 7839 10 7844 30 7876 20 7900 30 7902 20 7934 10 ENAME JOB MGR HIREDATE SAL COMM D ---------.----CLARK KING MILLER SMITH JONES SCOTT MANAGER PRESIDENT CLERK CLERK MANAGER ANALYST 7839 09-JUN-81 17-NOV-81 7782 23-JAN-82 7902 17-DEC-80 7839 02-APR-81 7566 19-APR-87 2450 5000 1300 800 2975 3000 . SQL> SELECT * FROM EMP. EMPNO EPTNO -------------7782 10 7839 10 7934 10 7369 20 7566 20 7788 20 ENAME JOB MGR HIREDATE SAL COMM D ---------. SQL> SELECT * FROM EMP WHERE DEPTNO BETWEEN(SELECT DEPTNO FROM EMP WHERE ENAME=' CLARK') AND (SELECT DEPTNO FROM EMP WHERE ENAME='ALLEN').--------.---------.--------.---------.--------.---------.--------.---------.---------.

---------.----ALLEN WARD MARTIN TURNER SALESMAN SALESMAN SALESMAN SALESMAN 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7698 08-SEP-81 1600 1250 1250 1500 300 500 1400 0 SQL> ED Wrote file afiedt.---------.---------.---------. EMPNO EPTNO -------------7369 20 7499 30 7521 30 7654 30 7698 30 ENAME JOB MGR HIREDATE SAL COMM D ---------.--------.--------.buf 1* SELECT * FROM EMP WHERE COMM BETWEEN(SELECT COMM FROM EMP WHERE EMPNO=7782) AND (SELECT COMM FROM EMP WHERE EMPNO=7654) SQL> / no rows selected SQL> SELECT * FROM EMP WHERE ENAME NOT IN('MILLER'. EMPNO EPTNO -------------7499 30 7521 30 7654 30 7844 30 ENAME JOB MGR HIREDATE SAL COMM D ---------.'JONES').7876 ADAMS 20 7902 FORD 20 7499 ALLEN 30 7521 WARD 30 7654 MARTIN 30 7698 BLAKE 30 7844 TURNER 30 7900 JAMES 30 14 rows selected. CLERK ANALYST SALESMAN SALESMAN SALESMAN MANAGER SALESMAN CLERK 7788 23-MAY-87 7566 03-DEC-81 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7839 01-MAY-81 7698 08-SEP-81 7698 03-DEC-81 1100 3000 1600 1250 1250 2850 1500 950 0 300 500 1400 SQL> SELECT * FROM EMP WHERE COMM BETWEEN(SELECT COMM FROM EMP WHERE EMPNO=7844) AND (SELECT COMM FROM EMP WHERE EMPNO=7654).--------.---------.---------.----SMITH ALLEN WARD MARTIN BLAKE CLERK SALESMAN SALESMAN SALESMAN MANAGER 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7839 01-MAY-81 800 1600 1250 1250 2850 300 500 1400 .--------.

7782 CLARK 10 7788 SCOTT 20 7839 KING 10 7844 TURNER 30 7876 ADAMS 20 7900 JAMES 30 7902 FORD 20 12 rows selected. MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 2450 3000 5000 1500 1100 950 3000 0 SQL> SELECT * FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE DNAME='SALES') AND JOB=(SELECT JOB FROM EMP WHERE ENAME='ALLEN') AND ENAME<>'ALLEN'.buf 1* SELECT * FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='SALES' ) AND JOB=(SELECT JOB FROM EMP WHERE ENAME='ALLEN') AND ENAME<>'ALLEN' SQL> / . SELECT * FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE DNAME='SALES') AND JOB=(SELECT JOB FROM EMP WHERE ENAME='ALLEN') AND ENAME<>'ALLEN' * ERROR at line 1: ORA-00904: "DNAME": invalid identifier SQL> ED Wrote file afiedt.buf 1* SELECT * FROM DEPT WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE DNAME='SALES' ) AND JOB=(SELECT JOB FROM EMP WHERE ENAME='ALLEN') AND ENAME<>'ALLEN' SQL> / SELECT * FROM DEPT WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE DNAME='SALES') AND JOB=(SELECT JOB FROM EMP WHERE ENAME='ALLEN') AND ENAME<>'ALLEN' * ERROR at line 1: ORA-00904: "ENAME": invalid identifier SQL> ED Wrote file afiedt.buf 1* SELECT * FROM DEPT WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE DNAME='SALES' ) AND JOB=(SELECT JOB FROM EMP WHERE ENAME='ALLEN') SQL> / SELECT * FROM DEPT WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE DNAME='SALES') AND JOB=(SELECT JOB FROM EMP WHERE ENAME='ALLEN') * ERROR at line 1: ORA-00904: "JOB": invalid identifier SQL> ED Wrote file afiedt.

EMPNO EPTNO -------------7499 30 7521 30 7654 30 7844 30 ENAME JOB MGR HIREDATE SAL COMM D ---------.---------. EMPNO EPTNO -------------7521 30 7654 30 7844 30 ENAME JOB MGR HIREDATE SAL COMM D ---------.EMPNO EPTNO -------------7521 30 7654 30 7844 30 ENAME JOB MGR HIREDATE SAL COMM D ---------.--------.----WARD MARTIN TURNER SALESMAN SALESMAN SALESMAN 7698 22-FEB-81 7698 28-SEP-81 7698 08-SEP-81 1250 1250 1500 500 1400 0 SQL> SELECT DEPTNO FROM DEPT WHERE DNAME='SALES'.--------.---------. JOB --------SALESMAN SQL> SELECT * FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='SALES' ) AND JOB=(SELECT JOB FROM EMP WHERE ENAME='ALLEN').---------.---------.---------.----WARD MARTIN TURNER SALESMAN SALESMAN SALESMAN 7698 22-FEB-81 7698 28-SEP-81 7698 08-SEP-81 1250 1250 1500 500 1400 0 .--------. DEPTNO ---------30 SQL> SELECT JOB FROM EMP WHERE ENAME='ALLEN'.----ALLEN WARD MARTIN TURNER SALESMAN SALESMAN SALESMAN SALESMAN 7698 20-FEB-81 7698 22-FEB-81 7698 28-SEP-81 7698 08-SEP-81 1600 1250 1250 1500 300 500 1400 0 SQL> SELECT * FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='SALES' ) AND JOB=(SELECT JOB FROM EMP WHERE ENAME='ALLEN') AND ENAME<>'ALLEN'.--------.--------.---------.--------.---------.---------.---------.

buf 1* SELECT * FROM EMP WHERE SAL IN ((SELECT SAL FROM EMP WHERE ENAME='WARD').SQL> SELECT * FROM EMP WHERE SAL IN (SELECT SAL FROM EMP WHERE ENAME='WARD').(SELECT SAL FROM EMP WHERE ENAME='FORD') * ERROR at line 1: ORA-00933: SQL command not properly ended SQL> ED Wrote file afiedt. EMPNO EPTNO -------------7369 20 7900 30 7934 10 ENAME JOB MGR HIREDATE SAL COMM D ---------.---------.--------.---------.(SE LECT SAL FROM EMP WHERE ENAME='FORD').----- .(S ELECT SAL FROM EMP WHERE ENAME='FORD')) SQL> / EMPNO EPTNO -------------7521 30 7654 30 7788 20 7902 20 ENAME JOB MGR HIREDATE SAL COMM D ---------.--------.---------.---------.--------.---------. MIN(SAL) ---------1300 800 950 SQL> SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO).---------.----WARD MARTIN SCOTT FORD SALESMAN SALESMAN ANALYST ANALYST 7698 22-FEB-81 7698 28-SEP-81 7566 19-APR-87 7566 03-DEC-81 1250 1250 3000 3000 500 1400 SQL> SELECT * FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO). SELECT * FROM EMP WHERE SAL IN (SELECT SAL FROM EMP WHERE ENAME='WARD').---------. EMPNO ENAME JOB MGR HIREDATE SAL COMM D EPTNO ---------.---------.--------.--------.----SMITH JAMES MILLER CLERK CLERK CLERK 7902 17-DEC-80 7698 03-DEC-81 7782 23-JAN-82 800 950 1300 SQL> SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO.---------.---------.--------.

--------.---------.SELECT DISTINCT(DEPTNO) FROM EMP) * ERROR at line 1: ORA-00936: missing expression SQL> ED Wrote file afiedt.----BLAKE SCOTT FORD KING MANAGER ANALYST ANALYST PRESIDENT 7839 01-MAY-81 7566 19-APR-87 7566 03-DEC-81 17-NOV-81 2850 3000 3000 5000 SQL> ED Wrote file afiedt.----7698 BLAKE 30 7788 SCOTT 20 7902 FORD 20 7839 KING 10 SQL> ED Wrote file afiedt.---------.buf 1* SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP GROUP BY JOB) SQL> / .---------.buf 1* SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO) SQL> / EMPNO EPTNO -------------7698 30 7788 20 7902 20 7839 10 ENAME JOB MGR HIREDATE SAL COMM D PRESIDENT 17-NOV-81 5000 ANALYST 7566 03-DEC-81 3000 ANALYST 7566 19-APR-87 3000 MANAGER 7839 01-MAY-81 2850 ---------.--------.S ELECT DISTINCT(DEPTNO) FROM EMP) SQL> / SELECT * FROM EMP WHERE SAL IN((SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO).buf 1* SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO) A ND DISTINCT(DEPTNO) SQL> / SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO) AND DI STINCT(DEPTNO) * ERROR at line 1: ORA-00936: missing expression SQL> ED Wrote file afiedt.buf 1* SELECT * FROM EMP WHERE SAL IN((SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO).

---------.--------.---------.----BLAKE SCOTT FORD KING MANAGER ANALYST ANALYST PRESIDENT 7839 01-MAY-81 7566 19-APR-87 7566 03-DEC-81 17-NOV-81 2850 3000 3000 5000 SQL> SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP GROUP BY JOB).----MILLER ALLEN JONES SCOTT FORD KING CLERK SALESMAN MANAGER ANALYST ANALYST PRESIDENT 7782 23-JAN-82 7698 20-FEB-81 7839 02-APR-81 7566 19-APR-87 7566 03-DEC-81 17-NOV-81 1300 1600 2975 3000 3000 5000 300 6 rows selected.---------.--------.---------.--------.---------.--------.--------. SQL> SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO).---------.----MILLER ALLEN JONES SCOTT FORD KING CLERK SALESMAN MANAGER ANALYST ANALYST PRESIDENT 7782 23-JAN-82 7698 20-FEB-81 7839 02-APR-81 7566 19-APR-87 7566 03-DEC-81 17-NOV-81 1300 1600 2975 3000 3000 5000 300 6 rows selected.---------. EMPNO EPTNO -------------7934 10 7499 30 7566 20 7788 20 7902 20 7839 10 ENAME JOB MGR HIREDATE SAL COMM D ---------.---------. EMPNO EPTNO -------------7698 30 7788 20 7902 20 7839 10 ENAME JOB MGR HIREDATE SAL COMM D ---------. SQL> SELECT * FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROUP BY JOB).---------. EMPNO ENAME EPTNO JOB MGR HIREDATE SAL COMM D .--------.EMPNO EPTNO -------------7934 10 7499 30 7566 20 7788 20 7902 20 7839 10 ENAME JOB MGR HIREDATE SAL COMM D ---------.

---------.---------. SELECT * FROM EMP WHERE SAL IN(SELECT AVG(SAL) FROM EMP WHERE GROUP BY JOB) * ERROR at line 1: ORA-00936: missing expression SQL> ED Wrote file afiedt.buf 1* SELECT * FROM EMP WHERE SAL=(SELECT MAX(SAL) FROM (SELECT * FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROUP BY JOB))) SQL> / EMPNO ENAME JOB MGR HIREDATE SAL COMM D EPTNO ---------.buf 1* SELECT * FROM EMP WHERE SAL IN(SELECT AVG(SAL) FROM EMP GROUP BY JOB) SQL> / EMPNO ENAME JOB MGR HIREDATE SAL COMM D EPTNO ---------.--------.---------.---------.---------.----SMITH WARD MARTIN CLARK SCOTT FORD KING CLERK SALESMAN SALESMAN MANAGER ANALYST ANALYST PRESIDENT 7902 17-DEC-80 7698 22-FEB-81 7698 28-SEP-81 7839 09-JUN-81 7566 19-APR-87 7566 03-DEC-81 17-NOV-81 800 1250 1250 2450 3000 3000 5000 500 1400 7 rows selected. SQL> SELECT * FROM EMP WHERE SAL=(SELECT MAX(SAL) FROM SELECT * FROM EMP WHERE S AL IN(SELECT MIN(SAL) FROM EMP GROUP BY JOB)).---------.--------.--------.---------.-------------7369 20 7521 30 7654 30 7782 10 7788 20 7902 20 7839 10 ---------. SELECT * FROM EMP WHERE SAL=(SELECT MAX(SAL) FROM SELECT * FROM EMP WHERE SAL IN (SELECT MIN(SAL) FROM EMP GROUP BY JOB)) * ERROR at line 1: ORA-00903: invalid table name SQL> ED Wrote file afiedt.--------.---------.--------- .--------7839 KING PRESIDENT 17-NOV-81 5000 10 SQL> SELECT * FROM EMP WHERE SAL IN(SELECT AVG(SAL) FROM EMP WHERE GROUP BY JOB) .---------.--------.---------.---------.--------.

---------.buf 1* SELECT * FROM EMP WHERE SAL>(SELECT AVG(SAl) FROM (SELECT * FROM EMP WHERE SAL IN(SELECT AVG(SAL) FROM EMP GROUP BY JOB))) . SELECT * FORM EMP WHERE SAL=(SELECT MAX(SAl) FROM (SELECT * FROM EMP WHERE SAL I N(SELECT AVG(SAL) FROM EMP GROUP BY JOB))) * ERROR at line 1: ORA-00923: FROM keyword not found where expected SQL> ED Wrote file afiedt.buf 1* SELECT * FROM EMP WHERE SAL=(SELECT MIN(SAl) FROM (SELECT * FROM EMP WHERE SAL IN(SELECT AVG(SAL) FROM EMP GROUP BY JOB))) SQL> / EMPNO EPTNO -------------7788 20 7902 20 ENAME JOB MGR HIREDATE SAL COMM D ---------.--------.7788 SCOTT 20 7902 FORD 20 7839 KING 10 ANALYST ANALYST PRESIDENT 7566 19-APR-87 7566 03-DEC-81 17-NOV-81 3000 3000 5000 SQL> SELECT * FORM EMP WHERE SAL=(SELECT MAX(SAl) FROM (SELECT * FROM EMP WHERE SAL IN(SELECT AVG(SAL) FROM EMP GROUP BY JOB))).--------7839 KING PRESIDENT 17-NOV-81 5000 10 SQL> ED Wrote file afiedt.---------.buf 1* SELECT * FROM EMP WHERE SAL=(SELECT MAX(SAl) FROM (SELECT * FROM EMP WHERE SAL IN(SELECT AVG(SAL) FROM EMP GROUP BY JOB))) SQL> / EMPNO ENAME JOB MGR HIREDATE SAL COMM D EPTNO ---------.----SCOTT FORD ANALYST ANALYST 7566 19-APR-87 7566 03-DEC-81 3000 3000 SQL> ED Wrote file afiedt.--------.--------.---------.---------.---------.buf 1* SELECT * FROM EMP WHERE SAL=(SELECT AVG(SAl) FROM (SELECT * FROM EMP WHERE SAL IN(SELECT AVG(SAL) FROM EMP GROUP BY JOB))) SQL> / no rows selected SQL> ED Wrote file afiedt.---------.--------.---------.

buf 1* SELECT * FROM EMP WHERE SAL<(SELECT AVG(SAl) FROM (SELECT * FROM EMP WHERE SAL IN(SELECT AVG(SAL) FROM EMP GROUP BY JOB))) SQL> / EMPNO EPTNO -------------7369 20 7900 30 7876 20 7521 30 7654 30 7934 10 7844 30 7499 30 7782 10 7698 30 7566 20 7788 20 7902 20 ENAME JOB MGR HIREDATE SAL COMM D ---------.----SMITH JAMES ADAMS WARD MARTIN MILLER TURNER ALLEN CLARK BLAKE JONES SCOTT FORD CLERK CLERK CLERK SALESMAN SALESMAN CLERK SALESMAN SALESMAN MANAGER MANAGER MANAGER ANALYST ANALYST 7902 17-DEC-80 7698 03-DEC-81 7788 23-MAY-87 7698 22-FEB-81 7698 28-SEP-81 7782 23-JAN-82 7698 08-SEP-81 7698 20-FEB-81 7839 09-JUN-81 7839 01-MAY-81 7839 02-APR-81 7566 19-APR-87 7566 03-DEC-81 800 950 1100 1250 1250 1300 1500 1600 2450 2850 2975 3000 3000 0 300 500 1400 13 rows selected. SQL> SELECT * FROM EMP WHERE SAL IN(SELECT AVG(SAL) FROM EMP GROUP BY DEPTNO).--------7839 KING PRESIDENT 17-NOV-81 5000 10 SQL> ED Wrote file afiedt.---------.--------.---------.---------.--------.buf 1* SELECT * FROM EMP WHERE SAL =(SELECT AVG(SAL) FROM EMP GROUP BY DEPTNO) SQL> / SELECT * FROM EMP WHERE SAL =(SELECT AVG(SAL) FROM EMP GROUP BY DEPTNO) * .---------.---------.---------.--------.--------. no rows selected SQL> ED Wrote file afiedt.---------.SQL> / EMPNO ENAME JOB MGR HIREDATE SAL COMM D EPTNO ---------.

EMPNO EPTNO -------------7369 20 7499 30 7521 30 7566 20 7654 30 7698 30 7782 10 7788 20 7839 10 7844 30 7876 20 7900 30 7902 20 7934 10 ENAME JOB MGR HIREDATE SAL COMM D ---------. no rows selected SQL> SELECT ENAME. .buf 1* SELECT * FROM EMP WHERE SAL >(SELECT AVG(SAL) FROM EMP GROUP BY DEPTNO) SQL> / SELECT * FROM EMP WHERE SAL >(SELECT AVG(SAL) FROM EMP GROUP BY DEPTNO) * ERROR at line 1: ORA-01427: single-row subquery returns more than one row SQL> SELECT * FROM EMP.--------.---------.SAL.----SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82 800 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 950 3000 1300 0 1400 300 500 14 rows selected.ERROR at line 1: ORA-01427: single-row subquery returns more than one row SQL> ED Wrote file afiedt.DEPTNO FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROU P BY DEPTNO).---------. SQL> SELECT * FROM EMP WHERE SAL NOT IN(SELECT MIN(SAL) FROM EMP WHERE JOB='MANA GERS').--------.---------.

SAL.SAL.SAL.---------SMITH JAMES MILLER 800 950 1300 20 30 10 SQL> ED Wrote file afiedt.buf 1* SELECT ENAME.ENAME SAL DEPTNO ---------.DEPTNO FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP ORD ER BY DEPTNO GROUP BY DEPTNO ) SQL> / SELECT ENAME.DEPTNO FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROU P BY DEPTNO ORDER BY DEPTNO)/ SQL> / SELECT ENAME.buf 1* SELECT ENAME.DEPTNO FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO ORDER BY DEPTNO)/ * ERROR at line 1: ORA-00907: missing right parenthesis SQL> ED Wrote file afiedt.---------.DEPTNO FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROU P BY DEPTNO ORDER BY DEPTNO) SQL> / SELECT ENAME.DEPTNO FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP ORDER BY DEPTNO GROUP BY DEPTNO ) * ERROR at line 1: ORA-00907: missing right parenthesis SQL> ED Wrote file afiedt.SAL.SAL.DEPTNO FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GRO UP BY DEPTNO ) SQL> / ENAME SAL DEPTNO .buf 1* SELECT ENAME.SAL.buf 1* SELECT ENAME.DEPTNO FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO ORDER BY DEPTNO) * ERROR at line 1: ORA-00907: missing right parenthesis SQL> ED Wrote file afiedt.SAL.

.-------------. no rows selected SQL> SELECT * FROM DEPT.---------. DEPTNO DNAME LOC ---------.buf 1* SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATE IN(SELECT HIREDATE FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCHE S'))) SQL> / no rows selected SQL> SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCHES'. SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATE IN(SELECT HIRE DATE FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE DNAME='RESEARCHES'))) * ERROR at line 1: ORA-00904: "DNAME": invalid identifier SQL> ED Wrote file afiedt.------------10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS NEW YORK DALLAS CHICAGO BOSTON SQL> SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCH'.---------SMITH JAMES MILLER 800 950 1300 20 30 10 SQL> SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATE IN(SELECT HIREDATE FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE DNAME='RESEARCHES '))). DEPTNO ---------20 SQL> SELECT HIREDATE FROM EMP WHERE DEPTNO=10.---------.

.rest of line ignored. SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATE IN(SELECT HIRE DATE FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCH' * ERROR at line 1: ORA-00921: unexpected end of SQL command SQL> ED Wrote file afiedt. SP2-0734: unknown command beginning "SLEECT SAL.buf 1* SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATE IN(SELECT HIREDATE FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE DNAME='RESEAR CH' SQL> / SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATE IN(SELECT HIRE DATE FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCH' * ERROR at line 1: ORA-00907: missing right parenthesis SQL> SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATE FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCH'))).HIREDATE --------09-JUN-81 17-NOV-81 23-JAN-82 SQL> SLEECT SAL FROM EMP WHERE HIREDATE IN(SELECT HIREDATE FROM EMP WHERE DEPTNO =10). SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATE FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCH'))) * . SQL> SELECT SAL FROM EMP WHERE HIREDATE IN(SELECT HIREDATE FROM EMP WHERE DEPTNO =10)." .. SAL ---------2450 5000 1300 SQL> SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATE IN(SELECT HIREDATE FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCH' .

buf 1* SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATEIN(SELECT HIREDATE FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCH')) ) SQL> / SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATEIN(SELECT HIRED ATE FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCH'))) * ERROR at line 1: ORA-00936: missing expression SQL> ED Wrote file afiedt.---------.---------. .ERROR at line 1: ORA-00920: invalid relational operator SQL> ED Wrote file afiedt.----SMITH JAMES MILLER CLERK CLERK CLERK 7902 17-DEC-80 7698 03-DEC-81 7782 23-JAN-82 800 950 1300 SQL> SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO.--------.--------. EMPNO EPTNO -------------7369 20 7900 30 7934 10 ENAME JOB MGR HIREDATE SAL COMM D ---------.--------.---------.buf 1* SELECT * FROM EMP WHERE SAL IN(SELECT SAL FROM EMP WHERE HIREDATE IN(SELECT HIREDATE FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='RESEARCH') )) SQL> / EMPNO EPTNO -------------7369 20 7900 30 7876 20 7566 20 7788 20 7902 20 ENAME JOB MGR HIREDATE SAL COMM D ---------.---------.---------. SQL> SELECT * FROM EMP WHERE SAL IN(SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO).----SMITH JAMES ADAMS JONES SCOTT FORD CLERK CLERK CLERK MANAGER ANALYST ANALYST 7902 17-DEC-80 7698 03-DEC-81 7788 23-MAY-87 7839 02-APR-81 7566 19-APR-87 7566 03-DEC-81 800 950 1100 2975 3000 3000 6 rows selected.--------.---------.

.MIN(SAL) ---------1300 800 950 SQL> EXIT.

Sign up to vote on this title
UsefulNot useful