Begning HTML
Begning HTML
/0,) *,
12*23245 62)27585)* "94*584
!" $%&'() *+),)-.
LecLure #4: Subquerles ln SCL
:)),.)/585)*4
! ro[ecL asslgnmenL 1 due Loday
! Pomework 1 released Loday
due nexL lrlday 2/8
SCL and 8elauonal Algebra
rakash 2013 v1 CS 4604 2
";<=*5
! MosL popular embedded db ln Lhe world
lphone (lCS), Androld, Chrome..
! (very) Lasy Lo use: no need Lo seL lL up
! Self-conLalned: daLa+schema
! u8 on your lapLop: useful for Lesung,
undersLandlng..
rakash 2013 v1 CS 4604 3
<=)52+ >,*20,) ?,+ @5A20,)2A :A753+2
! 8elauonal algebra expresslons can become very
long.
! use llnear noLauon Lo sLore resulLs of
lnLermedlaLe expresslons.
A relauon name and a parenLheslsed llsL of aurlbuLes
for LhaL relauon
use Answer as Lhe convenuonal name for Lhe nal
resulL
1he asslgnmenL symbol :=
Any expresslon ln relauonal algebra on Lhe rlghL
rakash 2013 v1 CS 4604 4
BC28DA5 ,? <=)52+ >,*20,)
! name palrs of sLudenLs who llve aL Lhe same
address.
! normal expresslon:
rakash 2013 v1 CS 4604 3
S1.Name,S2.Name
(
S1.Address=S2.Address
(
S1
(Students)
S2
(Students)))
BC28DA5 ,? <=)52+ >,*20,)
! normal expresslon:
! Llnear noLauon:
rakash 2013 v1 CS 4604 6
S1.Name,S2.Name
(
S1.Address=S2.Address
(
S1
(Students)
S2
(Students)))
Pairs(P1, N1, A1, P2, N2, A2) :=
S1
(Students)
S2
(Students)
Matched(P1, N1, A1, P2, N2, A2) :=
A1=A2
(Pairs(P1, N1, A1, P2, N2, A2))
Answer(Name1, Name2) :=
N1,N2
(Matched(P1, N1, A1, P2, N2, A2))
()*5+D+50)7 ;.5+=54 ()E,AE=)7
6.A0DA5 @5A20,)4
! SLLLC1 A, 8 l8CM 8, S WPL8L C,
! nesLed loops:
for each Luple L1 ln 8
for each Luple L2 ln S
lf Lhe aurlbuLes ln L1 and L2 sausfy C
ouLpuL Lhe Luples lnvolvlng aurlbuLes A and 8
rakash 2013 v1 CS 4604 7
()*5+D+50)7 ;.5+=54 ()E,AE=)7
6.A0DA5 @5A20,)4
! SLLLC1 A, 8 l8CM 8, S WPL8L C,
! Converslon Lo relauonal algebra:
CompuLe 8 x S
Apply selecuon operaLor o() Lo 8 x S
ro[ecL Lhe resulL Luples Lo aurlbuLes A and 8
rakash 2013 v1 CS 4604 8
A,B
(
C
(R S))
6,0E20,) ?,+ ".3F.5+=54
! llnd Lhe name of Lhe professor who Leaches CS
4604.
!"#"$% '()*
+,-. /012*331034 %*(56
78"," 9/:; < /012*3310/:;= >'; 9'?)@*0 <
ABCA= >'; 9;*DE'()* < $!=;
! uo we need Lo Lake Lhe naLural [oln of Lwo blg
relauons [usL Lo geL a relauon wlLh one Luple?
! Can we rewrlLe Lhe query wlLhouL uslng a [oln?
rakash 2013 v1 CS 4604 9
>540)7
! A query can be puL lnslde anoLher query
! MosL commonly ln Lhe WPL8L clause
! Someumes ln Lhe l8CM clause (dependlng on
Lhe soware)
! 1hls subquery ls execuLed rsL (lf posslble)
rakash 2013 v1 CS 4604 10
".3F.5+9 BC28DA5
! llnd Lhe name of Lhe professor who Leaches
CS 4604.
SLLLC1 name
l8CM rofessors
WPL8L lu =
(SLLLC1 rofessorlu
l8CM 1each
WPL8L (number = 4604) Anu (uepLname = CS)
),
! When uslng =, Lhe subquery musL reLurn a
slngle Luple
rakash 2013 v1 CS 4604 11
!,)-=0,)4 ()E,AE=)7 @5A20,)4
! SCL lncludes a number of operaLors LhaL apply Lo
a relauon and produce a boolean resulL.
! 1hese operaLors are very useful Lo apply on
resulLs of sub-querles.
rakash 2013 v1 CS 4604 12
!,)-=0,)4 ()E,AE=)7 @5A20,)4
! LeL 8 be a relauon and L be a Luple wlLh Lhe same seL
of aurlbuLes.
LxlS1S 8 ls Lrue lf and only lf 8 conLalns aL leasL one Luple.
L ln 8 ls Lrue lf and only lf L equals a Luple ln 8.
L > ALL 8 ls Lrue lf and only lf 8 ls unary (has one aurlbuLe)
and L ls greaLer Lhan every value ln 8.
Can use any of Lhe oLher ve comparlson operaLors.
lf we use <>, 8 need noL be unary.
L > An? 8 (whlch ls unary) ls Lrue lf and only lf L ls greaLer
Lhan aL leasL one value ln 8.
! We can use nC1 Lo negaLe LxlS1S, ALL, and An?.
rakash 2013 v1 CS 4604 13
".3F.5+=54 G4=)7 !,)-=0,)4
! llnd Lhe deparLmenLs of Lhe courses Laken by Lhe
sLudenL wlLh name Surl.
SLLLC1 uepLname
l8CM 1ake
WPL8L SLudenLlu (>
( SLLLC1 lu
l8CM SLudenLs
WPL8L (name = Surl)
),
rakash 2013 v1 CS 4604 14
!,++5A2*5- E4 G)/,++5A2*5-
! 1he prevlous subquerles dld noL depend on anyLhlng
ouLslde Lhe subquery
.and Lhus need Lo be execuLed [usL once.
1hese are called uncorrelaLed.
! A correlaLed subquery depends on daLa from Lhe
ouLer query
. and Lhus has Lo be execuLed for each row of Lhe ouLer
Lable(s)
rakash 2013 v1 CS 4604 13
!,++5A2*5- ".3F.5+=54
! llnd course names LhaL have been used for Lwo or more
courses.
SELECT CourseName
FROM Courses AS First
WHERE CourseName IN
(SELECT CourseName
FROM Courses
WHERE (Number <> First.Number)
AND (DeptName <> First.DeptName)
);
rakash 2013 v1 CS 4604 16
BE2A.20)7 !,++5A2*5- ".3F.5+=54
SELECT CourseName
FROM Courses AS First
WHERE CourseName IN
(SELECT CourseName
FROM Courses
WHERE (Number <> First.Number)
AND (DeptName <> First.DeptName)
);
! LvaluaLe query by looplng over Luples of llrsL, and for each
Luple evaluaLe Lhe subquery.
! Scoplng rules: an aurlbuLe ln a subquery belongs Lo one of
Lhe Luple varlables ln LhaL subquerys l8CM clause, or Lo
Lhe lmmedlaLely surroundlng subquery, and so on.
rakash 2013 v1 CS 4604 17
".3F.5+=54 =) H@I6 /A2.454
! Can use a subquery as a relauon ln a l8CM clause.
! We musL glve such a relauon an allas uslng Lhe AS keyword.
! LeL us nd dlerenL ways of wrlung Lhe query llnd Lhe
names of rofessors who have LaughL Lhe sLudenL whose
rsL name ls 'Surl'.
! 1he old way:
SLLLC1 rofessors.name
l8CM rofessors, 1ake, 1each, SLudenLs
WPL8L (rofessors.lu = 1each.rofessorlu)
Anu (1each.Coursenumber = 1ake.Coursenumber)
Anu (1each.uepLname = 1ake.uepLname)
Anu (1ake.SLudenLlu = SLudenL.lu)
Anu (SLudenL.name = Surl ),
rakash 2013 v1 CS 4604 18
! llnd Lhe names of (rofessors who have LaughL
(courses Laken by (sLudenL wlLh rsL name 'Surl'))).
SELECT Name
FROM Professors
WHERE PID IN
(SELECT ProfessorPID
FROM Teach
WHERE (Number, DeptName) IN
( SELECT Number, DeptName
FROM Take, Students
WHERE (StudentPID = PID) AND
(Students.Name = Suri%)));
rakash 2013 v1 CS 4604 19
G)+,AA=)7 =* ?.+*J5+
! SELECT Name
FROM Professors
WHERE PID IN
(SELECT ProfessorPID
FROM Teach
WHERE (Number, DeptName) IN
(SELECT Number, DeptName
FROM Take
WHERE StudentPID IN
(SELECT PID
FROM Students
WHERE Name = 'Suri %')));
rakash 2013 v1 CS 4604 20
21
:77+572*5 ID5+2*,+4
! /0123 456
! /0123 4 789:392/3; $6
A ls a column
! :1< 4 789:392/3; $6
! $=> 4 789:392/3; $6
! <$? 4$6
! <92 4$6
! CounL Lhe number of sallors
:@A@/3 /0123 456
BC0< :)&DE+- :
rakash 2013 v1 CS 4604
22
H=)- )285 2)- 275 ,? *J5 ,A-54*
42=A,+K4L
:@A@/3 :"-F)GHI <$? 4:")JH6
BC0< :)&DE+- :
! 1hls ls lllegal, buL why?
CannoL comblne a column wlLh a value
:@A@/3 :"-F)GHI :")JH
BC0< :)&DE+- :
KL@C@ :")JH M 4:@A@/3 <$? 4:N")JH6 BC0< :)&DE+- :N6
rakash 2013 v1 CS 4604
23
M@IGN OP 2)- Q:R(>M
! So far, aggregaLe operaLors are applled Lo all (quallfylng)
Luples.
Can we apply Lhem Lo each of several groups of Luples?
! Lxample: nd Lhe age of Lhe youngesL sallor for each raung
level.
ln general, we donL know how many raung levels exlsL, and whaL Lhe
raung values for Lhese levels are!
Suppose we know LhaL raung values go from 1 Lo 10, we can wrlLe 10
querles LhaL look llke Lhls:
SELECT MIN (S.age)
FROM Sailors S
WHERE S.rating = i
For i = 1, 2, ... , 10:
rakash 2013 v1 CS 4604
24
H=)- *J5 275 ,? *J5 9,.)754* 42=A,+ ?,+
52/J +20)7 A5E5A
:@A@/3 :"+)OFJI <92 4:")JH6 )- )JH
BC0< :)&DE+- :
>C01* !P :"+)OFJ
(1) 1he sallors Luples are puL lnLo same
raung groups.
(2) CompuLe Lhe Mlnlmum age for each
raung group.
:&% :F)GH C)OFJ $JH
NN 8Q-OF R ST"U
VW AQXXH+ Y TT"T
YT $+' V NT"T
VN $F%( Y NT"T
ZT !EX V [V"T
C)OFJ $JH
V NT"T
V [V"T
R ST"U
Y TT"T
Y NT"T
C)OFJ $JH
V NT"T
R ST"U
Y NT"T
(1)
(2)
rakash 2013 v1 CS 4604
H=)- *J5 275 ,? *J5 9,.)754* 42=A,+ ?,+ 52/J
+20)7 A5E5A *J2* J24 2* A524* S 85835+4
:@A@/3 :"+)OFJI <92 4:")JH6 )-
G&F)JH
BC0< :)&DE+- :
>C01* !P :"+)OFJ
L$=92> /0123456 \ W
1. 1he sallors Luples are puL lnLo
same raung groups.
2. LllmlnaLe groups LhaL have < 2
members.
3. CompuLe Lhe Mlnlmum age for
each raung group.
:&% :F)GH C)OFJ $JH
NN 8Q-OF R ST"U
VW AQXXH+ Y TT"T
YT $+' V NT"T
VN $F%( Y NT"T
ZT !EX V [V"T
C)OFJ $JH
V NT"T
V [V"T
R ST"U
Y TT"T
Y NT"T
C)OFJ <&F)JH
V NT"T
Y NT"T
rakash 2013 v1 CS 4604 23
26
;.5+=54 T=*J !"#$% '( 2)- )*+,-!
:@A@/3 789:392/3; ')+JH']D&-'
BC0< +HD)OEF]D&-'
KL@C@ ^Q)D&_`)OEF
>C01* !P J+EQa&FJ]D&-'
L$=92> J+EQa]^Q)D&_`)OEF
! 1he ')+JH']D&-' conLalns (l) aurlbuLe names (ll) Lerms wlLh
aggregaLe operauons (e.g., AvC (:")JH)).
! 1he aurlbuLe llsL (e.g., :"+)OFJ) ln ')+JH']D&-' musL be ln
J+EQa&FJ]D&-'.
! 1he aurlbuLes ln group-quallcauon musL be ln J+EQa&FJ]D&-'.
SELECT S.rating, MIN (S.age) as age
FROM Sailors S
GROUP BY S.rating
HAVING S.rating > 5
rakash 2013 v1 CS 4604