SUBQUERIES TO SOLVE QUERIES

http://www.tuto rialspo int.co m/sql_ce rtificate /subque rie s_to _so lve _que rie s_que stio ns.htm
Co pyrig ht © tuto rials po int.co m

1. Which of the following are the types of sub-queries?
Ordered sub-queries
Grouped sub-queries
Sing le row sub-queries
None of the above
Answer: C. A subquery is a complete query nested in the SELECT ,FROM, HAVING, or WHERE clause of
another query.T he subquery must be enclosed in parentheses and have a SELECT and a FROM clause, at a
minimum. Sing le row sub-queries and multi-row sub-queries are the main types of sub-queries
2.Which of the following is true about sub-queries?
T hey execute after the main query executes
T hey execute in parallel to the main query
T he user can execute the main query and then, if wanted, execute the sub-query
T hey execute before the main query executes.
Answer: D. T he sub-query always executes before the execution of the main query.Subqueries are completed
first.T he result of the subquery is used as input for the outer query.
3.Which of the following is true about the result of a sub-query?
T he result of a sub-query is g enerally ig nored when executed.
T he result of a sub-query doesn't g ive a result, it is just helpful in speeding up the main query execution
T he result of a sub-query is used by the main query.
T he result of a sub-query is always NULL
Answer: C. Subqueries are completed first.T he result of the subquery is used as input for the outer query.
4.Which of the following clause is mandatorily used in a sub-query?
SELECT
WHERE
ORDER BY
GROUP BY
Answer: A. A sub-query is just like any other query which has to start with a SELECT clause. T hey are
contained within an outer query.
5. Which of the following is a method for writing a sub-query in a main query?
By using JOINS
By using WHERE clause
By using the GROUP BY clause
By writing a SELECT statement embedded in the clause of another SELECT statement

Answer: D. A subquery is a complete query nested in the SELECT , FROM, HAVING, or WHERE clause of
another query.T he subquery must be enclosed in parentheses and have a SELECT and a FROM clause, at a
minimum.
6.In the g iven scenarios, which one would appropriately justify the usag e of sub-query?
When we need to sum up values
When we need to convert character values into date or number values
When we need to select rows from a table with a condition that depends on the data from the same or
different table.
None of the above
Answer: C.
7.In which of the following clauses can a sub-query be used?
HAVING
WHERE
FROM
All of the above
Answer: D. A sub-query is not different from a normal query. It can make use of all the primary clauses of a
SELECT statement.
8.Which of the following sing le-row operators can be used for writing a sub-query?
>=
<
=
All of the above
Answer: D. Sing le-row operators include =, >, <, >=, <=, and <>.
9.Which of the following multi-row operators can be used with a sub-query?
IN
ANY
ALL
All of the above
Answer: D. Multiple-row subqueries return more than one row of results.Operators that can be used with
multiple-row subqueries include IN, ALL, ANY, and EXIST S.
10.What is true about the output obtained from a sub-query?
It remains in the buffer cache
It remains inside the sub-query and can be used later when needed
It is used to complete the outer (main) query
Both A and C
Answer: C. Subqueries are completed first.T he result of the subquery is used as input for the outer query.

11.You need to find the salaries for all the employees who have a hig her salary than the Vice President of a
company 'ABC'.Which of the following queries will g ive you the required result? (Consider the table structure as
g iven)
SQL> DESC employees
Name
Null?
Type
----------------------- -------- ---------------EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SELECT first_name, last_name, salary
FROM employees
WHERE salary > (SELECT salary
FROM employees
WHERE job_id = 'VICE-PRESIDENT');
SELECT first_name, last_name, salary
FROM employees
WHERE salary = (SELECT salary
FROM employees
WHERE job_id = 'VICE-PRESIDENT');
SELECT first_name, last_name, salary
FROM employees
WHERE job_id = 'VICE-PRESIDENT');

None of the above
Answer: A. In the option 'A', the inner sub-query g ives the VP's salary as a result to the outer query.
12.What among the following is true about sub-queries?
Sub-queries can be written on either side of a comparison operator
Parenthesis is not mandatory for sub-queries
Sing le-row sub-queries can use multi-row operators but vice versa is not possible
All of the above
Answer: A. Sub queries can be placed on left or rig ht hand side of the comparison operator depending on the
query indentation and usability.
13. What will be the outcome of the following query? (Consider the g iven table structure)
SQL> DESC employees
Name
Null?
Type
----------------------- -------- ---------------EMPLOYEE_ID
NOT NULL NUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOT NULL VARCHAR2(25)
EMAIL
NOT NULL VARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOT NULL DATE
JOB_ID
NOT NULL VARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)

SELECT first_name, last_name, salary
FROM employees
WHERE salary ANY (SELECT salary FROM employees);

It executes successfully g iving the desired results
It executes successfully but does not g ive the desired results
It throws an ORA error
It executes successfully and g ives two values for each row obtained in the result set
Answer: C. Multi-row operators cannot be used in sing le-row sub-queries and vice versa.
14.Which of the following is true about sing le-row sub-queries?
T hey g ive one result from the main query
T hey g ive only one row in the result set
T hey return only one row from the inner SELECT statement
T hey g ive multiple rows from the main (outer) query
Answer: C. A sing le-row subquery can return a maximum of one value.
15.What is true about multi-row sub-queries?
T hey can return more than one column as the result of the inner query
T hey return multiple rows in the main query but only a sing le result set in the inner query
T hey return sing le row in the main query but multiple rows in the inner sub-query
T hey return more than one row from the inner SELECT statement
Answer: D. Multi-column sub-queries return more than one column in their result set, multi-row sub-queries
return more than one row from the inner query.
16.What among the following is true about sing le-row sub-queries?
T hey return only one row
T hey use sing le-row operators
Both A and B
None of the above
Answer: C.
17.Which of the following operators cannot be used in a sub-query?
AND
<
>
<>
Answer: A. Sing le-row operators include =, >, <, >=, <=, and <>. Multi-row operators that can be used with
multiple-row subqueries include IN, ALL, ANY, and EXIST S.
Examine the exhibit and answer the questions 18 to 21 that follow.

SELECT first_name. Whic h of the following queries will be c orrec t? SELECT first_name. 'C' c an fail if there are more than one employee with the same first and last name. SELECT first_name. Answer: D. SELECT first_name. last_name FROM employees WHERE department = (SELECT department FROM employees WHERE first_name = 'Jessica' AND last_name = 'Butcher').You need to find out the employees whic h belong to the department of 'J essic a Butc her' and have salary g reater than the salary of 'J essic a Butc her' who has an employee ID of 40. last_name FROM employees WHERE department = 100.18. last_name FROM employees WHERE department =100. last_name FROM employees WHERE last_name = 'Butcher' And first_name = 'Jessica'. last_name FROM employees WHERE department = (SELECT department FROM employees WHERE first_name = 'Jessica' AND last_name = 'Butcher' AND department = 100 AND employee_id = 40). last_name . 19. Whic h of the following queries will work? SELECT first_name. SELECT first_name. last_name FROM employees WHERE last_name = 'Butcher' AND first_name = 'Jessica' AND salary > 10000. SELECT first_name. 'D' is more appropriate than 'C' bec ause it filters on employee id whic h is unique and ensures that the sub-query will return sing le row only.You need to find out the names of all employees who belong to the same department as the employee 'J essic a Butc her' who is in department 100 and has an employee ID 40.

