0% found this document useful (0 votes)
155 views6 pages

A) A Select Statement With in Another Select Statement Is Called A Sub-Query

1. A subquery is a query nested inside another SELECT, INSERT, UPDATE, or DELETE statement. Correlated subqueries refer to columns from the outer query. Inline views use a subquery with an alias as a derived table. 2. Subqueries can return single columns with single or multiple rows, or multiple columns with single or multiple rows. Examples are provided for each type. 3. Correlated subquery execution first gets a row from the outer query, executes the subquery using values from that row, and then qualifies or disqualifies the row based on the subquery results.

Uploaded by

Srinivas Neelam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views6 pages

A) A Select Statement With in Another Select Statement Is Called A Sub-Query

1. A subquery is a query nested inside another SELECT, INSERT, UPDATE, or DELETE statement. Correlated subqueries refer to columns from the outer query. Inline views use a subquery with an alias as a derived table. 2. Subqueries can return single columns with single or multiple rows, or multiple columns with single or multiple rows. Examples are provided for each type. 3. Correlated subquery execution first gets a row from the outer query, executes the subquery using values from that row, and then qualifies or disqualifies the row based on the subquery results.

Uploaded by

Srinivas Neelam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

SUB-QUERIES INTERVIEW QUESTIONS:

1) WHAT IS THE SUBQUERY?


A) A select statement with in another select statement is called a sub-query.
-----> The dependent query is called a main query and other is called sub-query.
Example:
Select empno,ename,sal,deptno from emp where sal<(select avg(sal) from emp);
2) WHAT IS THE CORRELATED SUBQUERY?
A) The sub-query refers to any column in the parent statement it becomes a
correlated sub-query.
3) What is inline Views?
A) An inline view is a sub-query with an alias that you can use with in a Sql
statement.
---------> An inline view is not a schema object.
Example:
Select rownum as rank,name,sal from (select name,sal from emp order by sal desc)
where rownum<=3;

===========CORRELATED SUBQUERY EXECUTION


PROCESS=================
subquery
----> comparison
inline view ----> select stmt
-----------------------------------------select ename,sal,deptno from emp outer where sal > (select avg(sal) from emp
where deptno=outer.deptno);
--------------------------------------------Above query answer is find below:
ENAME SAL
DEPTNO
ALLEN 160030
JONES 297520
BLAKE 285030
SCOTT
300020
KING 500010
FORD 300020
----------------------------------------------------------------EMP(OUTER) TABLE:
----------------------------------------------------------------EMPNO
JOB
MGR HIREDATE SAL COMM
DEPTNO(candidate row)
7369
CLERK
7902
12/17/1980 800
20 ----> disqualified row
800 > 2175
7499
SALESMAN 7698 2/20/1981 1600 300 30 -----> qualified row 1600 >
1566.6
7521
SALESMAN 7698 2/22/1981 1250 500 30
7566
MANAGER 7839 4/2/1981
2975
20 -----> qualified row 2975 >
2175
7654
SALESMAN 7698 9/28/1981 1250 1400 30
7698
MANAGER 7839 5/1/1981
2850
30 -----> qualified row 2850 >
1566.6
7782
MANAGER 7839 6/9/1981
2450
10
7788
ANALYST
756612/9/1982 3000
20 -----> qualified row 3000 >
2175
7839
PRESIDENT
11/17/1981
5000
10 -----> qualified
row 5000 > 2916.6
7844
SALESMAN 7698
9/8/1981
1500 0
30
7876
CLERK
7788
1/12/1983 1100
20
7900
CLERK
7698
12/3/1981 950
30
7902
ANALYST
7566
12/3/1981 3000
20 -----> qualified row
3000 > 2175
7934
CLERK
7782 1/23/1982 1300
10
------------------------------------------------------------------------------------------------------BELOW SUBQUERY ANSWER:
select avg(sal) from emp where deptno=10; ---> 2916.66666666667;
select avg(sal) from emp where deptno=20; ---> 2175;
select avg(sal) from emp where deptno=30; ---> 1566.66666666667

------------------------------------------------------------------------------------------------------CORRELATED SUBQUERY EXECUTION PROCESS:


1) get candidate row from outer query. ---->20
2) execute innerquery using candidate row value. ---->2175
3) values from inner query to qualify or disqualiy candidate row -----> here we are
comparing sal column of candidate row 20.
--> (this structure will repeate unitil end of the last row)

=========SUBQUERIES===========
-- SUBQUERY
-- CORRELATED SUBQUERY
-- SINGLE COLUMN SINGLE ROW SUBQUERY --->MAIN QUERY CHECKING FOR
SINGLE COLUMN AND SUBQUERY RETURNS A SINGLE ROW
-- SINGLE COLUMN
MULTIPLE ROWS.

MULTIROW SUBQUERY ---> SUBQUERY RETURNS A

-- MULTI COLUMN SINGLEROW SUBQUERY -->MAIN QUERY CHECKING FOR


