Beruflich Dokumente
Kultur Dokumente
SubqueriestoSolveQueriesQuestions
SUBQUERIESTOSOLVEQUERIESQUESTIONS
http://www.tutorialspoint.com /sql_certificate/subqueries_to_solv e_queries_questions.htm
Copy r ig h t t u t or ia lspoin t .com
1.Whichofthefollowingarethetypesofsubqueries?
A. Orderedsubqueries
B. Groupedsubqueries
C. Singlerowsubqueries
D. Noneoftheabove
Answer:C.AsubqueryisacompletequerynestedintheSELECT,FROM,HAVING,orWHEREclause
ofanotherquery.ThesubquerymustbeenclosedinparenthesesandhaveaSELECTandaFROMclause,
ataminimum.Singlerowsubqueriesandmultirowsubqueriesarethemaintypesofsubqueries
2.Whichofthefollowingistrueaboutsubqueries?
A. Theyexecuteafterthemainqueryexecutes
B. Theyexecuteinparalleltothemainquery
C. Theusercanexecutethemainqueryandthen,ifwanted,executethesubquery
D. Theyexecutebeforethemainqueryexecutes.
Answer:D.Thesubqueryalwaysexecutesbeforetheexecutionofthemainquery.Subqueriesare
completedfirst.Theresultofthesubqueryisusedasinputfortheouterquery.
3.Whichofthefollowingistrueabouttheresultofasubquery?
A. Theresultofasubqueryisgenerallyignoredwhenexecuted.
B. Theresultofasubquerydoesn'tgivearesult,itisjusthelpfulinspeedingupthemainquery
execution
C. Theresultofasubqueryisusedbythemainquery.
D. TheresultofasubqueryisalwaysNULL
Answer:C.Subqueriesarecompletedfirst.Theresultofthesubqueryisusedasinputfortheouter
query.
4.Whichofthefollowingclauseismandatorilyusedinasubquery?
A. SELECT
B. WHERE
http://www.tutorialspoint.com/cgibin/printpage.cgi
1/55
8/19/2015
SubqueriestoSolveQueriesQuestions
C. ORDERBY
D. GROUPBY
Answer:A.AsubqueryisjustlikeanyotherquerywhichhastostartwithaSELECTclause.Theyare
containedwithinanouterquery.
5.Whichofthefollowingisamethodforwritingasubqueryinamainquery?
A. ByusingJOINS
B. ByusingWHEREclause
C. ByusingtheGROUPBYclause
D. BywritingaSELECTstatementembeddedintheclauseofanotherSELECTstatement
Answer:D.AsubqueryisacompletequerynestedintheSELECT,FROM,HAVING,orWHEREclause
ofanotherquery.ThesubquerymustbeenclosedinparenthesesandhaveaSELECTandaFROMclause,
ataminimum.
6.Inthegivenscenarios,whichonewouldappropriatelyjustifytheusageofsubquery?
A. Whenweneedtosumupvalues
B. Whenweneedtoconvertcharactervaluesintodateornumbervalues
C. Whenweneedtoselectrowsfromatablewithaconditionthatdependsonthedatafromthesame
ordifferenttable.
D. Noneoftheabove
Answer:C.
7.Inwhichofthefollowingclausescanasubquerybeused?
A. HAVING
B. WHERE
C. FROM
D. Alloftheabove
Answer:D.Asubqueryisnotdifferentfromanormalquery.Itcanmakeuseofalltheprimaryclauses
ofaSELECTstatement.
8.Whichofthefollowingsinglerowoperatorscanbeusedforwritingasubquery?
A. >=
B. <
C. =
http://www.tutorialspoint.com/cgibin/printpage.cgi
2/55
8/19/2015
SubqueriestoSolveQueriesQuestions
D. Alloftheabove
Answer:D.Singlerowoperatorsinclude=,>,<,>=,<=,and<>.
9.Whichofthefollowingmultirowoperatorscanbeusedwithasubquery?
A. IN
B. ANY
C. ALL
D. Alloftheabove
Answer:D.Multiplerowsubqueriesreturnmorethanonerowofresults.Operatorsthatcanbeused
withmultiplerowsubqueriesincludeIN,ALL,ANY,andEXISTS.
10.Whatistrueabouttheoutputobtainedfromasubquery?
A. Itremainsinthebuffercache
B. Itremainsinsidethesubqueryandcanbeusedlaterwhenneeded
C. Itisusedtocompletetheoutermain query
D. BothAandC
Answer:C.Subqueriesarecompletedfirst.Theresultofthesubqueryisusedasinputfortheouter
query.
11.Youneedtofindthesalariesforalltheemployeeswhohaveahighersalarythanthe
VicePresidentofacompany'ABC'.Whichofthefollowingquerieswillgiveyouthe
requiredresult? C onsiderthetablestructureasgiven
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
. SELECTfirst_name,last_name,salary
FROMemployees
WHEREsalary>(SELECTsalary
http://www.tutorialspoint.com/cgibin/printpage.cgi
3/55
8/19/2015
SubqueriestoSolveQueriesQuestions
FROMemployees
WHEREjob_id='VICEPRESIDENT');
. SELECTfirst_name,last_name,salary
FROMemployees
WHEREsalary=(SELECTsalary
FROMemployees
WHEREjob_id='VICEPRESIDENT');
. SELECTfirst_name,last_name,salary
FROMemployees
WHEREjob_id='VICEPRESIDENT');
D. Noneoftheabove
Answer:A.Intheoption'A',theinnersubquerygivestheVP'ssalaryasaresulttotheouterquery.
12.Whatamongthefollowingistrueaboutsubqueries?
A. Subqueriescanbewrittenoneithersideofacomparisonoperator
B. Parenthesisisnotmandatoryforsubqueries
C. Singlerowsubqueriescanusemultirowoperatorsbutviceversaisnotpossible
D. Alloftheabove
Answer:A.Subqueriescanbeplacedonleftorrighthandsideofthecomparisonoperatordepending
onthequeryindentationandusability.
13.Whatwillbetheoutcomeofthefollowingquery? C onsiderthegiventablestructure
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SELECTfirst_name,last_name,salary
FROMemployees
WHEREsalaryANY(SELECTsalaryFROMemployees);
http://www.tutorialspoint.com/cgibin/printpage.cgi
4/55
8/19/2015
SubqueriestoSolveQueriesQuestions
A. Itexecutessuccessfullygivingthedesiredresults
B. Itexecutessuccessfullybutdoesnotgivethedesiredresults
C. ItthrowsanORAerror
D. Itexecutessuccessfullyandgivestwovaluesforeachrowobtainedintheresultset
Answer:C.Multirowoperatorscannotbeusedinsinglerowsubqueriesandviceversa.
14.Whichofthefollowingistrueaboutsinglerowsubqueries?
A. Theygiveoneresultfromthemainquery
B. Theygiveonlyonerowintheresultset
C. TheyreturnonlyonerowfromtheinnerSELECTstatement
D. Theygivemultiplerowsfromthemainouterquery
Answer:C.Asinglerowsubquerycanreturnamaximumofonevalue.
15.Whatistrueaboutmultirowsubqueries?
A. Theycanreturnmorethanonecolumnastheresultoftheinnerquery
B. Theyreturnmultiplerowsinthemainquerybutonlyasingleresultsetintheinnerquery
C. Theyreturnsinglerowinthemainquerybutmultiplerowsintheinnersubquery
D. TheyreturnmorethanonerowfromtheinnerSELECTstatement
Answer:D.Multicolumnsubqueriesreturnmorethanonecolumnintheirresultset,multirowsub
queriesreturnmorethanonerowfromtheinnerquery.
16.Whatamongthefollowingistrueaboutsinglerowsubqueries?
A. Theyreturnonlyonerow
B. Theyusesinglerowoperators
C. BothAandB
D. Noneoftheabove
Answer:C.
17.Whichofthefollowingoperatorscannotbeusedinasubquery?
A. AND
B. <
C. >
http://www.tutorialspoint.com/cgibin/printpage.cgi
5/55
8/19/2015
SubqueriestoSolveQueriesQuestions
D. <>
Answer:A.Singlerowoperatorsinclude=,>,<,>=,<=,and<>.Multirowoperatorsthatcanbeused
withmultiplerowsubqueriesincludeIN,ALL,ANY,andEXISTS.
Examinetheexhibitandanswerthequestions18to21thatfollow.
18.Youneedtofindoutthenamesofallemployeeswhobelongtothesamedepartment
astheemployee'JessicaButcher'whoisindepartment100andhasanemployeeID40.
Whichofthefollowingquerieswillbecorrect?
. SELECTfirst_name,last_name
FROMemployees
WHERElast_name='Butcher'
Andfirst_name='Jessica';
. SELECTfirst_name,last_name
FROMemployees
WHEREdepartment=100;
. SELECTfirst_name,last_name
FROMemployees
WHEREdepartment=(SELECTdepartment
FROMemployees
WHEREfirst_name='Jessica'
ANDlast_name='Butcher');
http://www.tutorialspoint.com/cgibin/printpage.cgi
6/55
8/19/2015
SubqueriestoSolveQueriesQuestions
. SELECTfirst_name,last_name
FROMemployees
WHEREdepartment=(SELECTdepartment
FROMemployees
WHEREfirst_name='Jessica'
ANDlast_name='Butcher'
ANDdepartment=100
ANDemployee_id=40);
Answer:D.'D'ismoreappropriatethan'C'becauseitfiltersonemployeeidwhichisuniqueandensures
thatthesubquerywillreturnsinglerowonly.'C'canfailiftherearemorethanoneemployeewiththe
samefirstandlastname.
19.Youneedtofindouttheemployeeswhichbelongtothedepartmentof'Jessica
Butcher'andhavesalarygreaterthanthesalaryof'JessicaButcher'whohasanemployee
IDof40.Whichofthefollowingquerieswillwork?
. SELECTfirst_name,last_name
FROMemployees
WHERElast_name='Butcher'
ANDfirst_name='Jessica'
ANDsalary>10000;
. SELECTfirst_name,last_name
FROMemployees
WHEREdepartment=100;
. SELECTfirst_name,last_name
FROMemployees
WHEREdepartment=(SELECTdepartment
FROMemployees
WHEREfirst_name='Jessica'
ANDlast_name='Butcher'
ANDemployee_id=40)
ANDsalary>(SELECTsalary
FROMemployees
WHEREfirst_name='Jessica'
ANDlast_name='Butcher'
ANDemployee_id=40);
. SELECTfirst_name,last_name
FROMemployees
WHEREdepartment=(SELECTdepartment
FROMemployees
WHEREfirst_name='Jessica'
ANDlast_name='Butcher'
ANDdepartment=100);
Answer:C.MorethanonesubquerycanbewritteninoneSQLstatementtoaddmorethanone
http://www.tutorialspoint.com/cgibin/printpage.cgi
7/55
8/19/2015
SubqueriestoSolveQueriesQuestions
condition.
20.Basedontheanswersforquestions18thand19th,whattypeofsubqueriesisusedby
them?
A. Singlerowsubquery
B. Multiplerowsubquery
C. BothAandB
D. Inlinesubquery
Answer:A.Thequestions18thand19thgivenabovedemonstratetheusagesubqueriesinaSELECT
statement.
21.ConsidertwostatementsaboutouterandinnerqueriesincontextofSQLsubqueries?
i.Theinnerqueriescangetdatafromonlyonetable
ii.Theinnerqueriescangetdatafrommorethanonetable
Whichoftheabovestatementsaretrue?
A. i
B. ii
C. Bothiandii
D. Neitherinorii
Answer:B.Subqueriescanfetchdatafrommorethanonetable.
Examinethetablestructureasfollowsandanswerthequestions22to27thatfollow:
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
22.Whatwillbetheoutcomeofthefollowingquery? C hoosethemostappropriateanswer
http://www.tutorialspoint.com/cgibin/printpage.cgi
8/55
8/19/2015
SubqueriestoSolveQueriesQuestions
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SELECTlast_name,job_id,salary
FROMemployees
WHEREsalary=(SELECTmax(salary)
FROMemployees);
A. Itexecutessuccessfullyandgivestheemployeeswhohavesalariesequaltothemaxsalary.
B. Itexecutessuccessfullybutdoesnotgivetherequiredresults
C. Itthrowsanerrorasagroupfunctionisusedinthesubquery
D. Itthrowsanerrorasasinglerowsubqueryshouldcontainamultirowoperator
Answer:A.Agroupfunctioncanbeusedwithinasubquery.
23.Whatwillbetheoutcomeofthequerythatfollows?
SELECTfirst_name,last_name,min(salary)
FROMemployees
GROUPBYdepartment_id
HAVINGMIN(salary)>
(SELECTmin(salary)
FROMemployees
WHEREdepartment_id=100);
A. Itexecutessuccessfullyandgivesthenamesandminimumsalarygreaterthandepartment100of
allemployees
B. Itexecutessuccessfullyandgivesthesalariesoftheemployeesindepartment100
C. Itexecutessuccessfullyandgivesthenamesandminimumsalariesofalltheemployees.
D. Itthrowsanerror.
Answer:A.HAVINGclausecanbeusedinsubqueriesasshown
24.Youneedtofindthejobwhichhasamaximumaveragesalary.Whichofthefollowing
http://www.tutorialspoint.com/cgibin/printpage.cgi
9/55
8/19/2015
SubqueriestoSolveQueriesQuestions
querieswillgiveyoutherequiredresults?
. SELECTjob_id,avg(salary)
FROMemployees
GROUPBYjob_id;
. SELECTjob_id,avg(salary)
FROMemployees
GROUPBYjob_id
HAVINGjob_idin(SELECTmax(avg(salary)FROMemployees);
. SELECTjob_id,avg(salary)
FROMemployees
GROUPBYjob_id
HAVINGmax(avg(salary)in(SELECTmax(avg(salary)FROMemployees);
. SELECTjob_id,avg(salary)
FROMemployees
GROUPBYjob_id
HAVINGavg(salary)in(SELECTmax(avg(salary)FROMemployeesGROUPBYjob_id);
Answer:D.SubqueriescanmakeuseofgroupfunctionsandHAVINGclausetorestrictthegroups.
25.T hefollowingquerythrowsanerror.Choosethecorrectreasonfortheerrorasgiven
intheoptions.
SELECTfirst_name,last_name
FROMemployees
WHEREcommission_pct=(SELECTmin(commission_pct)
FROMemployees
GROUPBYdepartment_id);
A. TheGROUPBYclauseisnotrequiredinthesubquery
B. AfunctioncannotbeusedinasubquerySELECTstatement
C. Thesinglerowsubquerygivesmultiplerecords
D. Theuseof"="operatorisinvalidanINoperatorwillworkcorrectly
Answer:C,D.TheGROUPBYclausegivestheminimumcommission_pctforeachdepartmentand
hencemultipleresultsarefetchedtothemainquerygivinganerror.
26.Considerthequerygivenbelow.Howmanyrecordswillbereturnedasaresultofthe
abovequery? AssumingthenoemployeewithjobidXXexistsinthecompany
SELECTfirst_name,last_name
FROMemployees
WHEREsalary=(SELECTsalary
http://www.tutorialspoint.com/cgibin/printpage.cgi
10/55
8/19/2015
SubqueriestoSolveQueriesQuestions
FROMemployees
WHEREjob_id='XX');
A. 1
B. NULL
C. 0
D. ThequeryraisesORAerrorbecausesubqueryisinvalid.
Answer:C.Sincethereisnoemployeewithjob_id"XX"inthecompany,thesubqueryreturnsno
result,whichwhenequatedtojob_idinthemainquerygivesa0.
27.WhathappensiftheWHEREconditioninthequerygiveninquestion26isreplaced
withanewone W H EREjob dIS NOT NU LL? Assumethenumberof recordsin employee s tableis14 .
A. 1
B. 14
C. 0
D. ORAerror
Answer:D.Thequeryexecutionraisestheexception"ORA01427:singlerowsubqueryreturnsmore
thanonerow".
28.Whichofthefollowingarevalidmultirowoperatorsusedforsubqueries?
A. <=
B. ANY>=
C. !=
D. >=
Answer:B.Multiplerowsubqueriesreturnmorethanonerowofresults.Operatorsthatcanbeused
withmultiplerowsubqueriesincludeIN,ALL,ANY,andEXISTS.ThemultirowoperatorsIN,ANY,ALL
mustbeusedwithsinglerowoperatorsasshownintheoptionB.
Examinethetablestructureasgiven.Considerthequerygivenbelowandanswerthe
questions29to33thatfollow
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
http://www.tutorialspoint.com/cgibin/printpage.cgi
11/55
8/19/2015
HIRE_DATE
JOB_ID
SALARY
COMMISSION_PCT
MANAGER_ID
DEPARTMENT_ID
SubqueriestoSolveQueriesQuestions
NOTNULLDATE
NOTNULLVARCHAR2(10)
NUMBER(8,2)
NUMBER(2,2)
NUMBER(6)
NUMBER(4)
SELECTfirst_name,last_name,salary,commission_pct
FROMemployees
WHEREsalary<ANY(SELECTsalary
FROMemployees
WHEREdepartment_id=100)
ANDdepartment_id<>101;
29.WhatdoestheANYoperatorevaluatestointheabovequery?
A. TRUE
B. FALSE
C. NULL
D. 0
Answer:A.ThemultirowoperatorsreturnBooleanresults.Asthereareresultsofsalaryinthe
department100,itreturnsTRUE.Ifthereare0results,itevaluatestoFALSE.
30.Whatwillbetheoutcomeofthequeryifweassumethatthedepartment100hasonly
oneemployee?
A. Itexecutessuccessfullygivingtheoneresult
B. Itexecutessuccessfullygivingsalariesofalltheemployees
C. NULL
D. ItthrowsanORAerror
Answer:D.Ifthedepartment100hasoneresultsinglerowsub query ,the<ANYoperatorgivesthe
errorasitisamultirowoperator.
31.Whatwillbetheoutcomeofthequerygivenaboveifthe<ANYoperatorisreplaced
with=ANYoperator?
A. OraclewilltreateachvalueofthesalaryreturnedfromthesubqueryasitdoeswithINoperator
B. Therewillbenodifferenceintheresults
C. Theresultswilldiffer
D. TheexecutionwillthrownanORAerror
Answer:A.=ANYoperatorisequivalenttoINoperator.
http://www.tutorialspoint.com/cgibin/printpage.cgi
12/55
8/19/2015
SubqueriestoSolveQueriesQuestions
32.Whatcanbesaidaboutthe<ANYoperatorinthequerygivenabove?
A. Itgivesthemaximumvalueofsalary
B. Itgivestheminimumvalueofsalary
C. Itmeansitgivesthevaluesthatarelesserthanthehighest
D. Noneoftheabove
Answer:C.Themultirowoperator<ANYevaluatestothestatements"Lessthanthemaximum"ofthe
subquery.'>ALL'Morethanthehighestvaluereturnedbythesubquery.'<ALL'Lessthanthelowest
valuereturnedbythesubquery.'<ANY'Lessthanthehighestvaluereturnedbythesubquery.'<ANY'
Morethanthelowestvaluereturnedbythesubquery.'=ANY'Equaltoanyvaluereturnedbythe
subquerysameasIN .'[NOT]EXISTS'Rowmustmatchavalueinthesubquery
<
33.Assumethatthe<ANYoperatorisreplacedwiththe>ANY.Whatistrueaboutthis
operator?
A. Itgivesthemaximumsalary
B. Itfindsonlythemaximumsalaryfromthesubquery
C. Itgivesmorethantheminimumsalary
D. Itgivestheminimumsalary
Answer:C.Themultirowoperator>ANYevaluatestothestatements"Greaterthantheminimum"of
thesubquery.'>ALL'Morethanthehighestvaluereturnedbythesubquery.'<ALL'Lessthanthelowest
valuereturnedbythesubquery.'<ANY'Lessthanthehighestvaluereturnedbythesubquery.'>ANY'
Morethanthelowestvaluereturnedbythesubquery.'=ANY'Equaltoanyvaluereturnedbythe
subquerysameasIN .'[NOT]EXISTS'Rowmustmatchavalueinthesubquery
<
34.Examinethegiventablestructureandconsiderthefollowingquery:
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
http://www.tutorialspoint.com/cgibin/printpage.cgi
13/55
8/19/2015
SubqueriestoSolveQueriesQuestions
DEPARTMENT_ID
NUMBER(4)
SELECTemployee_id,first_name,last_name
FROMemployees
WHEREsalaryIN(SELECTmax(salary)
FROMemployees
GROUPBYdepartment_id);
WhichWHEREclauseamongthefollowingisequivalenttothatgivenintheabovequery?
Assumethatthesalariesare2500, 3000, 3500, 4000
. WHEREsalary<ANY(SELECTmax(salary)
FROMemployees
GROUPBYdepartment_id);
. WHEREsalary<ALL(SELECTmax(salary)
FROMemployees
GROUPBYdepartment_id);
. WHEREsalary=(SELECTmax(salary)
FROMemployees
GROUPBYdepartment_id);
. WHEREsalaryIN(2500,3000,3500,4000);
Answer:D.WhentheINoperatorisused,Oracletreatsindividualresultsofthesubqueryasshownin
theoptionD.
ExaminethestructureoftheEMPLOYEEStableasgivenbelowandanswerthequestions
35to37thatfollow.
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
<
35.Youneedtofindoutwhichoftheemployeeshaveasalarylessthanthatofthesalary
http://www.tutorialspoint.com/cgibin/printpage.cgi
14/55
8/19/2015
SubqueriestoSolveQueriesQuestions
forthejobID'FIN_ACT '.Whichofthefollowingquerieswillgiveyoutherequired
output?
. SELECTemployee_id,first_name,last_name
FROMemployees
WHEREsalary<ALL
(SELECTsalary
FROMemployees
WHEREjob_id='FIN_ACT')
ANDjob_id<>'FIN_ACT';
. SELECTemployee_id,first_name,last_name
FROMemployees
WHEREsalary>ALL
(SELECTsalary
FROMemployees
WHEREjob_id='FIN_ACT')
ANDjob_id<>'FIN_ACT';
. SELECTemployee_id,first_name,last_name
FROMemployees
WHEREsalary<ANY
(SELECTsalary
FROMemployees
WHEREjob_id='FIN_ACT')
ANDjob_id<>'FIN_ACT';
. SELECTemployee_id,first_name,last_name
FROMemployees
WHEREsalary=(SELECTsalary
FROMemployees
WHEREjob_id='FIN_ACT')
ANDjob_id<>'FIN_ACT';
Answer:A.<ALLmeanslessthantheminimum.'>ALL'Morethanthehighestvaluereturnedbythe
subquery.'<ALL'Lessthanthelowestvaluereturnedbythesubquery.'<ANY'Lessthanthehighest
valuereturnedbythesubquery.'>ANY'Morethanthelowestvaluereturnedbythesubquery.'=ANY'
EqualtoanyvaluereturnedbythesubquerysameasIN .'[NOT]EXISTS'Rowmustmatchavalueinthe
subquery
36.Whatwillbetheoutcomeoftheabovequery theoptionAinthequestionabove ,ifthe<ALL
isreplacedwiththe>ALL?
A. Itwillexecutesuccessfullygivingthesameresult.
B. ItwillthrowanORAerror
C. Itwillexecutesuccessfullybutgivetheemployees'detailswhohavesalarieslesserthanallthe
employeeswithjob_id'FI_ACCOUNTANT'.
http://www.tutorialspoint.com/cgibin/printpage.cgi
15/55
8/19/2015
SubqueriestoSolveQueriesQuestions
D. Noneoftheabove
Answer:C.>ALLmeanslessthantheminimum.'>ALL'Morethanthehighestvaluereturnedbythe
subquery.'<ALL'Lessthanthelowestvaluereturnedbythesubquery.'<ANY'Lessthanthehighest
valuereturnedbythesubquery.'>ANY'Morethanthelowestvaluereturnedbythesubquery.'=ANY'
EqualtoanyvaluereturnedbythesubquerysameasIN .'[NOT]EXISTS'Rowmustmatchavalueinthe
subquery
37.Youneedtofindthesalariesforallemployeeswhoarenotinthedepartment100.
Whichofthefollowingquerieswillgiveyoutherequiredresult?
. SELECTemployee_id,first_name,last_name
FROMemployees
WHEREsalary!=ALL
(SELECTsalary
FROMemployees
WHEREdepartment_id=100)
ANDdepartment_id<>100;
. SELECTemployee_id,first_name,last_name
FROMemployees
WHEREsalaryNOTIN
(SELECTsalary
FROMemployees
WHEREdepartment_id=100)
ANDdepartment_id<>100;
. SELECTemployee_id,first_name,last_name
FROMemployees
WHEREsalaryNOTALL
(SELECTsalary
FROMemployees
WHEREdepartment_id=100)
ANDdepartment_id<>100;
. SELECTemployee_id,first_name,last_name
FROMemployees
WHEREsalary!=(SELECTsalary
FROMemployees
WHEREdepartment_id=100)
ANDdepartment_id<>100;
Answer:C.NOTcanbeusedwiththemultirowoperatorsIN,ANYandALL.
Examinethetablestructureasgiven.Considerthefollowingqueryandanswerthequestions38and39
thatfollow.Youneedtofindtheemployeeswhodonothaveasubordinatereportingtothem.
Assumethereare0expectedresults
SQL>DESCemployees
http://www.tutorialspoint.com/cgibin/printpage.cgi
16/55
8/19/2015
SubqueriestoSolveQueriesQuestions
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SELECTfirst_name,last_name
FROMemployees
WHEREemployee_idNOTIN
(SELECTmanager_id
FROMemployees);
38.Whatwillbetheresultofthequerygivenabove?
A. 10
B. NULL
C. ORAerror
D. 0
Answer:D.OneofthevaluesintheinnersubqueryisNULLallemployeesarenotmanagers!
39.WhichofthefollowingWHEREclausesshouldbeadded/modifiedtotheabovequery
togivetheexpectedresults?
. WHEREemployee_id!=(SELECTmanager_idFROMemployees);
. WHEREemployee_idIN(SELECTmanager_idFROMemployees);
. WHEREemployee_id<>ALL(SELECTmanager_idFROMemployees);
. WHEREemployee_idNOTIN(SELECTmanager_id
FROMemployees
WHEREmanager_idisNOTNULL);
Answer:B,D.IfthesubqueryislikelytohaveNULLvalues,donotusetheNOTINoperatororif
using,modifythesubquerywithanadditionalWHEREclauseoptionD
40.Whatistrueaboutsubqueriesingeneral?
http://www.tutorialspoint.com/cgibin/printpage.cgi
17/55
8/19/2015
SubqueriestoSolveQueriesQuestions
A. Subquerieshavetobeexecutedseparatelyfromthemainqueries
B. Subqueriescanbeexecutedatthewilloftheuser,theyarenotrelatedtothemainqueryexecution
C. Subqueriesareequaltotwosequentialquerieswheretheresultsofinnerqueryareusedbythe
mainquery
D. Alloftheabove
Answer:C.
41.Whichofthefollowingistrueaboutsubqueries?
A. Asubquerycanreturn0ormorerows
B. AsubquerycanbeusedonlyintheSELECTclause
C. Nestingofsubqueriesislimitedto2levels
D. Groupfunctionscannotbeusedinsubqueries
Answer:A.AsubqueryisacompletequerynestedintheSELECT,FROM,HAVING,orWHEREclause
ofanotherquery.ThesubquerymustbeenclosedinparenthesesandhaveaSELECTandaFROMclause,
ataminimum.Asinglerowsubquerycanreturnamaximumofonevalue.Multiplecolumnsubqueries
returnmorethanonecolumntotheouterquery.
42.Examinethetablestructureasgiven.
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
Considerthefollowingquery.
SELECTfirst_name,last_name
FROMemployees
WHEREemployee_idNOTIN
(SELECTmanager_id,hire_date
FROMemployees
WHEREmanager_idisnotnull);
http://www.tutorialspoint.com/cgibin/printpage.cgi
18/55
8/19/2015
SubqueriestoSolveQueriesQuestions
Thisqueryreturnsanerror.Whatisthereasonforerror?
A. TheNOTINoperatorusedisinvalid
B. TheWHEREclauseinthesubqueryisincorrectlywritten
C. ThecolumninthesubquerySELECTclauseshouldonlybeonewhenthere'saninequalityusedin
themainquery
D. Thesubqueryusesthesametableasthemainquery
Answer:C.Thecolumnsselectedinthesubqueryshouldbesameasontheothersideofcomparison
operator.AnyinequalityofdatatypeornumberofcolumnswouldresultinanORAerror.
43.Areporthastobeextractedwhichdisplaysallthedepartmentsthathaveoneormore
employeesassignedtothem.Whichofthefollowingquerieswillgivetherequired
output? C onsiderthetablestructureasgiven
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
. SELECTdepartment_name
FROMemployees
WHEREdepartment_idIN(SELECTdistinct(department_id)
FROMemployees);
. SELECTdepartment_name
FROMemployees
WHEREdepartment_idANY(SELECTdistinct(department_id)
FROMemployees);
. SELECTdepartment_name
FROMemployees
WHEREdepartment_id<ANY(SELECTdistinct(department_id)
FROMemployees);
. SELECTdepartment_name
FROMemployees
http://www.tutorialspoint.com/cgibin/printpage.cgi
19/55
8/19/2015
SubqueriestoSolveQueriesQuestions
WHEREdepartment_id=ANY(SELECTdistinct(department_id)
FROMemployees);
Answer:A,D.
44.WhatisthemaximumlevelofsubqueriesallowedinOracleinasingleSQL
statement?
A. 20
B. 50
C. Unlimited
D. 255
Answer:D.OraclesupportstheNestingofqueriesto255levels.
45.Whatshouldbethebestpracticetofollowwhenweknowwhatvaluesweneedtopass
ontothemainqueryinOraclequeries?
A. UsingGROUPBY
B. Usingsubqueries
C. UsingHAVING
D. Noneoftheabove
Answer:D.ItmightbecomepossiblethatthesubqueriesgiveaNULLresult,whichresultsin0rowsin
themainresulthenceitisagoodpracticetousethemonlyifweknowwhatvaluesweneed.
Examinethetablestructureasgiven.Considerthefollowingqueryandanswerthe
questions46and47thatfollow:
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SELECTemployee_id,first_name,last_name,job_id
FROMemployees
http://www.tutorialspoint.com/cgibin/printpage.cgi
20/55
8/19/2015
SubqueriestoSolveQueriesQuestions
WHEREjob_id=(SELECTjob_idFROMemployees);
46.YouneedtofindalltheemployeeswhosejobIDisthesameasthatofanemployee
withIDas210.WhichofthefollowingWHEREclauseswouldyouadd/modifyto
achievethisresult?(Considerthetablestructureasgiven
. WHEREjob_id=(SELECTjob_idFROMemployeesWHEREemployee_id=210);
. WHEREjob_idIN(SELECTjob_idFROMemployeesWHEREemployee_id=210);
. WHEREjob_id>(SELECTjob_idFROMemployeesWHEREemployee_id=210);
. WHEREjob_id>=(SELECTjob_idFROMemployeesWHEREemployee_id=210);
Answer:A.
47.AssumethatyouchangetheWHEREclauseasgivenintheoptionAinquestion46as
thefollowing.
WHEREjob_id=(SELECTjob_idFROMemployeesWHEREemployee_id<210);
Whatwillbetheoutcomeofthischange?
A. Theresultswillbethesame
B. ORAerrorthrownonexecution
C. Theresultswilldiffer
D. Thequerywillexecutesuccessfullygiving0rows.
Answer:B.Thesubquerygivesmorethanoneresultonthegivenchangeandhenceamultirow
operatorshouldreplacethe"="inthemainquerygivenabove.
48.Examinethetablestructuresasshownintheexhibitbelow.
http://www.tutorialspoint.com/cgibin/printpage.cgi
21/55
8/19/2015
SubqueriestoSolveQueriesQuestions
Youneedtodisplaythenamesoftheemployeeswhohavethehighestsalary.WhichofthefollowingSQL
statementswillbecorrect?
. SELECTfirst_name,last_name,grade
FROMemployees,grade
WHERE(SELECTmax(salary)FROMemployees)BETWEENlosalandhisal;
. SELECTfirst_name,last_name,grade
FROMemployees,grade
WHERE(SELECTmax(salary)FROMemployees)BETWEENlosalandhisal
ANDsalaryBETWEENlosalandhisal;
. SELECTfirst_name,last_name,grade
FROMemployees,grade
WHEREsalary=(SELECTmax(salary)FROMemployees)
ANDsalaryBETWEENlosalandhisal;
. SELECTfirst_name,last_name,grade
FROMemployees,grade
WHEREsalaryIN(SELECTmax(salary)FROMemployees)
ANDmax(salary)BETWEENlosalandhisal;
Answer:B,C.Thesubqueriescanbewrittenoneithersideoftheoperator
49.WhatisthesubqueryintheFROMclauseofanSQLstatement?
C hoosethemostappropriateanswer
http://www.tutorialspoint.com/cgibin/printpage.cgi
22/55
8/19/2015
SubqueriestoSolveQueriesQuestions
A. Singlerowsubquery
B. Multirowsubquery
C. InlineView
D. Corelatedsubquery
Answer:C.IfasubqueryappearsintheFROMclauseoftheSELECTstatements,itformsanInline
view.Oracleinternallycreatesatemporaryviewforthequeryexecution.
50.WhatisthemaximumnumberofnestinglevelallowedinanInlineViewtypesub
query?
A. 255
B. 300
C. 216
D. Unlimited
Answer:D.Asthereisnolimitonthenumberoftableswhichcanbejoined,thereisnolimitonthe
numberofinlineviewinaquery.
51.Whatistrueaboutcorelatedsubqueries?
A. Thetablesusedinthemainqueryarealsousedinacorelatedsubquery
B. Thesubquerieswhichreferenceacolumnusedinthemainqueryarecalledcorelatedsubqueries
C. Thesubquerieswhicharewrittenwithoutparenthesisarecalledcorelatedsubqueries
D. Thesubquerieswhichmandatorilyusedifferenttablesthanthoseusedinthemainqueryarecalled
corelatedsubqueries
Answer:B.Correlatedsubqueryreferencesacolumnintheouterqueryandexecutesthesubqueryonce
foreveryrowintheouterquerywhileUncorrelatedsubqueryexecutesthesubqueryfirstandpassesthe
valuetotheouterquery.
52.Whichofthefollowingstatementscannotbeparentstatementsforasubquery?
A. SELECT
B. GROUPBY
C. UPDATE
D. DELETE
Answer:B.Therestoftheoptionscanbeinthemainqueryparentquery ofasubquery.
53.Whatistrueaboutacorelatedsubquery?
http://www.tutorialspoint.com/cgibin/printpage.cgi
23/55
8/19/2015
SubqueriestoSolveQueriesQuestions
A. Itisevaluatedonlyoncefortheparentquery
B. Itisevaluatedonlythricefortheparentquery
C. Itisevaluatedonceforeachrowprocessedbytheparentsubquery
D. Alloftheabove
Answer:C.Correlatedsubqueryreferencesacolumnintheouterqueryandexecutesthesubqueryonce
foreveryrowintheouterqueryandtheEXISTSoperatorisusedtotestwhethertherelationshiporlink
ispresent.
54.Examinethegiventablestructure.Youneedtowriteaquerywhichreturnsthenames
oftheemployeeswhosesalariesexceedtheirrespectivedepartment'saveragesalary.
Whichofthefollowingwillwork? C hoosethemostappropriateanswer
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
. SELECTemployee_id,first_name,last_name
FROMemployeese
WHEREsalary>(SELECTavg(salary)
FROMemployees
WHEREe.department_id=department_id)
ORDERBYdepartment_id;
. SELECTemployee_id,first_name,last_name
FROMemployeese
WHEREsalary>ANY(SELECTavg(salary)
FROMemployees
WHEREe.department_id=department_id)
ORDERBYdepartment_id;
. SELECTemployee_id,first_name,last_name
FROMemployeese
WHEREsalary=(SELECTavg(salary)
FROMemployees
WHEREe.department_id=department_id)
ORDERBYdepartment_id;
http://www.tutorialspoint.com/cgibin/printpage.cgi
24/55
8/19/2015
SubqueriestoSolveQueriesQuestions
. SELECTemployee_id,first_name,last_name
FROMemployeese
WHEREsalary<ANY(SELECTavg(salary)
FROMemployees
WHEREe.department_id=department_id)
ORDERBYdepartment_id;
Answer:A.HerethedepartmentIDisobtained,usedtoevaluatetheparentqueryandifthesalaryin
thatrowisgreaterthantheaveragesalaryofthedepartmentsofthatrow,thatresultisreturned.
55.Examinethegiventablestructure.Whichofthefollowingquerieswilldisplay
duplicaterecordsinatableEMPLOYEES?
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
. SELECT*
FROMemployeesE
WHEREexists(SELECT1FROMemployeesE1
WHEREE.employee_id=E1.employee_id);
. SELECT*
FROMemployeesE
WHEREexists(SELECT1FROMemployeesE1
WHEREE.employee_id=E1.employee_id
ANDE.ROWID<E1.ROWID);
. SELECT*
FROMemployeesE
WHEREexists(SELECT1FROMemployeesE1
WHEREE.ROWID<E1.ROWID);
. SELECT*
FROMemployeesE
WHERE=ANY(SELECT1FROMemployeesE1
WHEREE.employee_id=E1.employee_id
http://www.tutorialspoint.com/cgibin/printpage.cgi
25/55
8/19/2015
SubqueriestoSolveQueriesQuestions
AndE.ROWID<E1.ROWID);
Answer:A.Correlatedsubqueryreferencesacolumnintheouterqueryandexecutesthesubqueryonce
foreveryrowintheouterqueryandtheEXISTSoperatorisusedtotestwhethertherelationshiporlink
ispresent.Itcanbeusedtofindtheduplicaterowsinatablewhereduplicityissubjectedtoacolumnor
setofcolumns.
ExaminethestructuresforthetablesDEPART MENT SandEMPLOYEESandanswerthe
questions56and57thatfollow.
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SQL>DESCdepartments
Name
Null? Type
DEPARTMENT_ID
NOTNULLNUMBER(4)
DEPARTMENT_NAME
NOTNULLVARCHAR2(30)
MANAGER_ID
NUMBER(6)
LOCATION_ID
NUMBER(4)
56.Whichofthefollowingquerieswilldisplaythesystemdateandcountofrecordsinthe
DEPART MENT SandEMPLOYEEStable?
. SELECTsysdate,
FROMDUAL;
(SELECT*FROMdepartments)dept_count,
(SELECT*FROMemployees)emp_count
. SELECTsysdate,
(SELECTcount(*)FROMdepartments)dept_count,
(SELECTcount(*)FROMemployees)emp_count
FROMDUAL
GROUPBYdepartment_id;
. SELECTsysdate,
(SELECT*FROMdepartments)dept_count,
http://www.tutorialspoint.com/cgibin/printpage.cgi
26/55
8/19/2015
SubqueriestoSolveQueriesQuestions
(SELECT*FROMemployees)emp_count
FROMDUAL
GROUPBYemployee_id;
. SELECTsysdate,
FROMDUAL;
(SELECTcount(*)FROMdepartments)dept_count,
(SELECTcount(*)FROMemployees)emp_count
Answer:D.Asinglerowsubquerycanalsobenestedintheouterquery'sSELECTclause.Inthiscase,
thevaluethesubqueryreturnsisavailableforeveryrowofoutputtheouterquerygenerates.Typically,
thistechniqueisusedtoperformcalculationswithavalueproducedfromasubquery.
57.Whichofthefollowingquerieswilltellwhetheragivenemployeeisamanagerina
Company'XYZ'?
. SELECTemployee_id,manager_id
FROMemployeesA
WHEREemployee_idANY(SELECTmanager_idfromemployeesB)
ORDERBYmanager_iddesc;
. SELECTemployee_id,manager_id
FROMemployeesA
WHEREemployee_id<ALL(SELECTmanager_idfromemployeesB)
. SELECTemployee_id,manager_id
FROMemployeesA
WHEREemployee_idIN(SELECTmanager_idfromemployeesB)
ORDERBYmanager_iddesc;
. SELECTemployee_id,manager_id
FROMemployeesA
WHEREemployee_idin(SELECTmanager_idfromemployeesB)
GROUPBYdepartment_id;
Answer:C.
Examinetheexhibitandanswerthequestion58thatfollows:
http://www.tutorialspoint.com/cgibin/printpage.cgi
27/55
8/19/2015
SubqueriestoSolveQueriesQuestions
58.Whichofthefollowingquerieswillgiveyoumaximumsalaryofanemployeeina
particularcity?
. SELECTmax(salary),city
FROM
(SELECTsalary,department_id,loc,city
FROMemployeesnaturaljoindepartmentsnaturaljoinlocations);
. SELECTsalary,city
FROM
(SELECTsalary,department_id,loc,city
FROMemployeesnaturaljoindepartmentsnaturaljoinlocations);
. SELECTmax(salary),city
FROM
(SELECTsalary,department_id,loc,city
FROMemployeesnaturaljoindepartmentsnaturaljoinlocations)
GROUPBYcity;
. SELECTmax(avg(salary)),city
FROM
http://www.tutorialspoint.com/cgibin/printpage.cgi
28/55
8/19/2015
SubqueriestoSolveQueriesQuestions
(SELECTsalary,department_id,loc,city
FROMemployeesnaturaljoindepartmentsnaturaljoinlocations);
Answer:C.Whenamultiplecolumnsubqueryisusedintheouterquery'sFROMclause,itcreatesa
temporarytablethatcanbereferencedbyotherclausesoftheouterquery.Thistemporarytableismore
formallycalledaninlineview.Thesubquery'sresultsaretreatedlikeanyothertableintheFROMclause.
Ifthetemporarytablecontainsgroupeddata,thegroupedsubsetsaretreatedasseparaterowsofdataina
table.
Examinethetablestructuresasgivenbelow.
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SQL>DESCdepartments
Name
Null? Type
DEPARTMENT_ID
NOTNULLNUMBER(4)
DEPARTMENT_NAME
NOTNULLVARCHAR2(30)
MANAGER_ID
NUMBER(6)
LOCATION_ID
NUMBER(4)
Considerthefollowingqueryandanswerthequestionsthat59to62thatfollow.
SELECTdepartment_name
FROMdepartmentsdINNERJOINemployeese
ON(d.employee_id=e.employee_id)
GROUPBYdepartment_name;
59.Whichofthefollowingqueriescanreplacetheabovequerybyusingsubqueries
givingthesameresult?
. SELECTdepartment_name
FROMdepartments
WHEREdepartment_id=ANY(SELECTdepartment_idFROMemployees);
. SELECTdepartment_name
http://www.tutorialspoint.com/cgibin/printpage.cgi
29/55
8/19/2015
SubqueriestoSolveQueriesQuestions
FROMdepartments
WHEREdepartment_idIN(SELECTdistinct(department_id)FROMemployees);
. SELECTdepartment_name
FROMdepartments
WHEREdepartment_id=(SELECTdistinct(department_id)FROMemployees);
. SELECTdepartment_name
FROMdepartments
WHEREdepartment_idANY(SELECTdistinct(department_id)FROMemployees);
Answer:A,B.
60.Assumethatthesubqueryasshowninthequerygivenaboveismodifiedtothe
following.
(SELECTdistinct(department_id)FROMemployeesORDERBYdepartment_id);
Whatwillbetheoutcomeasaresultofthischange?C hoosethemostappropriateanswer
A. Itwillorderthedepartment_idfetchedfromthesubqueryanddisplaytheminascendingorder
B. ItwillthrowanORAerrorastheORDERBYclauseshouldbeaccompaniedbytheGROUPBY
clause
C. ItwillthrowanORAerrorbecauseanORDERBYclausecannotbeusedinsideasubquery
D. Itwillexecutesuccessfully.
Answer:C.Asubquery,exceptoneintheFROMclause,can'thaveanORDERBYclause.Ifyouneedto
displayoutputinaspecificorder,includeanORDERBYclauseastheouterquery'slastclause.
61.Assumethatthequerygivenaboveismodifiedasthebelowone.
SELECTdepartment_name
FROMdepartments
WHEREdepartment_id=ANY(SELECTdepartment_idFROMemployees)
ORDERBYdepartment_iddesc;
Whatwillbetheoutcomeasaresultofthischange?C hoosethemostappropriateanswer
A. Itwillorderthedepartment_idfetchedfromthesubqueryanddisplaytheminascendingorder
B. Itwillorderthedepartment_idfetchedfromthesubqueryanddisplaythemindescendingorder
C. ItwillthrowanORAerrorbecauseanORDERBYclausecannotbeusedinsideasubquery
D. Noneoftheabove
Answer:D.Asubquery,exceptoneintheFROMclause,can'thaveanORDERBYclause.Ifyouneedto
http://www.tutorialspoint.com/cgibin/printpage.cgi
30/55
8/19/2015
SubqueriestoSolveQueriesQuestions
displayoutputinaspecificorder,includeanORDERBYclauseastheouterquery'slastclause.
62.Whichofthefollowingcanbeusedtoorderresultsinasubquery?
A. ORDERBY
B. HAVING
C. GROUPBY
D. Alloftheabove
Answer:C.Bydefault,theGROUPBYclauseperformsorderinginasubquery.
Examinetheexhibitbelowandanswerthequestions63to65thatfollow:
Considerthefollowingquery:
SELECTau_id,au_title
FROMaudit
WHEREau_detailsin(SELECTau_details
FROMaudit
WHEREau_titlelike'S%')
ORDERBYau_title;
63.Whatwillbetheoutcomeofthequerygivenabove?
A. ItgivesallAU_IDandAU_TITLEsstartingwiththeletter'S%'
B. ItgivesallAU_IDandAU_TITLEsstartingwiththeletter'S%'orderedbythetitlesinascending
order
C. ItthrowsanORAerror
D. Itreturnsa0value
Answer:C.AcolumnwithaCLOB,BLOB,NCLOBoranARRAYcannotbeusedinasubquery.
64.Whatwillbetheoutcomeofthefollowingquery?
SELECT*
FROMemployees
WHEREsalaryBETWEEN(SELECTmax(salary)
http://www.tutorialspoint.com/cgibin/printpage.cgi
31/55
8/19/2015
SubqueriestoSolveQueriesQuestions
FROMemployees
WHEREdepartment_id=100)
AND(SELECTmin(salary)FROMemployeeswheredepartment_id=100);
Thisqueryreturnsanerror.Whatisthereasonfortheerror?
A. AGROUPBYclauseshouldbeusedasthefunctionMAXisused
B. BoththesubqueriescannotusethesamedepartmentIDinthesameouterquery
C. BETWEENoperatorcannotbeusedwithasubquery
D. SELECTclauseshouldhavecolumnsmentionedandnotaasterix
Answer:C.TheBETWEENoperatorcanbeusedwithinasubquerybutnotwithasubquery.
65.WhatistrueaboutusingNOT INwhenwritingquerieswithsubqueriesinthem?
A. NOTINignoresalltheNULLvaluesandgivesonlytheNOTNULLvalues
B. NOTINputsalltheNULLvaluesatthelastandgivestheNOTNULLtobedisplayedfirst
C. NOTINshouldbenotbeusedifaNULLvalueisexpectedintheresultset
D. NOTINisjustanegationoftheoperatorINandcanbechangedwithoutanycaveat.
Answer:C.SQLhandlesNULLvaluesinadifferentwayandhenceitisagoodpracticetoavoidNOTIN
iftheresultsetmightcontainaNULL.
Considerthefollowingtablestructuresandanswerthequestions66to72thatfollow:
http://www.tutorialspoint.com/cgibin/printpage.cgi
32/55
8/19/2015
SubqueriestoSolveQueriesQuestions
66.YouneedtofindoutthenamesandIDsofthedepartmentsinwhichtheleastsalary
isgreaterthanthehighestsalaryinthedepartment10.Whichofthefollowingqueries
willgivetherequiredresult.
. SELECTdepartment_id,min(salary)
FROMemployees
GROUPBYdepartment_id
HAVINGmin(salary)>
selectmax(salary)
FROMemployees
wheredepartment_id=10
. SELECTdepartment_id,min(salary)
FROMemployees
GROUPBYdepartment_id
HAVINGmin(salary)>ANY
selectmax(salary)
FROMemployees
. SELECTdepartment_id,min(salary)
FROMemployees
HAVINGmax(salary)<ANY
selectmin(salary)
FROMemployees
wheredepartment_id=10
. SELECTdepartment_id,min(salary)
FROMemployees
GROUPBYdepartment_id
HAVINGmin(salary)>ALL
selectmax(salary)
FROMemployees
wheredepartment_id=10
Answer:A.
67.Writeaquerytofindtheemployeeswhosesalaryisequaltothesalaryofatleastone
employeeindepartmentofid10. C hoosethebestanswer
. SELECTemployee_id,Salary
FROMemployees
WHEREsalaryin
http://www.tutorialspoint.com/cgibin/printpage.cgi
33/55
8/19/2015
SubqueriestoSolveQueriesQuestions
(
SELECTsalary
FROMemployees
wheredepartment_id=10
)
. SELECTemployee_id,Salary
FROMemployees
WHEREsalary=ANY
SELECTsalary
FROMemployees
wheredepartment_id=10
. SELECTemployee_id,Salary
FROMemployees
WHEREsalaryALL
SELECTsalary
FROMemployees
wheredepartment_id=10
. SELECTemployee_id,Salary
FROMemployees
WHEREsalary<ANY
SELECTsalary
FROMemployees
wheredepartment_id=10
Answer:A,B.
68.Youneedtofindoutalltheemployeeswhohavesalarygreaterthanatleastone
employeeinthedepartment10.Whichofthefollowingquerieswillgiveyoutherequired
output?
. SELECTemployee_id,Salary
FROMemployees
WHEREsalary>=ANY
SELECTsalary
FROMemployees
wheredepartment_id=10
. SELECTemployee_id,Salary
FROMemployees
http://www.tutorialspoint.com/cgibin/printpage.cgi
34/55
8/19/2015
SubqueriestoSolveQueriesQuestions
WHEREsalary>ANY
SELECTsalary
FROMemployees
wheredepartment_id=10
. SELECTemployee_id,Salary
FROMemployees
WHEREsalary<ANY
SELECTsalary
FROMemployees
wheredepartment_id=10
. SELECTemployee_id,Salary
FROMemployees
WHEREsalary=ALL
SELECTsalary
FROMemployees
wheredepartment_id=10
Answer:B.
69.Youneedtofindoutalltheemployeeswhohavesalarylesserthanthesalaryofallthe
employeesinthedepartment10.Whichofthefollowingquerieswillgiveyouthe
requiredoutput?
. SELECTemployee_id,Salary
FROMemployees
WHEREsalary>ALL
SELECTsalary
FROMemployees
wheredepartment_id=10
. SELECTemployee_id,Salary
FROMemployees
WHEREsalary=ALL
SELECTsalary
FROMemployees
wheredepartment_id=10
. SELECTemployee_id,Salary
http://www.tutorialspoint.com/cgibin/printpage.cgi
35/55
8/19/2015
SubqueriestoSolveQueriesQuestions
FROMemployees
WHEREsalary<ALL
SELECTsalary
FROMemployees
wheredepartment_id=10
. SELECTemployee_id,Salary
FROMemployees
WHEREsalary<ANY
SELECTsalary
FROMemployees
wheredepartment_id=10
Answer:C.Multiplerowsubqueriesreturnmorethanonerowofresults.Operatorsthatcanbeused
withmultiplerowsubqueriesincludeIN,ALL,ANY,andEXISTS.Multiplecolumnsubqueriesreturn
morethanonecolumntotheouterquery.Thecolumnsofdataarepassedtotheouterqueryinthesame
orderinwhichthey'relistedinthesubquery'sSELECTclause.
70.Youneedtofindoutalltheemployeeswhohavetheirmanageranddepartment
matchingwiththeemployeehavinganEmployeeIDof121or200.Whichofthe
followingquerieswillgiveyoutherequiredoutput?
. SELECTemployee_id,manager_id,department_id
FROMemployees
WHERE(manager_id,department_id)=ANY
selectmanager_id,
department_id
FROMemployees
whereemployee_idin(121,200)
. SELECTemployee_id,manager_id,department_id
FROMemployees
WHERE(manager_id,department_id)<ANY
selectmanager_id,
department_id
FROMemployees
whereemployee_idin(121,200)
. SELECTemployee_id,manager_id,department_id
FROMemployees
WHERE(manager_id,department_id)>ANY
http://www.tutorialspoint.com/cgibin/printpage.cgi
36/55
8/19/2015
SubqueriestoSolveQueriesQuestions
selectmanager_id,
department_id
FROMemployees
whereemployee_idin(121,200)
)
. SELECTemployee_id,manager_id,department_id
FROMemployees
WHERE(manager_id,department_id)in
selectmanager_id,
department_id
FROMemployees
whereemployee_idin(121,200)
Answer:A,D.Multiplerowsubqueriesreturnmorethanonerowofresults.Operatorsthatcanbeused
withmultiplerowsubqueriesincludeIN,ALL,ANY,andEXISTS.Multiplecolumnsubqueriesreturn
morethanonecolumntotheouterquery.Thecolumnsofdataarepassedtotheouterqueryinthesame
orderinwhichthey'relistedinthesubquery'sSELECTclause.
71.YouneedtofindthedepartmentnameofanemployeewithemployeeID200.Which
ofthefollowingquerieswillbecorrect? C hoosethemostappropriateanswer
. SELECTemployee_id,first_name,last_name,department_id,
(SELECTdepartment_name
FROMdepartmentsd,employeesE
WHEREd.department_id=e.department_id
Andemployee_id=200
)
FROMemployeese
. SELECTemployee_id,first_name,last_name,department_id,
(SELECTdepartment_ID
FROMdepartmentsd
WHEREd.department_id=department_id
)
FROMemployeese
WHEREemployee_id=200;
. SELECTemployee_id,first_name,last_name,department_id,
(SELECTdepartment_name
FROMdepartmentsd
WHEREd.department_id=e.department_id
Andemployee_id=200
)
FROMemployeese
. SELECTemployee_id,first_name,last_name,department_id,
http://www.tutorialspoint.com/cgibin/printpage.cgi
37/55
8/19/2015
SubqueriestoSolveQueriesQuestions
(SELECTdepartment_name
FROMdepartmentsd,employeeE
WHEREd.department_id=e.department_id
)
FROMemployeese
Answer:C.
72.YouneedtofindthehighestearningemployeewiththejobIDas'SA_REP'.Whichof
thefollowingquerieswillbecorrect? C hoosethemostappropriateanswer
. SELECTjob_id,employee_id,Salary
FROMemployeese
WHEREjob_id=
(
SELECTdistinctsalary
FROMemployeesE1
WHEREE.job_id=E1.job_id
ANDE.salary<=E1.salary
ANDjob_id='SA_REP'
. SELECTdepartment_id,employee_id,Salary
FROMemployeesE
WHERE1=
(
SELECTcount(distinctsalary)
FROMemployeesE1
WHEREE.job_id=E1.job_id
ANDE.salary<=E1.salary
ANDjob_id='SA_REP'
)
. SELECTdepartment_id,employee_id,Salary
FROMemployeesE
WHERE0=
(
SELECTcount(distinctsalary)
FROMemployeesE1
WHEREE.job_id=E1.job_id
ANDE.salary=E1.salary
ANDjob_id='SA_REP'
)
. SELECTdepartment_id,employee_id,Salary
FROMemployeesE
WHERE1=
(
SELECTsalary
FROMemployeesE1
WHEREE.job_id<E1.job_id
http://www.tutorialspoint.com/cgibin/printpage.cgi
38/55
8/19/2015
SubqueriestoSolveQueriesQuestions
ANDE.salary<=E1.salary
ANDjob_id='SA_REP'
)
Answer:B.
ConsidertheEMPLOYEEStablestructureasshownintheexhibitandanswerthe
questions73to77thatfollow:
73.Youneedtofindthejobwhichhasatleastoneemployeeinit.Whichofthefollowing
querieswillbecorrect? C hoosethemostappropriateanswer
. SELECTemployee_id,Job_id
FROMemployeesE
WHEREexists
(
SELECT1
FROMemployeesE1
WHEREE.job_id=E1.job_id)
. SELECTemployee_id,Job_id
FROMemployeesE
WHEREexists
(
SELECT*
FROMemployeesE1
WHEREE.job_id=E1.job_id)
. SELECTemployee_id,Job_id
FROMemployeesE
WHEREnotexists
(
SELECT*
FROMemployeesE1
WHEREE.job_id=E1.job_id)
http://www.tutorialspoint.com/cgibin/printpage.cgi
39/55
8/19/2015
SubqueriestoSolveQueriesQuestions
. SELECTemployee_id,Job_id
FROMemployeesE
WHEREexists
(
SELECT1
FROMemployeesE1
WHEREE.job_id<E1.job_id)
Answer:A.TheEXISTSoperatorisusedtocheckandmatchrecordsbetweenqueries.Itreturnsa
BOOLEANvalue.Correlatedsubqueryreferencesacolumnintheouterqueryandexecutesthesubquery
onceforeveryrowintheouterqueryandtheEXISTSoperatorisusedtotestwhethertherelationshipor
linkispresent.AnUncorrelatedsubqueryexecutesthesubqueryfirstandpassesthevaluetotheouter
query.
74.Youneedtofindthejobwhichhasnoemployeesinit.Whichofthefollowingqueries
willbecorrect? C hoosethemostappropriateanswer
. SELECTemployee_id,Job_id
FROMemployeesE
WHEREexists
(
SELECT*
FROMemployeesE1
WHEREE.job_id=E1.job_id)
. SELECTemployee_id,Job_id
FROMemployeesE
WHEREnotexists
(
SELECT1
FROMemployeesE1
WHEREE.job_id=E1.job_id)
. SELECTemployee_id,Job_id
FROMemployeesE
WHEREnotexists
(
SELECT*
FROMemployeesE1
WHEREE.job_id=E1.job_id)
. SELECTemployee_id,Job_id
FROMemployeesE
WHEREexists
(
SELECT1
FROMemployeesE1
WHEREE.job_id<E1.job_id)
http://www.tutorialspoint.com/cgibin/printpage.cgi
40/55
8/19/2015
SubqueriestoSolveQueriesQuestions
Answer:B.TheNOTEXISTSisthenegationoperatorforEXISTS.
75.Youneedtofindthe3rdmaximumsalaryfromtheEMPLOYEEStable.Whichofthe
followingquerieswillgiveyoutherequiredresults? C hoosethemostappropriateanswer
. SELECT*
FROMemployeesE
WHEREsalary=(SELECTcount(distinctsalary)
FROMemployees
WHEREe.salary=salary
);
. SELECT*
FROMemployeesE
WHERE1=(SELECTcount(distinctsalary)
FROMemployees
WHEREe.salary<salary
);
. SELECT*
FROMemployeesE
WHERE2=(SELECTcount(distinctsalary)
FROMemployees
WHEREe.salary>salary
);
. SELECT*
FROMemployeesE
WHERE3=(SELECTcount(distinctsalary)
FROMemployees
WHEREe.salary<=salary
);
Answer:D.
76.Youneedtofindthemaximumsalarybyusingtheuserinputforgettingthevalueof
N.Whichofthefollowingquerieswillgiveyoutherequiredresults?
C hoosethemostappropriateanswer
. SELECTsalaryFROM
SELECTrowidasuser_sal
FROM(SELECTdistinctsalaryfromemployeesORDERBYsalarydesc)
)
WHEREuser_sal=&N;
. SELECTsalaryFROM
(
SELECTrownumasuser_sal
http://www.tutorialspoint.com/cgibin/printpage.cgi
41/55
8/19/2015
SubqueriestoSolveQueriesQuestions
FROM(SELECTdistinctsalaryFROMemployees
)
WHEREuser_sal<=&N;
GROUPBYsalary)
. SELECTsalaryFROM
SELECTrownumasuser_sal,salary
FROM(SELECTdistinctsalaryFROM
employees
ORDERBYsalarydesc)
)
WHEREuser_sal=&N;
. SELECTsalaryFROM
SELECTmax(rownum)asuser_sal,salaryFROM(SELECTdistinctsalaryFROM
employees
ORDERBYsalarydesc)
)
WHEREuser_sal=&N;
Answer:C.ROWNUMisapseudocolumnusedforfindingthenthorderresults.
77.Whatwillhappenifavalueisprovidedtothe&Nvariableintheabovequery
optionC inquestion76 doesnotmatchwithanyrow? C hoosethebestanswer
A. ThestatementwouldthrowanORAerror
B. Thestatementwouldreturnalltherowsinthetable
C. ThestatementwouldreturnNULLastheoutputresult.
D. Thestatementwouldreturnnorowsintheresult.
Answer:D.
78.WhatisthemaximumleveluptowhichSubqueriescanbenested?
A. 255
B. 100
C. 2
D. 16
Answer:A.
79.WhatistrueabouttheEXIST SoperatorinSQLquerieswithrespecttosubqueries?
A. Thecolumnsselectedinthesubqueriesareimportant
B. Theinnerquery'sshouldreturnrows,anyresultiswhatisimportant,notwhatisSELECTED
C. BothAandB
http://www.tutorialspoint.com/cgibin/printpage.cgi
42/55
8/19/2015
SubqueriestoSolveQueriesQuestions
D. NeitherAnorB
Answer:B.
80.WhatistrueabouttheANYoperatorusedforsubqueries?
A. Returnsrowsthatmatchallthevaluesinalist/subquery
B. Returnsrowsthatmatchthefirst5valuesinalist/subquery
C. Returnsrowsthatmatchanyvalueinalist/subquery
D. Returnsthevalue0whenalltherowsmatchinalist/subquery
Answer:C.
81.WhatistrueabouttheALLoperatorusedforsubqueries?
C hoosethemostappropriateanswer.
A. Returnsrowsthatmatchallthevaluesinalist/subquery
B. Returnsrowsthatmatchonlysomevaluesinalist/subquery
C. Returnsrowsonlyifallthevaluesmatchinalist/subquery
D. Alloftheabove
Answer:C.'>ALL'Morethanthehighestvaluereturnedbythesubquery.'<ALL'Lessthanthelowest
valuereturnedbythesubquery.'<ANY'Lessthanthehighestvaluereturnedbythesubquery.'>ANY'
Morethanthelowestvaluereturnedbythesubquery.'=ANY'Equaltoanyvaluereturnedbythe
subquerysameasIN .'[NOT]EXISTS'Rowmustmatchavalueinthesubquery.
82.WhatistrueaboutusingsubqueriesinINSERT statementsinOracle?
A. TheycanbeusedintheINSERTclausewithoutanyrestriction
B. TheycanbeusedintheINSERTclauseonlyforNumericvalues
C. TheSELECTlistofasubqueryshouldbethesameasthecolumnlistoftheINSERTstatement.
D. Noneoftheabove
Answer:C.
Examinethetablestructuresasgivenbelowandanswerthequestions83to86that
follow.
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
http://www.tutorialspoint.com/cgibin/printpage.cgi
43/55
8/19/2015
SubqueriestoSolveQueriesQuestions
PHONE_NUMBER
HIRE_DATE
JOB_ID
SALARY
COMMISSION_PCT
MANAGER_ID
DEPARTMENT_ID
NOTNULLVARCHAR2(25)
VARCHAR2(20)
NOTNULLDATE
NOTNULLVARCHAR2(10)
NUMBER(8,2)
NUMBER(2,2)
NUMBER(6)
NUMBER(4)
SQL>DESCdepartments
Name
Null? Type
DEPARTMENT_ID
NOTNULLNUMBER(4)
DEPARTMENT_NAME
NOTNULLVARCHAR2(30)
MANAGER_ID
NUMBER(6)
LOCATION_ID
NUMBER(4)
83.YouneedtofindthedetailsofallemployeeswhowerehiredforthejobID'SA_REP'in
themonthofJune,2013.Whichofthefollowingquerieswillgivetherequiredresults?
C onsiderthetablestructureasgiven
. SELECTfirst_name
FROMemployees
WHEREemployee_id=
(
SELECTemployee_id
FROMemployees
WHEREto_char(hiredate,'MM/YYYY')='02/1981'
ANDjob_id='SA_REP'
);
. SELECTfirst_name
FROMemployees
WHEREemployee_id=ANY
(
SELECTemployee_id
FROMemployees
WHEREto_char(hiredate,'MM/YYYY')='02/1981'
ANDjob_id='SA_REP'
);
. SELECTfirst_name
FROMemployees
WHEREemployee_idANY
(
SELECTemployee_id
FROMemployees
WHEREto_char(hiredate,'MM/YYYY')='02/1981'
ANDjob_id='SA_REP'
);
. SELECTfirst_name
FROMemployees
http://www.tutorialspoint.com/cgibin/printpage.cgi
44/55
8/19/2015
SubqueriestoSolveQueriesQuestions
WHEREemployee_idexists
(
SELECTemployee_id
FROMemployees
WHEREto_char(hiredate,'MM/YYYY')='02/1981'
ANDjob_id='SA_REP'
);
Answer:B.
84.Whichofthefollowingstatementsareequivalent?
. SELECTemployee_id,salary
FROMemployees
WHEREsalary<ALL(SELECTsalaryFROMemployeesWHEREdepartment_id=100);
. SELECTemployee_id,salary
FROMemployeesWHEREsalary<(SELECTmin(salary)FROMemployeesWHERE
department_id=100);
. SELECTemployee_id
FROMemployees
WHEREsalarynot>=ANY(SELECTsalaryFROMemployeesWHEREdepartment_id=100);
D. Noneoftheabove
Answer:A,B.
85.Considerthefollowingtwoqueries:
Query1:
SELECTfirst_name
FROMemployeesejoindepartmentsd
ONe.department_id=d.department_id
WHEREdepartment_name='ACCOUNTS';
Query2:
SELECTfirst_name
FROMemployeese
WHEREdepartment_id=ANY(SELECTdepartment_id
WHEREdepartment_name='ACCOUNTS');
FROMdepartmentsd
Whatcanbesaidaboutthetwostatements?
A. Boththequeriesshouldgeneratethesameresult.
B. Boththequerieswillthrowanerror.
C. Iftherearetwodepartmentswiththesamename,boththequerieswillfail.
D. Boththequerieswillrunsuccessfullyevenifthereismorethanonedepartmentnamed
http://www.tutorialspoint.com/cgibin/printpage.cgi
45/55
8/19/2015
SubqueriestoSolveQueriesQuestions
'ACCOUNTS'.
Answer:A,D.
86.Youneedtodisplayalltheemployeeswhohavethehighestsalaryinadepartment
100.Youfireaqueryasbelow.
SELECTE.first_name,E.last_name,E.salary
FROMemployeesE
WHEREE.salary>ALL(SELECTE1.salary
FROMemployeesE1
WHEREE.department_id=E1.department_id
ANDE.department_id=100);
Whatwillbetheoutcomeoftheabovequery?
A. Itexecutessuccessfullyandgivestherequiredresults
B. Itexecutessuccessfullybutdoesn'tgivetherequiredoutput
C. ItthrowsanORAerroronexecution
D. Itexecutessuccessfullyandgivestherequiredresultwhen>ALLisreplacedwith>=ALL
Answer:B,D.>ALLwillnotgivetherequiredresultastheremaybetwoemployeeswiththesame
salaryandwhoarethehighestearnersinthedepartment100
Considertablestructuresasshownintheexhibitandanswerthequestions87to89that
follow:
http://www.tutorialspoint.com/cgibin/printpage.cgi
46/55
8/19/2015
SubqueriestoSolveQueriesQuestions
. SELECTE.first_name,job_id,salary
FROMemployeesE
WHEREsalary=
(SELECTmax(salary)
FROMemployeesE1
WHEREE1.department_id=100
GROUPBYjob_id)
ANDjob_id='SA_REP'
ORDERBYfirst_name;
. SELECTE.first_name,job_id,salary
FROMemployeesE
WHEREsalaryin
(SELECTmax(salary)
FROMemployeesE1
whereE1.department_id=100)
ORDERBYfirst_name;
. SELECTE.first_name,job_id,salary
FROMemployeesE
WHEREsalaryIN
(SELECTmax(salary)
FROMemployeesE1
wherejob_id='SA_REP'
GROUPBYjob_id)
ANDWHEREE.department_id=100
ORDERBYfirst_namedesc;
. SELECTE.first_name,job_id,salary
FROMemployeesE
WHEREsalaryIN
(SELECTmax(salary)
FROMemployeesE1
WHEREE1.department_id=100
GROUPBYjob_id)
ORDERBYfirst_name;
http://www.tutorialspoint.com/cgibin/printpage.cgi
47/55
8/19/2015
SubqueriestoSolveQueriesQuestions
Answer:C.
88.Inthequeriesgivenabove optionC isthecorrectanswer ,youneedtodisplayallthe
employeeswiththeJOBID'SA_REP'whohavethemaximumsalaryinthedepartment
100.Whichofthefollowingquerieswillgivetherequiredoutput?
. SELECTE.first_name,job_id,salary
FROMemployeesE
WHEREsalaryIN
(SELECTmax(salary)
FROMemployeesE1
WHEREE1.department_id=100
GROUPBYjob_id)
ANDjob_id='SA_REP'
ORDERBYfirst_name;
. SELECTE.first_name,job_id,salary
FROMemployeesE
WHEREsalaryin
(SELECTmax(salary)
FROMemployeesE1
WHEREE1.department_id=100)
ORDERBYfirst_name;
. SELECTE.first_name,job_id,salary
FROMemployeesE
WHEREsalaryin
(SELECTmax(salary)
FROMemployeesE1
WHEREjob_id='SA_REP'
GROUPBYjob_id)
AndWHEREE.department_id=100
ORDERBYfirst_namedesc;
. SELECTE.first_name,job_id,salary
FROMemployeesE
WHEREsalaryin
(SELECTmax(salary)
FROMemployeesE1
WHEREE1.department_id=100
GROUPBYjob_id)
ORDERBYfirst_name;
Answer:A.
89.Selectthequerywhichwillgiveyouthemaximumsalaryandmaximumcomm
percentage.T hequeryshouldalsogivethemaximumcommpercentagepaidifthe
highestsalariedemployeegetsthemaximumcommpercentage.
http://www.tutorialspoint.com/cgibin/printpage.cgi
48/55
8/19/2015
SubqueriestoSolveQueriesQuestions
. SELECTemployee_id,max(salary),max(commission_pct)
FROMemployeesE
GROUPBYsalary,commission_pct;
. SELECTemployee_id,max(salary),max(commission_pct)
FROMemployeesE
GROUPBYsalary;
. SELECTemployee_id,max(salary)
FROMemployeesE
GROUPBYsalary,commission_pct
HAVINGmax(commission_pct)=100;
. SELECTemployee_id,
(SELECTmax(salary)FROMemployees)*(SELECTmax(commission_pct)FROMemployees)
FROMDUAL;
Answer:D.Asinglerowsubquerycanalsobenestedintheouterquery'sSELECTclause.Inthiscase,
thevaluethesubqueryreturnsisavailableforeveryrowofoutputtheouterquerygenerates.Typically,
thistechniqueisusedtoperformcalculationswithavalueproducedfromasubquery.
90.WhatistrueaboutthesubqueriesusedintheSELECT clauseofanSQLstatement?
A. ThesesubqueriesarethesameinallaspectsasthoseusedintheFROMorWHEREclauses
B. Thesesubquerieshavetomandatorilybesinglerowsubqueries
C. Wecanusemultirowoperatorswhenwritingsuchsubqueries
D. Noneoftheabove
Answer:B.
91.Whatwillbetheoutcomeofthefollowingquery? C onsiderthetablestructureasgiven
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
http://www.tutorialspoint.com/cgibin/printpage.cgi
49/55
8/19/2015
SubqueriestoSolveQueriesQuestions
SELECTsysdate,
(SELECTmax(salary)FROMemployeesGROUPBYdepartment_id)
FROMDUAL;
A. Itgivesthesystemdateandthemaximumsalaryforeachdepartment
B. Itgivesthemaximumsalaryforallthedepartments
C. ItthrowsanORAerror
D. Itexecutessuccessfullywith0rows
Answer:C.AMultirowsubquerycannotbeusedintheSELECTclauseofanSQLstatement.Onlya
singlerowsubquerycanbenestedintheouterquery'sSELECTclause.
Examinethegiventablestructure.Considerthefollowingqueryandanswerthe
questions92to95thatfollow:
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SELECTsalary
FROMemployees
WHEREsalary>ALL(10,20,30);
92.Whichofthefollowingqueriesareequivalenttotheabovequery?
. SELECTsalary
FROMemployees
WHEREsalary>10orsalary>20andsalary>30;
. SELECTsalary
FROMemployees
WHEREsalary<10andsalary<20andsalary<30;
. SELECTsalary
FROMemployees
WHEREsalary>10andsalary>20andsalary>30;
http://www.tutorialspoint.com/cgibin/printpage.cgi
50/55
8/19/2015
SubqueriestoSolveQueriesQuestions
. SELECTsalary
FROMemployees
WHEREsalary>10andsalary>20orsalary<30;
Answer:C.ThequestionshowstheALLclauseinasimplifiedmannerwhenitisfollowedbyalist.
93.Ifintheabovequerythelist 10, 20, 30 isreplacedbyasubquery,whichofthe
followingquerieswillgivetherequiredoutputforthedepartmentnumber100?
. SELECTE.salary
FROMemployeesE
WHEREE.salary>(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
. SELECTE.salary
FROMemployeesE
WHEREE.salary>ALL(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
. SELECTE.salary
FROMemployeesE
WHEREE.salary=(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
. SELECTE.salary
FROMemployeesE
WHEREE.salary>=(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
Answer:B.ThequestionshowstheALLclauseinasimplifiedmannerwhenitisfollowedbyasubquery
94.Withrespecttothequestion14above,whatamongthefollowingwillbeanequivalent
queryifALLhastobereplacedwithANY?
. SELECTE.salary
FROMemployeesE
WHERENOTEXISTS(E.salary=ANY(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
. SELECTE.salary
FROMemployeesE
WHEREE.salary>ANY(SELECTE1.salary
http://www.tutorialspoint.com/cgibin/printpage.cgi
51/55
8/19/2015
SubqueriestoSolveQueriesQuestions
FROMemployeesE1
WHEREE1.department_id=100);
. SELECTE.salary
FROMemployeesE
WHEREE.salary=ANY(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
. SELECTE.salary
FROMemployeesE
WHERENOT(E.salary<=ANY(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100));
Answer:D.TheNOToperatorusedwhileusing'<=ANY'isusedfornegationoftheresultsreturnedby
thesubquery
95.Withrespecttothequestion94,iftheoperatorANYisnottobeused,whichofthe
followingquerieswillbecorrect?
. SELECTE.salary
FROMemployeesE
WHERENOTEXISTS(E.salary=ANY(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
. SELECTE.salary
FROMemployeesE
WHERENOTEXISTS(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100
AndE.salary<=E1.salary);
C. EitherAorB
D. Noneoftheabove
Answer:B.Correlatedsubqueryreferencesacolumnintheouterqueryandexecutesthesubqueryonce
foreveryrowintheouterqueryandtheEXISTSoperatorisusedtotestwhethertherelationshiporlink
ispresent.AnUncorrelatedsubqueryexecutesthesubqueryfirstandpassesthevaluetotheouterquery.
Examinethegiventablestructures.Considerthefollowingqueryandanswerthe
questions96to98thatfollow:
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
http://www.tutorialspoint.com/cgibin/printpage.cgi
52/55
8/19/2015
SubqueriestoSolveQueriesQuestions
FIRST_NAME
LAST_NAME
PHONE_NUMBER
HIRE_DATE
JOB_ID
SALARY
COMMISSION_PCT
MANAGER_ID
DEPARTMENT_ID
VARCHAR2(20)
NOTNULLVARCHAR2(25)
NOTNULLVARCHAR2(25)
VARCHAR2(20)
NOTNULLDATE
NOTNULLVARCHAR2(10)
NUMBER(8,2)
NUMBER(2,2)
NUMBER(6)
NUMBER(4)
SELECTsalary
FROMemployees
WHEREsalary>ANY(10,20,30);
96.Whichofthefollowingqueriesareequivalenttotheabovequery?
. SELECTsalary
FROMemployees
WHEREsalary>10orsalary>20andor>30;
. SELECTsalary
FROMemployees
WHEREsalary<10andsalary<20andsalary<30;
. SELECTsalary
FROMemployees
WHEREsalary>10andsalary>20orsalary>30;
. SELECTsalary
FROMemployees
WHEREsalary>10andsalary>20orsalary<30;
Answer:A.ThequestionshowstheANYclauseinasimplifiedmannerwhenitisfollowedbyalist.
97.Intheabovequery,ifthelist 10, 20, 30 isreplacedbyasubquery,whichofthe
followingquerieswillgivetherequiredoutputforthedepartmentnumber100?
. SELECTE.salary
FROMemployeesE
WHEREE.salary>(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
. SELECTE.salary
FROMemployeesE
WHEREE.salary>ANY(SELECTE1.salary
FROMemployeesE1
http://www.tutorialspoint.com/cgibin/printpage.cgi
53/55
8/19/2015
SubqueriestoSolveQueriesQuestions
WHEREE1.department_id=100);
. SELECTE.salary
FROMemployeesE
WHEREE.salary=(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
. SELECTE.salary
FROMemployeesE
WHEREE.salary>=(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
Answer:B.ThequestionshowstheANYclauseinasimplifiedmannerwhenitisfollowedbyasub
query
98.Withrespecttothequestion97above,whatamongthefollowingwillbeanequivalent
queryifANYisremoved?
. SELECTE.salary
FROMemployeesE
WHERENOTEXISTS(E.salary=ANY(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
. SELECTE.salary
FROMemployeesE
WHEREEXISTS(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100
AndE.salary>E1.salary);
. SELECTE.salary
FROMemployeesE
WHEREEXISTS(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100
);
. SELECTE.salary
FROMemployeesE
WHEREIN(SELECTE1.salary
FROMemployeesE1
WHEREE1.department_id=100);
Answer:B.TheEXISTSoperatorcansubstitutetheANYoperator.Correlatedsubqueryreferencesa
columnintheouterqueryandexecutesthesubqueryonceforeveryrowintheouterqueryandthe
http://www.tutorialspoint.com/cgibin/printpage.cgi
54/55
8/19/2015
SubqueriestoSolveQueriesQuestions
EXISTSoperatorisusedtotestwhethertherelationshiporlinkispresent.
99.Examinethegiventablestructure.Howmanyrowswillgetgeneratedifthesubquery
mentionedreturns0rows?
SQL>DESCemployees
Name
Null? Type
EMPLOYEE_ID
NOTNULLNUMBER(6)
FIRST_NAME
VARCHAR2(20)
LAST_NAME
NOTNULLVARCHAR2(25)
EMAIL
NOTNULLVARCHAR2(25)
PHONE_NUMBER
VARCHAR2(20)
HIRE_DATE
NOTNULLDATE
JOB_ID
NOTNULLVARCHAR2(10)
SALARY
NUMBER(8,2)
COMMISSION_PCT
NUMBER(2,2)
MANAGER_ID
NUMBER(6)
DEPARTMENT_ID
NUMBER(4)
SELECTE.salary
FROMemployeesE
WHEREE.salary>ANY(selectE1.salaryFROMemployeesE1whereE1.department_id=100);
A. 1row
B. Norows
C. EitherAorB
D. Noneoftheabove
Answer:B.Ifthesubqueryreturnszerorows,the'>ANY'conditionevaluatestoFALSE,hence"No
rows"arereturned.
100.Asubquerymustbeplacedintheouterquery'sHAVINGclauseif:
A. Theinnerqueryneedstoreferencethevaluereturnedtotheouterquery.
B. Thevaluereturnedbytheinnerqueryistobecomparedtogroupeddataintheouterquery.
C. Thesubqueryreturnsmorethanonevaluetotheouterquery.
D. Noneoftheabove.Subqueriescan'tbeusedintheouterquery'sHAVINGclause.
Answer:B.AHAVINGclauseisusedwhenthegroupresultsofaqueryneedtoberestrictedbasedon
somecondition.Ifasubquery'sresultmustbecomparedwithagroupfunction,youmustnesttheinner
queryintheouterquery'sHAVINGclause.
http://www.tutorialspoint.com/cgibin/printpage.cgi
55/55