FROM employees WHERE department = (SELECT department FROM employees WHERE first_name = 'Jessica' AND last_name = 'Butcher' AND employee_id = 40) AND salary > (SELECT salary FROM employees WHERE first_name = 'Jessica' AND last_name = 'Butcher' AND employee_id = 40). 20.2) COMMISSION_PCT NUMBER(2. T he questions 18th and 19th g iven above demonstrate the usag e sub-queries in a SELECT statement.Based on the answers for questions 18th and 19th.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8. what type of sub-queries is used by them? Sing le row sub-query Multiple row sub-query Both A and B Inline sub-query Answer: A.-------. Answer: C. More than one sub-query c an be written in one SQ L statement to add more than one c ondition. Examine the table struc ture as follows and answer the questions 22 to 27 that follow: SQL> DESC employees Name Null? Type ----------------------. SELECT first_name. 21. T he inner queries c an g et data from only one table ii. last_name FROM employees WHERE department = (SELECT department FROM employees WHERE first_name = 'Jessica' AND last_name = 'Butcher' AND department = 100). Sub-queries c an fetc h data from more than one table. T he inner queries c an g et data from more than one table Whic h of the above statements are true? (i) (ii) Both (i) and (ii) Neither (i) nor (ii) Answer: B.2) MANAGER_ID NUMBER(6) .Consider two statements about outer and inner queries in c ontext of SQ L sub-queries? i.