MULTIPLE COLUMNS AND SUBQUERY RETURNS A SINGLE ROW.
-- MULTI COLUMN MULTIROW SUBQUERY -->SUBQUERY RETURNS A MULTIPLE
ROWS.
------------------------------------------------------------------------- SINGLE COLUMN SINGLEROW SUBQUERY EXAMPLE:
SELECT * FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE
LOC='CHICAGO');
SELECT * FROM EMP WHERE SAL=(SELECT MAX(SAL) FROM EMP);
SELECT * FROM EMP WHERE SAL=(SELECT MAX(SAL) FROM EMP WHERE SAL <
(SELECT MAX(SAL) FROM EMP));
-------------------------------------------------------------------------- SINGLE COLUMN MULTIROW SUBQUERY EXAMPLE:
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC
IN ('CHICAGO','NEW YORK'));
SELECT DEPTNO FROM DEPT WHERE LOC IN ('CHICAGO','NEW YORK');
--------------------------------------------------------------------------- MULTI COLUMN SINGLEROW SUBQUERY EXAMPLE:
SELECT * FROM EMP WHERE (DEPTNO,JOB)=(SELECT DEPTNO,JOB FROM EMP
WHERE ENAME='TURNER'); --> PAIRWISE
OR
SELECT * FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE
ENAME='TURNER')
AND
JOB=(SELECT JOB FROM EMP WHERE ENAME='TURNER'); --> NON PAIRWISE
--------------------------------------------------------------------------- MULTI COLUMN MULTIROW EXAMPLE:
SELECT * FROM EMP WHERE (DEPTNO,JOB) IN (SELECT DEPTNO,JOB FROM EMP
WHERE ENAME IN ('TURNER','SMITH')); -->PAIRWISE.

OR
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM EMP WHERE
ENAME IN ('TURNER','SMITH'))
AND
JOB IN (SELECT JOB FROM EMP WHERE ENAME IN ('TURNER','SMITH')); -->NON
PAIRWISE.
--------------------------------------------------------------------------EXAMPLE OF SUBQUERY:
SELECT * FROM EMP WHERE SAL = (SELECT MAX(SAL) FROM EMP);
EXAMPLE OF CORRELATED SUBQUERY:
SELECT * FROM EMP M WHERE SAL=(SELECT MAX(SAL) FROM EMP S WHERE
S.DEPTNO=M.DEPTNO);
EMPNO
ENAME
JOB
MGR HIREDATE
SAL COMMDEPTNO
7698 BLAKEMANAGER 7839 5/1/1981
2850
30
7788 SCOTT
ANALYST
7566 12/9/1982 3000
20
7839 KING PRESIDENT 11/17/1981 5000
10
7902 FORD ANALYST
7566 12/3/1981 3000
20
--------------------------------------------------------------------------------------SELECT * FROM EMP;
EMPNO
ENAME
JOB
MGR HIREDATE
SAL COMMDEPTNO
7369 SMITH CLERK 790212/17/1980 800
20
7499 ALLENSALESMAN 7698 2/20/1981 1600 300 30
7521 WARD SALESMAN 7698 2/22/1981 1250 500 30
7566 JONES MANAGER
78394/2/1981
2975
20
7654 MARTIN
SALESMAN 7698 9/28/1981 1250 1400 30
7698 BLAKEMANAGER
78395/1/1981
2850
30
7782 CLARK
MANAGER
78396/9/1981
2450
10
7788 SCOTT
ANALYST
756612/9/1982 3000
20
7839 KING PRESIDENT 11/17/1981 5000
10
7844 TURNER
SALESMAN 7698 9/8/1981
1500 0
30
7876 ADAMS
CLERK 77881/12/1983 1100
20
7900 JAMES CLERK 769812/3/1981 950
30
7902 FORD ANALYST
756612/3/1981 3000
20
7934 MILLER
CLERK 77821/23/1982 1300
10
---------------------------------------------------------------------------------------INLINE VIEWS: IF THE TABLE NAME IS REPLACE BY A SELECT STATEMENT THEN IT
IS CALLED AS THE INLINE VIEWS.
SYNTAX: SELECT ......FROM (SELECT .......FROM (SELECT .........FROM ));
EXAMPLE 1: SELECT EMPNO,ENAME,DNO FROM (SELECT
EMPNO,ENAME,SAL,DEPTNO AS DNO FROM EMP) WHERE DNO=20;
EXAMPLE 2: SELECT * FROM (SELECT ROWNUM AS R1,EMPNO,ENAME,SAL,DEPTNO
FROM EMP) WHERE R1=5;
-----------------------------------------------------------------------------------------EXISTS/NOTEXISTS OPERATOR EXAMPLES:
-- SELECT * FROM EMP OUTER WHERE EXISTS (SELECT * FROM EMP WHERE
MGR=OUTER.EMPNO);
-- SELECT * FROM EMP OUTER WHERE NOT EXISTS (SELECT * FROM EMP WHERE
MGR=OUTER.EMPNO);
-------------------------------------------------------------------------------------------CORRELATED INSERT AND DELETE EXAMPLE:

--ALTER TABLE EMP ADD DNAME VARCHAR2(20);


--UPDATE EMP E SET DNAME = (SELECT DNAME FROM DEPT D WHERE
E.DEPTNO=D.DEPTNO);
--DELETE FROM EMP M WHERE M.DNAME = (SELECT DNAME FROM DEPT S WHERE
S.DEPTNO=M.DEPTNO);
---------------------------------------------------------------------------------------------also go through below link you will get more information about subqueries:
http://oracle-database-tips.com/oracle_subquery.html

You might also like