Documente Academic
Documente Profesional
Documente Cultură
SUBQUERY
SUBQUERIES
A subquery is a SELECT statement that is nested inside another SELECT statement or another subquery There is no restriction on the depth of such nesting A query containing subqueries is evaluated starting with the innermost subquery E ample!
Suppose
we want to list employees that earn the lowest salary This requires two queries" based on what we have learned so far
#irst we will find the minimum salary earned by an employee Then we will use the min salary value to list the names of employees who earn this salary
2
$e can combine these two steps in to a single query by nesting a subquery within a query
SUBQUERIES
&' #ind the minimum salary '& SELECT min(salary) #*+, Employee&' List names of employees that earn the min salary found in the previous step '& SELECT fname" lname #*+, Employee $0E*E salary 1 2.///&' 2se of subquery '& SELECT fname" lname #*+, Employee $0E*E salary 1 (SELECT min(salary) #*+, Employee)-
,in(Salary) 2./// #name Alicia 4oyce Ahmad Lname 3elaya English 4abbar
SUBQUERIES
normally uses aggregate functions The returned value is compared in the outer query using a comparison operator such as 1" 61" 7" 8" etc9 these values are from a single column" such as a list of order numbers" list of ma:ors" list of departments" etc9 +uter query uses a list comparison operator" such as ;<" or an comparison operator modified by A<= or ALL clause9 SELECT clause of the subquery must contain a single column name +uter query uses the E?;STS >eyword9
5
SUBQUERY SYNTAX
A subquery uses a SELECT statement ;t is enclosed within parentheses ;t may appear in the $0E*E clause or the 0A@;<A clause in the outer query A subquery cannot use the +*BE* C= clause The result of a subquery must be :oin compatible with $0E*E & 0A@;<A clause of the outer query" i9e9" the result must be comparable9
SUBQUERIES WITH IN
SUBQUERIES WITH IN
SUBQUERIES WITH IN
=ou can get the same result by writing a 4+;< query ;t is" however" desirable to use a subquery in this case because the result is from a single table
&' The equivalent 4+;< query '& SELECT lname" fname #*+, Employee" Bependent $0E*E employee9ssn 1 dependent9essn A<B dependent9se 1 G,H&' Chec> out the subquery and the :oin query with se 1 G#H '& &' Are the results the sameJ E plain9 '&
&' $e start with the innermost subquery '& SELECT pnumber #*+, Nro:ect $0E*E pname 1 O<ewbenefitsP&' The result is plugged into the ne t subquery '& SELECT essn #*+, $or>sMon $0E*E hours 7 1/ A<B pno ;< (%/)&' Then the final query is evaluated '& SELECT lname" fname #*+, Employee $0E*E ssn ;< (KKKLLIIII" KLID.5%21)-
1/
$0E*E 8e pression7 8comparison operator7 EA<= Q ALLF (subquery) The result of the subquery in this case is a list
&' List all employees that earn more than all of the employees in Bept9 . '& SELECT lname" fname #*+, Employee $0E*E salary 7 ALL (SELECT salary #*+, Employee $0E*E dno 1 .)&' Can you reformulate the subquery so that ALL is not required in the outer queryJ $hat will be the result if we replace 7 ALL by 1 ALLJ '&
11
<+T ;< (1"2"%) means 61 1and 61 2 and 61 % 61 A<= (1"2"%) means 61 1 or 612 or 61 %
Lname #name 3elaya Alicia <arayan *amesh English 4oyce 4abbar Ahmad Corg 4ames
&' List all employees that donHt have a dependent '& SELECT lname" fname #*+, Employee $0E*E ssn 61 ALL (SELECT essn #*+, Bependent)&' =ou will get the same result by replacing 61 ALL by <+T ;< $hat will be the result if you simply replace ALL by A<= in the above queryJ '&
1%
CORRELATED SUBQUERIES
;n correlated subqueries" the inner query depends on values provided by the outer query The inner query is e ecuted once for each row that might be selected by the outer query
&' List all employees who have wor>ed 1/ hours on any pro:ect '& SELECT lname" fname #*+, Employee $0E*E 1/ ;< (SELECT hours #*+, $or>sMon $0E*E ssn 1 essn)&' $hat is the equivalent :oin query for the aboveJ
15
USE of EXISTS
$0E*E E<+TF E?;STS (subquery) Chec>s for e istence of data R returns T*2E or #ALSE A subquery introduced with E?;STS is always a corelated subquery The SELECT list in the subquery is invariably '" because there is no need for selecting a column
#name 4ohn #ran>lin 4ennifer
&' List all employees that have one or more dependents '& Lname SELECT lname" fname Smith #*+, Employee $ong $0E*E E?;STS $allace (SELECT ' #*+, Bependent $0E*E ssn 1 essn)&' 0ow many times does the subquery e ecute in the above queryJ '&
1.
USE of EXISTS
&' List all employees that do not have any dependent '& SELECT lname" fname #*+, Employee $0E*E <+T E?;STS (SELECT ' #*+, Bependent $0E*E ssn 1 essn)Lname #name 3elaya Alicia <arayan *amesh English 4oyce 4abbar Ahmad Corg 4ames
1D
SELF TEST
List student name" ma:or and department name for all students List student name" ma:or" and department name for students that have a ANA 7 %9/
1I