23.2) COMMISSION_PCT NUMBER(2. SELECT job_id.-------.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SELECT last_name.DEPARTMENT_ID NUMBER(4) 22.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8.What will be the outc ome of the following query? (Choose the most appropriate answer) SQL> DESC employees Name Null? Type ----------------------.You need to find the job whic h has a maximum averag e salary. salary FROM employees WHERE salary = (SELECT max(salary) FROM employees). avg(salary) FROM employees GROUP BY job_id.What will be the outc ome of the query that follows? SELECT first_name. It exec utes suc c essfully but does not g ive the required results It throws an error as a g roup func tion is used in the sub-query It throws an error as a sing le row sub-query should c ontain a multi-row operator Answer: A. last_name. min(salary) FROM employees GROUP BY department_id HAVING MIN(salary) > (SELECT min(salary) FROM employees WHERE department_id = 100). It exec utes suc c essfully and g ives the names and minimum salary g reater than department 100 of all employees It exec utes suc c essfully and g ives the salaries of the employees in department 100 It exec utes suc c essfully and g ives the names and minimum salaries of all the employees. avg(salary) FROM employees GROUP BY job_id HAVING job_id in (SELECT max(avg(salary) FROM employees). . Answer: A. A g roup func tion c an be used within a sub-query. It exec utes suc c essfully and g ives the employees who have salaries equal to the max salary.Whic h of the following queries will g ive you the required results? SELECT job_id. job_id. HAVING c lause c an be used in sub-queries as shown 24. It throws an error.

an IN operator will work c orrec tly Answer: C. T he GRO UP BY c lause is not required in the sub-query A func tion c annot be used in a sub-query SELECT statement T he sing le row sub-query g ives multiple rec ords T he use of "=" operator is invalid.T he following query throws an error. avg(salary) FROM employees GROUP BY job_id HAVING max(avg(salary) in (SELECT max(avg(salary) FROM employees).SELECT job_id.What happens if the WHERE c ondition in the query g iven in question 26 is replac ed with a new one (WHERE job_id IS NO T NULL)? (Assume the number of rec ords in 'employees' table is 14). 26. Choose the c orrec t reason for the error as g iven in the options. last_name FROM employees WHERE commission_pct = (SELECT min(commission_pct ) FROM employees GROUP BY department_id). 1 NULL 0 T he query raises O RA error bec ause sub-query is invalid. T he GRO UP BY c lause g ives the minimum c ommission_pc t for eac h department and henc e multiple results are fetc hed to the main query g iving an error. Sinc e there is no employee with job_id "XX" in the c ompany. Sub-queries c an make use of g roup func tions and HAVING c lause to restric t the g roups. 25 . last_name FROM employees WHERE salary = (SELECT salary FROM employees WHERE job_id = 'XX'). avg(salary) FROM employees GROUP BY job_id HAVING avg(salary) in (SELECT max(avg(salary) FROM employees GROUP BY job_id). whic h when equated to job_id in the main query g ives a 0. the sub-query returns no result.How many rec ords will be returned as a result of the above query? (Assuming the no employee with job id XX exists in the c ompany) SELECT first_name. SELECT first_name. 27. Answer: D. T he query exec ution raises the exc eption "O RA-01427: sing le-row subquery . D.Consider the query g iven below. 1 14 0 O RA error Answer: D. Answer: C. SELECT job_id.

30. As there are results of salary in the department 100. it evaluates to FALSE. Examine the table struc ture as g iven. If there are 0 results. last_name.O perators that c an be used with multiple-row subqueries inc lude IN.-------. What will be the outc ome of the query g iven above if the < ANY operator is replac ed . T he multi row operators return Boolean results.T he multi row operators IN.Whic h of the following are valid multi row operators used for sub-queries? <= ANY >= != >= Answer: B. salary. 29.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SELECT first_name. ALL must be used with sing le row operators as shown in the option B. Consider the query g iven below and answer the questions 29 to 33 that follow: SQL> DESC employees Name Null? Type ----------------------.What will be the outc ome of the query if we assume that the department 100 has only one employee? It exec utes suc c essfully g iving the one result It exec utes suc c essfully g iving salaries of all the employees NULL It throws an O RA error Answer: D.What does the ANY operator evaluates to in the above query? T RUE FALSE NULL 0 Answer: A. and EXIST S. ALL. If the department 100 has one result (sing le row sub-query).returns more than one row". ANY. 31. ANY. Multiple-row subqueries return more than one row of results.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8. the < ANY operator g ives the error as it is a multi-row operator. commission_pct FROM employees WHERE salary < ANY (SELECT salary FROM employees WHERE department_id = 100) AND department_id <> 101.2) COMMISSION_PCT NUMBER(2. it returns T RUE. 28.

T he multi row operator < ANY evaluates to the statements "Less than the maximum" of the subquery.with = ANY operator? O rac le will treat eac h value of the salary returned from the sub-query as it does with IN operator T here will be no differenc e in the results T he results will differ T he exec ution will thrown an O RA error Answer: A. '< ANY' Less than the hig hest value returned by the subquery. '[NO T ] EXIST S' Row must matc h a value in the subquery < 33.-------.Assume that the < ANY operator is replac ed with the > ANY. '< ALL' Less than the lowest value returned by the subquery. Examine the g iven table struc ture and c onsider the following query: SQL> DESC employees Name Null? Type ----------------------. '> ALL' More than the hig hest value returned by the subquery. . first_name. '> ANY' More than the lowest value returned by the subquery. last_name FROM employees WHERE salary IN (SELECT max(salary) FROM employees GROUP BY department_id ). '< ANY' More than the lowest value returned by the subquery. '> ALL' More than the hig hest value returned by the subquery.What c an be said about the < ANY operator in the query g iven above? It g ives the maximum value of salary It g ives the minimum value of salary It means it g ives the values that are lesser than the hig hest None of the above Answer: C. 32. T he multi row operator > ANY evaluates to the statements "Greater than the minimum" of the subquery.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SELECT employee_id. '= ANY' Equal to any value returned by the subquery (same as IN). '< ANY' Less than the hig hest value returned by the subquery. '= ANY' Equal to any value returned by the subquery (same as IN). '< ALL' Less than the lowest value returned by the subquery. = ANY operator is equivalent to IN operator.2) COMMISSION_PCT NUMBER(2.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8. '[NO T ] EXIST S' Row must matc h a value in the subquery < 34. What is true about this operator? It g ives the maximum salary It finds only the maximum salary from the sub-query It g ives more than the minimum salary It g ives the minimum salary Answer: C.

first_name. WHERE salary IN (2500. SELECT employee_id. WHERE salary = (SELECT max(salary) FROM employees GROUP BY department_id ). When the IN operator is used. Examine the struc ture of the EMPLO YEES table as g iven below and answer the questions 35 to 37 that follow. SQL> DESC employees Name Null? Type ----------------------.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) < 35 . WHERE salary < ALL (SELECT max(salary) FROM employees GROUP BY department_id ).3000. 35 00. SELECT employee_id. last_name FROM employees WHERE salary < ALL (SELECT salary FROM employees WHERE job_id = 'FIN_ACT') AND job_id <> 'FIN_ACT'.4000). Answer: D.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8. . You need to find out whic h of the employees have a salary less than that of the salary for the job ID 'FIN_ACT '. O rac le treats individual results of the sub-query as shown in the option D. first_name. last_name FROM employees WHERE salary > ALL (SELECT salary FROM employees WHERE job_id = 'FIN_ACT') AND job_id <> 'FIN_ACT'.3500.4000) WHERE salary < ANY (SELECT max(salary) FROM employees GROUP BY department_id ). last_name FROM employees WHERE salary < ANY (SELECT salary FROM employees WHERE job_id = 'FIN_ACT') AND job_id <> 'FIN_ACT'. 3000.2) COMMISSION_PCT NUMBER(2. first_name. Whic h of the following queries will g ive you the required output? SELECT employee_id.-------.Whic h WHERE c lause among the following is equivalent to that g iven in the above query? (Assume that the salaries are 25 00.

NO T c an be used with the multi row operators IN. '[NO T ] EXIST S' Row must matc h a value in the subquery 36. first_name. < ALL means less than the minimum. . '= ANY' Equal to any value returned by the subquery (same as IN). first_name. first_name. Answer: A. '< ANY' Less than the hig hest value returned by the subquery. ANY and ALL. SELECT employee_id. last_name FROM employees WHERE salary NOT IN (SELECT salary FROM employees WHERE department_id = 100) AND department_id <> 100. last_name FROM employees WHERE salary !=ALL (SELECT salary FROM employees WHERE department_id = 100) AND department_id <> 100. None of the above Answer: C. if the < ALL is replac ed with the >ALL? It will exec ute suc c essfully g iving the same result. '> ANY' More than the lowest value returned by the subquery. '> ALL' More than the hig hest value returned by the subquery. '> ANY' More than the lowest value returned by the subquery.What will be the outc ome of the above query (the option A in the question above). last_name FROM employees WHERE salary != (SELECT salary FROM employees WHERE department_id = 100) AND department_id <> 100. SELECT employee_id. first_name. '< ALL' Less than the lowest value returned by the subquery. '< ALL' Less than the lowest value returned by the subquery. '< ANY' Less than the hig hest value returned by the subquery. '= ANY' Equal to any value returned by the subquery (same as IN).SELECT employee_id. SELECT employee_id. '[NO T ] EXIST S' Row must matc h a value in the subquery 37.You need to find the salaries for all employees who are not in the department 100. last_name FROM employees WHERE salary = (SELECT salary FROM employees WHERE job_id = 'FIN_ACT') AND job_id <> 'FIN_ACT'. '> ALL' More than the hig hest value returned by the subquery. It will throw an O RA error It will exec ute suc c essfully but g ive the employees' details who have salaries lesser than all the employees with job_id 'FI_ACCO UNT ANT '. first_name. Whic h of the following queries will g ive you the required result? SELECT employee_id. >ALL means less than the minimum. last_name FROM employees WHERE salary NOT ALL (SELECT salary FROM employees WHERE department_id = 100) AND department_id <> 100. Answer: C.

-------.2) COMMISSION_PCT NUMBER(2. last_name FROM employees WHERE employee_id NOT IN (SELECT manager_id FROM employees). modify the sub-query with an additional WHERE c lause (option D) 40. do not use the NO T IN operator or if using .Whic h of the following WHERE c lauses should be added / modified to the above query to g ive the expec ted results? WHERE employee_id != (SELECT manager_id FROM employees). 38.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8.What is true about sub-queries in g eneral? Sub-queries have to be exec uted separately from the main queries Sub-queries c an be exec uted at the will of the user. they are not related to the main query exec ution Sub-queries are equal to two sequential queries where the results of inner query are used by the main query All of the above Answer: C. D. If the sub-query is likely to have NULL values.SQL> DESC employees Name Null? Type ----------------------.What will be the result of the query g iven above? 10 NULL O RA error 0 Answer: D. WHERE employee_id <>ALL (SELECT manager_id FROM employees). O ne of the values in the inner sub-query is NULL (all employees are not manag ers!) 39. WHERE employee_id IN (SELECT manager_id FROM employees). WHERE employee_id NOT IN (SELECT manager_id FROM employees WHERE manager_id is NOT NULL). Answer: B. Whic h of the following is true about sub-queries? . 41.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SELECT first_name.

HAVING.-------. Any inequality of data type or number of c olumns would result in an O RA error. T he c olumns selec ted in the sub-query should be same as on the other side of c omparison operator.-------. last_name FROM employees WHERE employee_id NOT IN (SELECT manager_id.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8. Multiple-c olumn subqueries return more than one c olumn to the outer query. or WHERE c lause of another query. FRO M.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SELECT first_name.A sub-query c an return 0 or more rows A sub-query c an be used only in the SELECT c lause Nesting of sub-queries is limited to 2 levels Group func tions c annot be used in sub-queries Answer: A. 43. at a minimum.A report has to be extrac ted whic h displays all the departments that have one or more employees assig ned to them. What is the reason for error? T he NO T IN operator used is invalid T he WHERE c lause in the sub-query is inc orrec tly written T he c olumn in the sub-query SELECT c lause should only be one when there's an inequality used in the main query T he sub-query uses the same table as the main query Answer: C.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8. hire_date FROM employees WHERE manager_id is not null).2) COMMISSION_PCT NUMBER(2.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) . A sing le-row subquery c an return a maximum of one value. T his query returns an error. Whic h of the following queries will g ive the required output? (Consider the table struc ture as g iven) SQL> DESC employees Name Null? Type ----------------------. SQL> DESC employees Name Null? Type ----------------------.2) COMMISSION_PCT NUMBER(2. A subquery is a c omplete query nested in the SELECT . T he subquery must be enc losed in parentheses and have a SELECT and a FRO M c lause.

SELECT department_name FROM employees WHERE department_id < ANY (SELECT distinct (department_id ) FROM employees).2) COMMISSION_PCT NUMBER(2. O rac le supports the Nesting of queries to 25 5 levels. Answer: A.SELECT department_name FROM employees WHERE department_id IN (SELECT distinct (department_id ) FROM employees). D.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8. It mig ht bec ome possible that the sub-queries g ive a NULL result. What should be the best prac tic e to follow when we know what values we need to pass on to the main query in O rac le queries? Using GRO UP BY Using sub-queries Using HAVING None of the above Answer: D. 44. Consider the following query and answer the questions 46 and 47 that follow: SQL> DESC employees Name Null? Type ----------------------. 45 . SELECT department_name FROM employees WHERE department_id ANY (SELECT distinct (department_id ) FROM employees). SELECT department_name FROM employees WHERE department_id = ANY (SELECT distinct (department_id ) FROM employees). henc e it is a g ood prac tic e to use them only if we know what values we need.-------. Examine the table struc ture as g iven. whic h results in 0 rows in the main result.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) .What is the maximum level of sub-queries allowed in O rac le in a sing le SQ L statement? 20 50 Unlimited 25 5 Answer: D.

last_name. SELECT first_name. job_id FROM employees WHERE job_id = (SELECT job_id FROM employees). Answer: A. last_name. WHERE job_id >= (SELECT job_id FROM employees WHERE employee_id = 210). grade .SELECT employee_id. WHERE job_id IN (SELECT job_id FROM employees WHERE employee_id = 210). grade FROM employees. first_name. WHERE job_id = (SELECT job_id FROM employees WHERE employee_id < 210). grade WHERE (SELECT max (salary) FROM employees) BETWEEN losal and hisal. SELECT first_name. WHERE job_id > (SELECT job_id FROM employees WHERE employee_id = 210). What will be the outc ome of this c hang e? T he results will be the same O RA error thrown on exec ution T he results will differ T he query will exec ute suc c essfully g iving 0 rows. Answer: B. Whic h of the following SQ L statements will be c orrec t? SELECT first_name. grade FROM employees. Whic h of the following WHERE c lauses would you add / modify to ac hieve this result? (Consider the table struc ture as g iven) WHERE job_id = (SELECT job_id FROM employees WHERE employee_id = 210). last_name. last_name.You need to find all the employees whose job ID is the same as that of an employee with ID as 210. T he sub-query g ives more than one result on the g iven c hang e and henc e a multi row operator should replac e the "=" in the main query g iven above. grade FROM employees. You need to display the names of the employees who have the hig hest salary. grade WHERE (SELECT max (salary) FROM employees) BETWEEN losal and hisal AND salary BETWEEN losal and hisal. 46.

grade WHERE salary IN (SELECT max (salary) FROM employees) AND max(salary) BETWEEN losal and hisal. last_name.What is true about c o-related sub-queries? T he tables used in the main query are also used in a c o-related sub-query T he sub-queries whic h referenc e a c olumn used in the main query are c alled c orelated sub-queries T he sub-queries whic h are written without parenthesis are c alled c o-related subqueries T he sub-queries whic h mandatorily use different tables than those used in the main query are c alled c o-related sub-queries Answer: B. As there is no limit on the number of tables whic h c an be joined. 5 3.WHERE salary = (SELECT max (salary) FROM employees) AND salary BETWEEN losal and hisal. grade FROM employees.What is true about a c o-related sub-query? . 5 1.What is the sub-query in the FRO M c lause of an SQ L statement? (Choose the most appropriate answer) Sing le row sub-query Multi row sub-query Inline View Co-related sub-query Answer: C. there is no limit on the number of inline view in a query. Correlated subquery referenc es a c olumn in the outer query and exec utes the subquery onc e for every row in the outer query while Unc orrelated subquery exec utes the subquery first and passes the value to the outer query. 5 2.Whic h of the following statements c annot be parent statements for a sub-query? SELECT GRO UP BY UPDAT E DELET E Answer: B. 5 0. O rac le internally c reates a temporary view for the query exec ution.What is the maximum number of nesting level allowed in an Inline View type sub-query? 25 5 300 216 Unlimited Answer: D. SELECT first_name. T he rest of the options c an be in the main query (parent query) of a sub-query. T he sub-queries c an be written on either side of the operator 49. If a sub-query appears in the FRO M c lause of the SELECT statements.it forms an Inline view. Answer: B. C.

Whic h of the following will work? (Choose the most appropriate answer) SQL> DESC employees Name Null? Type ----------------------. 5 5 . first_name. last_name FROM employees e WHERE salary < ANY (SELECT avg(salary) FROM employees WHERE e. SELECT employee_id.department_id = department_id ) ORDER BY department_id . Here the department ID is obtained. SELECT employee_id.and the EXIST S operator is used to test whether the relationship or link is present. last_name FROM employees e WHERE salary > (SELECT avg (salary) FROM employees WHERE e.2) COMMISSION_PCT NUMBER(2.What is true about a c o-related sub-query? It is evaluated only onc e for the parent query It is evaluated only thric e for the parent query It is evaluated onc e for eac h row proc essed by the parent sub-query All of the above Answer: C. used to evaluate the parent query and if the salary in that row is g reater than the averag e salary of the departments of that row.department_id = department_id ) ORDER BY department_id . Answer: A.department_id = department_id ) ORDER BY department_id . first_name.-------. last_name FROM employees e WHERE salary = (SELECT avg(salary) FROM employees WHERE e. Correlated subquery referenc es a c olumn in the outer query and exec utes the subquery onc e for every row in the outer query. Whic h of the following queries will display duplic ate rec ords in a table EMPLO YEES? SQL> DESC employees . first_name. that result is returned. first_name.department_id = department_id ) ORDER BY department_id . 5 4. last_name FROM employees e WHERE salary > ANY (SELECT avg(salary) FROM employees WHERE e. You need to write a query whic h returns the names of the employees whose salaries exc eed their respec tive department's averag e salary.Examine the g iven table struc ture.5 3.Examine the g iven table struc ture.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SELECT employee_id. SELECT employee_id.

---------------DEPARTMENT_ID NOT NULL NUMBER(4) DEPARTMENT_NAME NOT NULL VARCHAR2(30) MANAGER_ID NUMBER(6) LOCATION_ID NUMBER(4) 5 6.-------.2) COMMISSION_PCT NUMBER(2.employee_id = E1.employee_id And E.ROWID).employee_id = E1.ROWID < E1.Name Null? Type ----------------------.2) COMMISSION_PCT NUMBER(2.-------. SQL> DESC employees Name Null? Type ----------------------.-------.ROWID). Answer: A.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SQL> DESC departments Name Null? Type ----------------------. SELECT * FROM employees E WHERE exists (SELECT 1 FROM employees E1 WHERE E. Correlated subquery referenc es a c olumn in the outer query and exec utes the subquery onc e for every row in the outer query. Examine the struc tures for the tables DEPART MENT S and EMPLO YEES and answer the questions 5 6 and 5 7 that follow.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SELECT * FROM employees E WHERE exists (SELECT 1 FROM employees E1 WHERE E.employee_id AND E.ROWID).ROWID < E1.employee_id).---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8. SELECT * FROM employees E WHERE exists (SELECT 1 FROM employees E1 WHERE E. It c an be used to find the duplic ate rows in a table where duplic ity is subjec ted to a c olumn or set of c olumns.Whic h of the following queries will display the system date and c ount of rec ords in the .employee_id = E1. SELECT * FROM employees E WHERE = ANY (SELECT 1 FROM employees E1 WHERE E.ROWID < E1.and the EXIST S operator is used to test whether the relationship or link is present.

(SELECT count(*) FROM departments) dept_count. this tec hnique is used to perform c alc ulations with a value produc ed from a subquery. (SELECT * FROM employees) emp_count FROM DUAL.Whic h of the following queries will tell whether a g iven employee is a manag er in a Company 'XYZ '? SELECT employee_id. manager_id FROM employees A WHERE employee_id in (SELECT manager_id from employees B) GROUP BY department_id . In this c ase. (SELECT count(*) FROM employees) emp_count FROM DUAL. A sing le-row subquery c an also be nested in the outer query's SELECT c lause. the value the subquery returns is available for every row of output the outer query g enerates. (SELECT * FROM employees) emp_count FROM DUAL GROUP BY employee_id. (SELECT * FROM departments) dept_count. (SELECT * FROM departments) dept_count. (SELECT count(*) FROM departments) dept_count. SELECT employee_id. (SELECT count(*) FROM employees) emp_count FROM DUAL GROUP BY department_id . SELECT sysdate. Answer: C. T ypic ally. Answer: D. manager_id FROM employees A WHERE employee_id < ALL (SELECT manager_id from employees B) SELECT employee_id. 5 7. SELECT employee_id.DEPART MENT S and EMPLO YEES table? SELECT sysdate. Examine the exhibit and answer the question 5 8 that follows: . manager_id FROM employees A WHERE employee_id IN (SELECT manager_id from employees B) ORDER BY manager_id desc. SELECT sysdate. SELECT sysdate. manager_id FROM employees A WHERE employee_id ANY (SELECT manager_id from employees B) ORDER BY manager_id desc.

Whic h of the following queries will g ive you maximum salary of an employee in a partic ular c ity? SELECT max (salary). SELECT max (salary). loc.5 8. department_id . loc. SELECT salary. SELECT max (avg(salary)). city FROM employees natural join departments natural join locations). loc. Examine the table struc tures as g iven below. If the temporary table c ontains g rouped data. city FROM (SELECT salary. loc. department_id . T his temporary table is more formally c alled an inline view. city FROM (SELECT salary. Answer: C. department_id . T he subquery's results are treated like any other table in the FRO M c lause. department_id . city FROM employees natural join departments natural join locations).-------. it c reates a temporary table that c an be referenc ed by other c lauses of the outer query. city FROM (SELECT salary. city FROM employees natural join departments natural join locations).2) . When a multiple-c olumn subquery is used in the outer query's FRO M c lause.2) COMMISSION_PCT NUMBER(2.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8. the g rouped subsets are treated as separate rows of data in a table. city FROM (SELECT salary. SQL> DESC employees Name Null? Type ----------------------. city FROM employees natural join departments natural join locations) GROUP BY city.

(SELECT distinct (department_id ) FROM employees ORDER BY department_id ). 5 9.---------------DEPARTMENT_ID NOT NULL NUMBER(4) DEPARTMENT_NAME NOT NULL VARCHAR2(30) MANAGER_ID NUMBER(6) LOCATION_ID NUMBER(4) SELECT department_name FROM departments d INNER JOIN employees e ON (d.If you need to display output in a spec ific order. inc lude an O RDER BY c lause as the outer query's last c lause. Answer: C. SELECT department_name FROM departments WHERE department_id = ANY (SELECT department_id ORDER BY department_id desc. SELECT department_name FROM departments WHERE department_id = (SELECT distinct(department_id ) FROM employees). 61. A subquery. c an't have an O RDER BY c lause. Answer: A.employee_id) GROUP BY department_name.Assume that the sub-query as shown in the query g iven above is modified to the following . SELECT department_name FROM departments WHERE department_id ANY (SELECT distinct(department_id ) FROM employees). What will be the outc ome as a result of this c hang e? (Choose the most appropriate answer) It will order the department_id fetc hed from the sub-query and display them in asc ending order It will throw an O RA error as the O RDER BY c lause should be ac c ompanied by the GRO UP BY c lause It will throw an O RA error bec ause an O RDER BY c lause c annot be used inside a subquery It will exec ute suc c essfully. SELECT department_name FROM departments WHERE department_id IN (SELECT distinct(department_id ) FROM employees).Whic h of the following queries c an replac e the above query by using sub-queries g iving the same result? SELECT department_name FROM departments WHERE department_id = ANY (SELECT department_id FROM employees).MANAGER_ID DEPARTMENT_ID NUMBER(6) NUMBER(4) SQL> DESC departments Name Null? Type ----------------------.-------.Assume that the query g iven above is modified as the below one. 60. FROM employees) .employee_id = e. exc ept one in the FRO M c lause. B.

A subquery. 62. NCLO B or an ARRAY c annot be used in a subquery. BLO B.What will be the outc ome of the query g iven above? It g ives all AU_ID and AU_T IT LEs starting with the letter 'S%' It g ives all AU_ID and AU_T IT LEs starting with the letter 'S%' ordered by the titles in asc ending order It throws an O RA error It returns a 0 value Answer: C.If you need to display output in a spec ific order.What will be the outc ome as a result of this c hang e? (Choose the most appropriate answer) It will order the department_id fetc hed from the sub-query and display them in asc ending order It will order the department_id fetc hed from the sub-query and display them in desc ending order It will throw an O RA error bec ause an O RDER BY c lause c annot be used inside a subquery None of the above Answer: D. A c olumn with a CLO B. By default.Whic h of the following c an be used to order results in a sub-query? O RDER BY HAVING GRO UP BY All of the above Answer: C. inc lude an O RDER BY c lause as the outer query's last c lause. Examine the exhibit below and answer the questions 63 to 65 that follow: SELECT au_id. 63. . c an't have an O RDER BY c lause. the GRO UP BY c lause performs ordering in a sub-query. exc ept one in the FRO M c lause. SELECT * FROM employees WHERE salary BETWEEN (SELECT max(salary) FROM employees WHERE department_id = 100) AND (SELECT min(salary) FROM employees where department_id = 100). au_title FROM audit WHERE au_details in (SELECT au_details FROM audit WHERE au_title like 'S%') ORDER BY au_title.

Whic h of the following queries will g ive the required result? SELECT department_id . What is the reason for the error? A GRO UP BY c lause should be used as the func tion MAX is used Both the sub-queries c annot use the same department ID in the same outer query BET WEEN operator c annot be used with a sub-query SELECT c lause should have c olumns mentioned and not a asterix (*) Answer: C. SQ L handles NULL values in a different way and henc e it is a g ood prac tic e to avoid NO T IN if the result set mig ht c ontain a NULL. 65 .T his query returns an error.What is true about using NO T IN when writing queries with sub-queries in them? NO T IN ig nores all the NULL values and g ives only the NO T NULL values NO T IN puts all the NULL values at the last and g ives the NO T NULL to be displayed first NO T IN should be not be used if a NULL value is expec ted in the result set NO T IN is just a neg ation of the operator IN and c an be c hang ed without any c aveat. min(salary) FROM employees GROUP BY department_id HAVING min(salary) > ( select max(salary) FROM employees where department_id = 10 ) SELECT department_id . min(salary) FROM employees GROUP BY department_id HAVING min(salary) > ANY ( select max(salary) FROM employees ) . Answer: C. You need to find out the names and IDs of the departments in whic h the least salary is g reater than the hig hest salary in the department 10. Consider the following table struc tures and answer the questions 66 to 72 that follow: 66. T he BET WEEN operator c an be used within a sub-query but not with a subquery.

Salary FROM employees WHERE salary in ( SELECT salary FROM employees where department_id = 10 ) SELECT employee_id. Salary FROM employees WHERE salary =ANY ( SELECT salary FROM employees where department_id = 10 ) SELECT employee_id. Salary FROM employees WHERE salary < ANY ( SELECT salary FROM employees where department_id = 10 ) Answer: A. min(salary) FROM employees HAVING max(salary) < ANY ( select min(salary) FROM employees where department_id = 10 ) SELECT department_id . Salary FROM employees WHERE salary ALL ( SELECT salary FROM employees where department_id = 10 ) SELECT employee_id.Write a query to find the employees whose salary is equal to the salary of at least one employee in department of id 10. Salary . Whic h of the following queries will g ive you the required output? SELECT employee_id.SELECT department_id . 67. min(salary) FROM employees GROUP BY department_id HAVING min(salary) > ALL ( select max(salary) FROM employees where department_id = 10 ) Answer: A. B.You need to find out all the employees who have salary g reater than at least one employee in the department 10. (Choose the best answer) SELECT employee_id. 68.

Salary FROM employees WHERE salary < ALL ( SELECT salary FROM employees where department_id = 10 ) . 69. Salary FROM employees WHERE salary < ANY ( SELECT salary FROM employees where department_id = 10 ) SELECT employee_id. Whic h of the following queries will g ive you the required output? SELECT employee_id.You need to find out all the employees who have salary lesser than the salary of all the employees in the department 10.FROM employees WHERE salary >= ANY ( SELECT salary FROM employees where department_id ) = 10 SELECT employee_id. Salary FROM employees WHERE salary =ALL ( SELECT salary FROM employees where department_id = 10 ) SELECT employee_id. Salary FROM employees WHERE salary > ANY ( SELECT salary FROM employees where department_id = 10 ) SELECT employee_id. Salary FROM employees WHERE salary > ALL ( SELECT salary FROM employees where department_id = 10 ) SELECT employee_id. Salary FROM employees WHERE salary = ALL ( SELECT salary FROM employees where department_id = 10 ) Answer: B.

Multiplec olumn subqueries return more than one c olumn to the outer query. Salary FROM employees WHERE salary < ANY ( SELECT salary FROM employees where department_id = 10 ) Answer: C.department_id ) in ( select manager_id.department_id ) > ANY ( select manager_id.200) ) SELECT employee_id. manager_id. ANY. manager_id. manager_id. manager_id. ANY. department_id FROM employees where employee_id in (121.200) ) SELECT employee_id. D. department_id FROM employees where employee_id in (121. Multiple-row subqueries return more than one row of results.SELECT employee_id. T he c olumns of data are passed to the outer query in the same order in whic h they're listed in the subquery's SELECT c lause.department_id FROM employees WHERE (manager_id. ALL.Multiplec olumn subqueries return more than one c olumn to the outer query.200) ) Answer: A. and EXIST S. 70. O perators that c an be used with multiple-row subqueries inc lude IN. Whic h of the following queries will g ive you the required output? SELECT employee_id.department_id ) < ANY ( select manager_id. ALL. department_id FROM employees where employee_id in (121.department_id ) = ANY ( select manager_id.department_id FROM employees WHERE (manager_id.You need to find out all the employees who have their manag er and department matc hing with the employee having an Employee ID of 121 or 200. O perators that c an be used with multiple-row subqueries inc lude IN.200) ) SELECT employee_id.department_id FROM employees WHERE (manager_id. department_id FROM employees where employee_id in (121.department_id FROM employees WHERE (manager_id. T he c olumns of data are passed to the outer query in the same order in whic h they're listed in the subquery's SELECT c lause. Multiple-row subqueries return more than one row of results. and EXIST S. .

department_id And employee_id = 200 ) FROM employees e SELECT employee_id.job_id AND E.department_id . employee_id. first_name.job_id AND E.department_id = e. employee_id.job_id = E1.department_id ) FROM employees e Answer: C.salary <= E1. last_name. last_name.employee E WHERE d.department_id = department_id ) FROM employees e WHERE employee_id = 200. employees E WHERE d.salary AND job_id = 'SA_REP' ) SELECT department_id . (SELECT department_name FROM departments d.You need to find the hig hest earning employee with the job ID as 'SA_REP'. (SELECT department_name FROM departments d. SELECT employee_id.department_id .department_id = e.department_id = e. (SELECT department_name FROM departments d WHERE d. Salary FROM employees e WHERE job_id = ( SELECT distinct salary FROM employees E1 WHERE E. Whic h of the following queries will be c orrec t? (Choose the most appropriate answer) SELECT employee_id. Salary FROM employees E WHERE 1 = ( SELECT count(distinct salary) FROM employees E1 WHERE E. first_name. Whic h of the following queries will be c orrec t? (Choose the most appropriate answer) SELECT job_id.salary AND job_id = 'SA_REP' SELECT department_id .department_id And employee_id = 200 ) FROM employees e SELECT employee_id. first_name.job_id = E1. 72. (SELECT department_ID FROM departments d WHERE d. last_name.salary <= E1. employee_id. last_name.71.department_id .department_id .You need to find the department name of an employee with employee ID 200. first_name. Salary FROM employees E WHERE 0 = ( SELECT count(distinct salary) FROM employees E1 .

Whic h of the following queries will be c orrec t? (Choose the most appropriate answer) SELECT employee_id. Consider the EMPLO YEES table struc ture as shown in the exhibit and answer the questions 73 to 77 that follow: 73.job_id = E1. Job_id FROM employees E WHERE exists ( SELECT * FROM employees E1 WHERE E.WHERE E.job_id < E1. Job_id FROM employees E WHERE exists ( SELECT 1 FROM employees E1 WHERE E.job_id AND E.salary AND job_id = 'SA_REP' ) Answer: B.job_id ) SELECT employee_id.You need to find the job whic h has at least one employee in it.salary <= E1. employee_id.job_id ) SELECT employee_id.salary AND job_id = 'SA_REP' ) SELECT department_id .job_id AND E. Job_id FROM employees E WHERE exists ( SELECT 1 .job_id = E1.salary = E1.job_id = E1.job_id = E1.job_id ) SELECT employee_id. Salary FROM employees E WHERE 1 = ( SELECT salary FROM employees E1 WHERE E. Job_id FROM employees E WHERE not exists ( SELECT * FROM employees E1 WHERE E.

salary = salary ).You need to find the job whic h has no employees in it.salary < salary ).job_id = E1. Job_id FROM employees E WHERE not exists ( SELECT * FROM employees E1 WHERE E. 75 . SELECT * FROM employees E WHERE 1 = (SELECT count(distinct salary ) FROM employees WHERE e. Job_id FROM employees E WHERE not exists ( SELECT 1 FROM employees E1 WHERE E.job_id < E1. SELECT * FROM employees E . Job_id FROM employees E WHERE exists ( SELECT * FROM employees E1 WHERE E.job_id ) SELECT employee_id.job_id ) Answer: A.You need to find the 3rd maximum salary from the EMPLO YEES table.job_id = E1.and the EXIST S operator is used to test whether the relationship or link is present.job_id < E1.job_id ) SELECT employee_id. Correlated subquery referenc es a c olumn in the outer query and exec utes the subquery onc e for every row in the outer query. T he NO T EXIST S is the neg ation operator for EXIST S. Whic h of the following queries will g ive you the required results? (Choose the most appropriate answer) SELECT * FROM employees E WHERE salary = (SELECT count(distinct salary ) FROM employees WHERE e. An Unc orrelated subquery exec utes the subquery first and passes the value to the outer query. It returns a BO O LEAN value.job_id ) SELECT employee_id.FROM employees E1 WHERE E. 74. Whic h of the following queries will be c orrec t? (Choose the most appropriate answer) SELECT employee_id.job_id ) Answer: B.job_id = E1. T he EXIST S operator is used to c hec k and matc h rec ords between queries. Job_id FROM employees E WHERE exists ( SELECT 1 FROM employees E1 WHERE E.

WHERE 2 = (SELECT count(distinct salary ) FROM employees WHERE e. 77. desc) GROUP BY salary ) FROM (SELECT distinct salary SELECT salary FROM ( SELECT max(rownum) as user_sal. from employees ORDER BY salary FROM employees SELECT salary FROM ( SELECT rownum as user_sal. 76.What is the maximum level up to whic h Sub-queries c an be nested? 25 5 100 . 78. SELECT salary FROM ( SELECT rownum as user_sal FROM (SELECT distinct salary ) WHERE user_sal <= &N . RO WNUM is a pseudo c olumn used for finding the nth order results.salary >salary ). You need to find the maximum salary by using the user input for g etting the value of N. Answer: D. FROM employees FROM (SELECT distinct salary ORDER FROM employees Answer: C.What will happen if a value is provided to the &N variable in the above query (option C in question 76) does not matc h with any row? (Choose the best answer) T he statement would throw an O RA error T he statement would return all the rows in the table T he statement would return NULL as the output result.salary <= salary ). Answer: D. T he statement would return no rows in the result. Whic h of the following queries will g ive you the required results? (Choose the most appropriate answer) SELECT salary FROM ( SELECT rowid as user_sal FROM (SELECT distinct salary ) WHERE user_sal=&N . SELECT * FROM employees E WHERE 3 = (SELECT count(distinct salary ) FROM employees WHERE e. salary BY salary desc) ) WHERE user_sal=&N . salary ORDER BY salary desc) ) WHERE user_sal=&N .

None of the above Answer: C. 79. '= ANY' Equal to any value returned by the subquery (same as IN).2 16 Answer: D. '< ALL' Less than the lowest value returned by the subquery. any result is what is important.What is true about the EXIST S operator in SQ L queries with respec t to sub-queries? T he c olumns selec ted in the sub-queries are important T he inner query's should return rows.-------. Examine the table struc tures as g iven below and answer the questions 83 to 86 that follow. '< ANY' Less than the hig hest value returned by the subquery.What is true about using sub-queries in INSERT statements in O rac le? T hey c an be used in the INSERT c lause without any restric tion T hey c an be used in the INSERT c lause only for Numeric values T he SELECT list of a sub-query should be the same as the c olumn list of the INSERT statement. 80. '[NO T ] EXIST S' Row must matc h a value in the subquery.What is true about the ANY operator used for sub-queries? Returns rows that matc h all the values in a list/sub-query Returns rows that matc h the first 5 values in a list/sub-query Returns rows that matc h any value in a list/sub-query Returns the value 0 when all the rows matc h in a list/sub-query Answer: C. '> ALL' More than the hig hest value returned by the subquery. 82. '> ANY' More than the lowest value returned by the subquery.What is true about the ALL operator used for sub-queries? (Choose the most appropriate answer. 81.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) . not what is SELECT ED Both A and B Neither A nor B Answer: B.) Returns rows that matc h all the values in a list/sub-query Returns rows that matc h only some values in a list/sub-query Returns rows only if all the values matc h in a list/sub-query All of the above Answer: C. SQL> DESC employees Name Null? Type ----------------------.

'MM/YYYY')= '02/1981' AND job_id = 'SA_REP' ). 'MM/YYYY')= '02/1981' AND job_id = 'SA_REP' ). .---------------DEPARTMENT_ID NOT NULL NUMBER(4) DEPARTMENT_NAME NOT NULL VARCHAR2(30) MANAGER_ID NUMBER(6) LOCATION_ID NUMBER(4) 83. 'MM/YYYY')= '02/1981' AND job_id = 'SA_REP' ). salary FROM employees WHERE salary < ALL (SELECT salary FROM employees WHERE department_id=100). 2013. SELECT first_name FROM employees WHERE employee_id exists ( SELECT employee_id FROM employees WHERE to_char(hiredate.2) COMMISSION_PCT NUMBER(2. SELECT first_name FROM employees WHERE employee_id ANY ( SELECT employee_id FROM employees WHERE to_char(hiredate. Whic h of the following queries will g ive the required results? (Consider the table struc ture as g iven) SELECT first_name FROM employees WHERE employee_id = ( SELECT employee_id FROM employees WHERE to_char(hiredate.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SQL> DESC departments Name Null? Type ----------------------.Whic h of the following statements are equivalent? SELECT employee_id . SELECT first_name FROM employees WHERE employee_id = ANY ( SELECT employee_id FROM employees WHERE to_char(hiredate. 84. Answer: B.LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8.You need to find the details of all employees who were hired for the job ID 'SA_REP' in the month of J une. 'MM/YYYY')= '02/1981' AND job_id = 'SA_REP' ).-------.

first_name.department_id = d. Both the queries will throw an error. SELECT employee_id FROM employees WHERE salary not >= ANY (SELECT salary FROM employees WHERE department_id=100). Both the queries will run suc c essfully even if there is more than one department named 'ACCO UNT S'.salary FROM employees E WHERE E.last_name . Answer: A.department_id WHERE department_name='ACCOUNTS'. If there are two departments with the same name. B. >ALL will not g ive the required result as there may be two employees with the same salary and who are the hig hest earners in the department 100 Consider table struc tures as shown in the exhibit and answer the questions 87 to 89 that . E. What will be the outc ome of the above query? It exec utes suc c essfully and g ives the required results It exec utes suc c essfully but doesn't g ive the required output It throws an O RA error on exec ution It exec utes suc c essfully and g ives the required result when >ALL is replac ed with >=ALL Answer: B. D.department_id AND E. E.salary FROM employees E1 WHERE E. None of the above Answer: A. Q uery 1: SELECT first_name FROM employees e join departments d ON e. both the queries will fail. FROM departments d What c an be said about the two statements? Both the queries should g enerate the same result.SELECT employee_id . SELECT E. salary FROM employees WHERE salary < (SELECT min(salary) FROM employees WHERE department_id=100). D.salary > ALL (SELECT E1. Q uery 2: SELECT first_name FROM employees e WHERE department_id = ANY (SELECT department_id WHERE department_name='ACCOUNTS').department_id =E1.department_id = 100).

Answer: C. job_id .department_id = 100) ORDER BY first_name. Whic h of the following queries will g ive the required output? SELECT E. 88. job_id .You need to fetc h the first names (in a reverse alphabetic al order) of all the employees in the department ID = 100 and who have the maximum salary in the J O B ID = 'SA_REP'. salary FROM employees E WHERE salary in (SELECT max(salary) FROM employees E1 where E1. salary FROM employees E WHERE salary IN (SELECT max(salary) FROM employees E1 WHERE E1. SELECT E.first_name.first_name.first_name. job_id . job_id .first_name. salary FROM employees E WHERE salary = (SELECT max(salary) FROM employees E1 WHERE E1. SELECT E. job_id .department_id = 100 GROUP BY job_id ) AND job_id = 'SA_REP' ORDER BY first_name. SELECT E.first_name. salary FROM employees E WHERE salary IN (SELECT max(salary) FROM employees E1 where job_id = 'SA_REP' GROUP BY job_id ) AND WHERE E.department_id = 100 GROUP BY job_id ) ORDER BY first_name .follow: 87.department_id = 100 ORDER BY first_name desc.In the queries g iven above (option C is the c orrec t answer). salary . you need to display all the employees with the J O B ID 'SA_REP' who have the maximum salary in the department 100. Whic h of the following queries will g ive the required output? (Choose the most appropriate output) SELECT E.

department_id GROUP BY job_id ) AND job_id = 'SA_REP' ORDER BY first_name.Selec t the query whic h will g ive you the maximum salary and maximum c omm perc entag e. salary FROM employees E WHERE salary in (SELECT max(salary) FROM employees E1 WHERE E1. max(commission_pct ) FROM employees E GROUP BY salary.first_name. SELECT employee_id. max(salary) FROM employees E GROUP BY salary. T he query should also g ive the maximum c omm perc entag e paid if the hig hest salaried employee g ets the maximum c omm perc entag e. T ypic ally. 89. A sing le-row subquery c an also be nested in the outer query's SELECT c lause. salary FROM employees E WHERE salary in (SELECT max(salary) FROM employees E1 WHERE E1. the value the subquery returns is available for every row of output the outer query g enerates. SELECT employee_id. max(salary). SELECT employee_id.department_id = 100 ORDER BY first_name desc. this tec hnique is used to perform c alc ulations with a value produc ed from a subquery. Answer: A. In this c ase. job_id .department_id = 100 GROUP BY job_id ) ORDER BY first_name . Answer: D. SELECT E. 90. commission_pct HAVING max(commission_pct ) = 100.FROM employees E WHERE salary IN (SELECT max(salary) FROM employees E1 WHERE E1. (SELECT max(salary) FROM employees) * (SELECT max(commission_pct ) FROM employees) FROM DUAL. job_id .first_name.department_id = 100) ORDER BY first_name. max(commission_pct ) FROM employees E GROUP BY salary. commission_pct . salary FROM employees E WHERE salary in (SELECT max(salary) FROM employees E1 WHERE job_id = 'SA_REP' GROUP BY job_id ) And WHERE E.What is true about the sub-queries used in the SELECT c lause of an SQ L statement? . SELECT E. max(salary). job_id . SELECT employee_id.first_name. = 100 SELECT E.

SQL> DESC employees Name Null? Type ----------------------.-------.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8.2) COMMISSION_PCT NUMBER(2. Examine the g iven table struc ture.2) COMMISSION_PCT NUMBER(2. (SELECT max(salary) FROM employees GROUP BY department_id ) FROM DUAL. It g ives the system date and the maximum salary for eac h department It g ives the maximum salary for all the departments It throws an O RA error It exec utes suc c essfully with 0 rows Answer: C. Consider the following query and answer the questions 92 to 95 that follow: SQL> DESC employees Name Null? Type ----------------------. 20.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SELECT sysdate. SELECT salary FROM employees WHERE salary >10 or salary > 20 and salary >30.-------. SELECT salary .---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8. A Multi row sub-query c annot be used in the SELECT c lause of an SQ L statement. O nly a sing le-row subquery c an be nested in the outer query's SELECT c lause. 30).T hese sub-queries are the same in all aspec ts as those used in the FRO M or WHERE c lauses T hese sub-queries have to mandatorily be sing le row sub-queries We c an use multi row operators when writing suc h sub-queries None of the above Answer: B.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SELECT salary FROM employees WHERE salary > ALL (10.

salary FROM employees E WHERE E.20. SELECT E.department_id = 100). SELECT E.salary >= (SELECT E1.department_id = 100).With respec t to the question 14 above.salary FROM employees E WHERE E.salary FROM employees E1 WHERE E1. Answer: C. SELECT E. whic h of the following queries will g ive the required output for the department number 100? SELECT E.salary FROM employees E1 WHERE E1. Answer: B.salary FROM employees E WHERE E.salary FROM employees E1 WHERE E1. E1. 93. what among the following will be an equivalent query if ALL has to be replac ed with ANY? SELECT E.salary =ANY (SELECT FROM employees E1 WHERE E1.salary FROM employees E WHERE E.salary =ANY (SELECT E1.salary FROM employees E WHERE E. If in the above query the list (10.salary FROM employees E WHERE NOT EXISTS (E.department_id = 100).salary FROM employees E1 WHERE E1. SELECT E.salary = (SELECT E1. T he question shows the ALL c lause in a simplified manner when it is followed by a sub-query 94.salary FROM employees E WHERE E.department_id = 100). T he question shows the ALL c lause in a simplified manner when it is followed by a list.salary .salary >ALL (SELECT E1.salary > (SELECT E1. SELECT E.FROM employees WHERE salary <10 and salary < 20 and salary <30.salary >ANY (SELECT E1. SELECT salary FROM employees WHERE salary >10 and salary > 20 and salary >30.30) is replac ed by a sub-query.salary FROM employees E1 WHERE E1.department_id = 100).salary FROM employees E1 WHERE E1. SELECT salary FROM employees WHERE salary >10 and salary > 20 or salary < 30.department_id = 100).department_id = 100).

salary SELECT E. An Unc orrelated subquery exec utes the subquery first and passes the value to the outer query.salary = ANY (SELECT FROM employees E1 WHERE E1. E1. Consider the following query and answer the questions 96 to 98 that follow: SQL> DESC employees Name Null? Type ----------------------.department_id = 100). T he NO T operator used while using '<= ANY' is used for neg ation of the results returned by the sub-query 95 . SELECT salary FROM employees WHERE salary <10 and salary < 20 and salary <30.salary FROM employees E WHERE NOT EXISTS (SELECT E1. Whic h of the following queries are equivalent to the above query? SELECT salary FROM employees WHERE salary >10 or salary > 20 and or >30.SELECT E. if the operator ANY is not to be used. 96. 20. Either A or B None of the above Answer: B. Examine the g iven table struc tures.With respec t to the question 94.department_id = 100 And E.and the EXIST S operator is used to test whether the relationship or link is present.-------.salary <= ANY (SELECT FROM employees E1 WHERE E1. E1.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8.salary Answer: D. 30). whic h of the following queries will be c orrec t? SELECT E.2) COMMISSION_PCT NUMBER(2.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SELECT salary FROM employees WHERE salary > ANY (10. Correlated subquery referenc es a c olumn in the outer query and exec utes the subquery onc e for every row in the outer query.salary FROM employees E WHERE NOT ( E.salary <= E1.department_id = 100)).salary FROM employees E1 WHERE E1. .salary FROM employees E WHERE NOT EXISTS (E.salary).

salary FROM employees E WHERE E.salary FROM employees E1 WHERE E1.salary FROM employees E1 WHERE E1.department_id = 100).department_id = 100).salary >E1. 97. SELECT E.salary =ANY (SELECT FROM employees E1 WHERE E1.department_id = 100).salary FROM employees E WHERE E.salary).With respec t to the question 97 above.salary FROM employees E WHERE NOT EXISTS (E.salary . T he question shows the ANY c lause in a simplified manner when it is followed by a list.salary FROM employees E WHERE EXISTS (SELECT E1. SELECT E. Answer: A. T he question shows the ANY c lause in a simplified manner when it is followed by a sub-query 98.department_id = 100 ). SELECT salary FROM employees WHERE salary >10 and salary > 20 or salary < 30. 30) is replac ed by a sub-query.department_id = 100).salary >= (SELECT E1. E1. whic h of the following queries will g ive the required output for the department number 100? SELECT E.salary FROM employees E1 WHERE E1.SELECT salary FROM employees WHERE salary >10 and salary > 20 or salary >30. SELECT E. In the above query.department_id = 100 And E.salary FROM employees E1 WHERE E1. if the list (10.salary >ANY (SELECT E1.department_id = 100).salary FROM employees E WHERE EXISTS (SELECT E1. SELECT E. SELECT E.salary = (SELECT E1. Answer: B.salary > (SELECT E1. 20. what among the following will be an equivalent query if ANY is removed? SELECT E.salary FROM employees E WHERE E.salary FROM employees E WHERE E.salary FROM employees E1 WHERE E1.salary FROM employees E1 WHERE E1.

None of the above.department_id = 100). If the sub-query returns zero rows.salary FROM employees E1 where E1.---------------EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8.SELECT E. you must nest the inner query in the outer query's HAVING c lause.2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) SELECT E. T he inner query needs to referenc e the value returned to the outer query. T he subquery returns more than one value to the outer query. A HAVING c lause is used when the g roup results of a query need to be restric ted based on some c ondition. T he EXIST S operator c an substitute the ANY operator. 1 row No rows Either A or B None of the above Answer: B. Correlated subquery referenc es a c olumn in the outer query and exec utes the subquery onc e for every row in the outer query. Subqueries c an't be used in the outer query's HAVING c lause. T he value returned by the inner query is to be c ompared to g rouped data in the outer query. SQL> DESC employees Name Null? Type ----------------------.salary FROM employees E1 WHERE E1. the '> ANY' c ondition evaluates to FALSE. Answer: B.salary FROM employees E WHERE IN (SELECT E1.2) COMMISSION_PCT NUMBER(2.salary FROM employees E WHERE E. . If a subquery's result must be c ompared with a g roup func tion. henc e "No rows" are returned.salary > ANY ( select E1.-------.and the EXIST S operator is used to test whether the relationship or link is present.department_id = 100). Answer: B.