SlideShare a Scribd company logo
SQL
1
Fall 2001 Database Systems 1
Nested Structures
SELECT Name
FROM Items
WHERE Iid IN (SELECT DISTINCT Iid FROM Bids)
 ¢¡¤£¦¥¨§  ©  ¨©   !!#%$#© ¢
')( ( 02143)5 6%7 89!7A@49B
'C C DFE G¢G%E BIH
8¦E P Q
RF9¢S47#@49B
'T C UV6V5 6¢7!1A9¢G#6 WX6%Y
`V9%P4a
'¢b T c9%dGA3#7A7 DV35 53 #7
'e b f g#9¢S!P
h9i#p%q
g%E 1A3#7A7A9
0)6¢3¢@I@¢5 6
'r ( g%E G#6 89!7A@49B
stVuXv s2w x uXy%€! t¦w x sV‚!ƒtx „†…†‡‚ˆ#€
‰¦ ‘2’ “”•– —V’ ˜ ’4™
‰¦ ‘2˜ “”•– —%d ™ e2™
‰¦ ‘¦• “”•¦’ —V’ d ’4“
‰¦ ‘d f)”)’ —e ™ gh™
‰¦ ‘¦™ f)”)’ —V’ ˜ ˜–
‰¦ ‘Ve f)”)˜ —%d • gV–
‰¦ ‘)g f)”)˜ —V˜ ’ d
‰¦ ‘¦“ f)”• —V˜ • ™
Fall 2001 Database Systems 2
Let Temp = SELECT Iid FROM Bids i†j¦kml nVo p
q¦r
qs
qVt
q¦u
SELECT Name FROM Items WHERE Iid IN (SELECT Iid FROM Bids)
Is Iid IN Temp?
Is I1 IN Temp?
v!wFxy{z v| } ~¨| } X€%‚ ƒ!„!…#€¢†#| „¢‡
ˆ)‰ ‰ Š2‹4Œ Ž¢ %‘!A’ ‘“
Pick a tuple from Items
YES!
Put this tuple in the output
v!wFxy{z v| } ~¨| } X€%‚ ƒ!„!…#€¢†#| „¢‡
ˆ” ” •¤– —!—%– “4˜™š ›¤‘¢œAA’I‘“
Is Iid IN Temp?
Is I2 IN Temp?
Pick another tuple from Items
YES!
Put this tuple in the output
v!wFxy{z v| } ~¨| } X€%‚ ƒ!„!…#€¢†#| „¢‡
ˆ ” ž)Ž) Ž¢#‹4‘%—AŽ ŸX 
Is Iid IN Temp?
Is I3 IN Temp?
Pick another tuple from Items
NO!
Remove this tuple from
the output
SQL
2
Fall 2001 Database Systems 3
Nested Structures – correlation
SELECT I.Name FROM Items I
WHERE I.Iid IN (SELECT DISTINCT B.Iid FROM Bids B
WHERE B.Buyid = I.Oid)
 ¢¡¤£¦¥¨§  ©  ©    !¢¢#$%$© '
(0) ) 132546 78 9@¢8BA @C
Pick a tuple from Items
Compute the
nested relation
DFEHGPI Q¤R S
T U
Is I1 in Temp? No, remove this tuple!
V¢W¤X¦Y¨` Va b ca b def g h¢i¢p$eq$a ir
s¦t t uwv x¢xv y5€‚¦ƒ „w…†B‡Bˆ‰…y
Pick a tuple from Items
Compute the
nested relation
’‘H“•” –¤— ˜
™ d
Is I2 in Temp? No, remove this tuple!
Fall 2001 Database Systems 4
Nested Structures – correlation
SELECT I.Name FROM Items I
WHERE I.Iid IN (SELECT DISTINCT B.Iid FROM Bids B
WHERE B.Buyid = I.Oid)
e$f¤g0hi ej k lmj k nopq r¢s¢t$ou$j sv
w¦x y z0{0| {}$~5€B{ {‚Fƒ3„
Pick a tuple from Items
Compute the
nested relation
…’†H‡•ˆ ‰¤Š ‹
Œ 
Is I3 in Temp? No, remove this tuple!
Ž¢¤¦‘¨’ Ž“ ” •H“ ” –¤—˜™ š¢›$œ$—$$“ ›¢ž
Ÿ  ¡ ¢£¢¤¤¥$¦$§‰§ ¨©¦ª ª¦ B§
Pick a tuple from Items
Compute the
nested relation
«’¬ ­P® ¯¤° ±
²´³
²µ
Is I4 in Temp? YES, keep this tuple!
SQL
3
Fall 2001 Database Systems 5
SQL - Nested Statements
• A nested sub-expression may refer to the tuples from the outer
relation
SELECT Buy.Name
FROM Buyers Buy
WHERE Buy.BuyId IN (
SELECT B.BuyID
FROM Bids B
WHERE B.Amount  20 AND
B.BuyID NOT IN (
SELECT B2.BuyID
FROM Bids B2
WHERE B2.Amount = 20
AND B2.Iid = B.Iid))
Names of buyers with a bid
greater than 20 on an item
for which the buyer has no
other bids less than 20.
Fall 2001 Database Systems 6
Nested Structures - Exists
SELECT
FROM
WHERE (NOT) EXISTS
(SELECT *
FROM R1, ..., Rm
WHERE P)
• Semantics: For each tuple of the outer query, execute
the inner query; if there is at least one (no) tuple in the
result of the inner query, then retrieve that tuple of the
outer query.
• This accounts for the “there exists” type of queries
SQL
4
Fall 2001 Database Systems 7
SQL Nested Statements
• Two more nested conditions:
– EXISTS ( SELECT … FROM … WHERE … ) is true if the
SFW clause returns a non-empty relation
– NOT EXISTS ( SELECT … FROM … WHERE … ) is true if
the SFW clause returns an empty relation
• Find the name of all buyers who have no bids with an amount less
than 50
SELECT Buy.Name
FROM Buyers Buy
WHERE NOT EXISTS (SELECT B.BuyId
FROM Bids B
WHERE B.Amount 50 AND
B.BuyID=Buy.BuyId)
Fall 2001 Database Systems 8
SQL - EXISTS
• Find the names of buyers who placed a bid on
all items with one or more bids
SELECT Buy.Name
FROM Buyers Buy
WHERE EXISTS (
SELECT B.Iid
FROM Bids B
WHERE Buy.BuyId = B.BuyId)
Is this correct?
Names of buyers who
have bid on an item
Find buyers such that there does not exist an
item with bids on which these buyers did not bid
SQL
5
Fall 2001 Database Systems 9
SQL - EXISTS
• Find the names of buyers who placed a bid on all items
with one or more bids
SELECT Buy.Name
FROM Buyers Buy
WHERE NOT EXISTS (
SELECT B.Iid
FROM Bids B
WHERE NOT EXISTS (
SELECT *
FROM Bids B2
WHERE B.Iid = B2.Iid AND
B2.Buyid = Buy.BuyId) )
bids for this item
by this buyer
items with bids, but
not by this buyer
either there are no bids
or this buyer has a bid
on everything
Fall 2001 Database Systems 10
Things to Think About
• Don’t use nested queries if they are not
needed.
– they make the query unnecessarily
complicated
– they make optimization harder
• If a query requires only “IN” and “EXISTS”,
then it probably can be done without nesting.
SQL
6
Fall 2001 Database Systems 11
Aggregate functions
• Count, Max, Min, Sum, and Avg are aggregate functions
that apply to the values of a set of tuples
• Find the total number of bids in the bids table
SELECT Count(*)
FROM Bids
• Find the average bid for the “Dipping Bird”.
SELECT Avg(B.Amount) AS AvgAmt
FROM Bids B, Items I
WHERE B.Iid = I.Iid AND I.Name = ‘Dipping Bird’
Fall 2001 Database Systems 12
Aggregate Functions
• Find the buyer(s) who placed the maximum bid on the “Dipping
Bird”
SELECT DISTINCT B2.Buyid
FROM Bids B2, Items I2
WHERE B2.Iid=I2.Iid AND
I2.Name = ‘Dipping Bird’ AND
B2.Amount = (SELECT Max(B.Amount)
FROM Bids B, Items I
WHEREB.Iid = I.Iid AND
I.Name = ‘Dipping Bird’)
SQL
7
Fall 2001 Database Systems 13
Grouping
SELECT a
FROM b
WHERE c
GROUP BY d
Create the Cartesian product of the relations in b
Find all tuples from the Cartesian product that satisfy the condition in c
(including any subexpressions)
For all distinct values of the columns listed in d, create a group that
contains the set of tuples with that value for “d”
Create the columns in a, which should start with columns in d and end
with aggregates that will apply to each group separately.
Fall 2001 Database Systems 14
Group by
• Find the average bid amount for each item, list
the item name, the average bid and the number
of bids
SELECT I.name,
avg(B.Amount) AS AvgBid,
count(*) AS Cnt
FROM Items I, Bids B
WHERE I.Iid = B.Iid
GROUP BY I.name
SQL
8
Fall 2001 Database Systems 15
 ¢¡¤£ ¥§¦ ¨ © ¦ ¨ ¥¢ !¨ $#%¤'( )0¦ ¨ 123# 4¤%¤5¤ ¦ %!'
687 9§@ A¢BC3D E@ 68F @HG IP@ QRTSU VW 63X¤WTYX3`
687 9§F A¢BC3D E!a 6G b8G IcC dXegf S(W(W hiS!U US W
687 98C A¢BC8@ E@ 63a @HA IP@ QRTSU VW 63X¤WTYX3`
687 9¢a pB!@ E3b 6G q§G IP@ r7 f V 63X¤WTYX3`
687 98G pB!@ E@ 68F F!D IP@ QRTSU VW 63X¤WTYX3`
687 9b pB!F E!a 63a qD IcC dXegf S(W(W hiS!U US W
687 9¢q pB!F EF 68@ a IPF h$7 f¤f!7 `tsu6 vwXx WTYX3`
687 98A pBC EF 6C G IPF h$7 f¤f!7 `tsu6 vwXx WTYX3`
First Step
• After the FROM/WHERE clause before grouping
Fall 2001 Database Systems 16
y€ ‚$ƒ „†…‡§ˆ ‰ƒ y‘ ƒt’ “”ƒ •—–˜†™ de y8f¢e3g(f§h
y€ ‚‡ „†…‡ƒ ‰ƒ y†i ƒ„ “”ƒ •—–˜†™ de y8f¢e3g(f§h
y€ ‚i’ j…§ƒ ‰ƒ y‘ ‘8ˆ “”ƒ •—–˜†™ de y8f¢e3g(f§h
kl m†n op8q rq ks t uwv x2y zz8y {¤|~} c€8!‚!ƒ(€†{
}y „i… †‡ˆ ‰v }iˆ Š uwv x2y zz8y {¤|~} c€8!‚!ƒ(€†{
‹Œ $Ž †¢‘§’ “¢” ‹†• –i• —˜‘ ™š¢›œž!ŸŸ  $ž†¡ ¡ž 3Ÿ
‹Œ – ¢Ž “¢” ‹8” £’ —˜‘ ™š¢›œž!ŸŸ  $ž†¡ ¡ž 3Ÿ
¤¥ ¦†§ ¨†©8ª «§¬ ¤†­ ®i­ ¯wª °8¥ ±² ¤†³!´!µ ³8¶
Second Step
• After the group by
SQL
9
Fall 2001 Database Systems 17
Third Step
• The SELECT statement creates a single tuple for each
group.
• The aggregates functions are applied to each group
individually.
 ¢¡¤£¦¥¨§©  ¦!$# % $'!(
)103254 687 9A@$B C¤@ D
E¦F GHG8F I!PRQ S B T U
V5WYXG 27!7 C¤@$B T U
` F G 6 @$T 9
Fall 2001 Database Systems 18
GROUP BY HAVING
• Find the items with 2 or more bids and with the average
bid greater than $20, for each item list the item name
SELECT I.name
FROM Items I, Bids B
WHERE I.Iid = B.Iid
GROUP BY I.name
HAVING avg(B.Amount)  20 AND
count(DISTINCT B.Bid) = 2
• Find all buyer and item pairs where the buyer placed
more than one bid on the same item
SELECT B.buyid, B.iid
FROM Bids B
GROUP BY B.buyid, B.iid
HAVING count(B.bid)  1
SQL
10
Fall 2001 Database Systems 19
• Find owners who have at least two items with
average bids greater than $20 for each item. List the
owner identifiers and the names of the items.
SELECT I2.Oid, I2.Name
FROM Items I2
WHERE I2.Oid IN (
SELECT I.Oid
FROM Items I
WHERE I.Iid IN (
SELECT B.Iid
FROM Bids B
GROUP BY B.Iid
HAVING Avg(Amount) 20)
GROUP BY I.Oid HAVING Count(*) = 2)
AND I2.Iid IN (
SELECT B.Iid
FROM Bids B
GROUP BY B.Iid
HAVING Avg(Amount) 20)
Fall 2001 Database Systems 20
ORDER BY
• Follows group by and orders the results in ascending or
descending order
• Example: Find buyer and item pairs in which the buyer has
more than one bid for the item, order the result in descending
order of buyer ids
SELECT B.buyid, B.iid
FROM Bids B
GROUP BY B.buyid, B.iid HAVING count(B.bid)  1
ORDER BY B.buyid DESC
SQL
11
Fall 2001 Database Systems 21
SELECT complete
• First the Cartesian product of all tables in the from clause is formed
• From this, rows not satisfying the where condition are eliminated
• The remaining rows are grouped in accordance with the group by
clause
• Groups not satisfying the having clause then eliminated
• The expressions of the select clause target list are evaluated
• If the key word distinct is present, duplicate rows are now
eliminated
• The union is taken after each Subselect is evaluated
• Finally, the set of all result rows is sorted if an order by clause is
present.
Fall 2001 Database Systems 22
Join types
• It is possible to define a join in the from clause
– INNER JOIN: regular join from relational algebra
where only tuples from the two tables satisfying the
join condition are chosen
– LEFT OUTER JOIN: (regular join) union (tuples from
the table to the left that did not join with any tuples,
padded with null values for the new attributes)
– RIGHT OUTER JOIN: (regular join) union (tuples from
the table to the right that did not join with any tuples,
padded with null values for the new attributes)
– FULL JOIN = LEFT RIGHT OUTER JOIN
SQL
12
Fall 2001 Database Systems 23
Join Examples
• Find names of all items with bids
SELECT DISTINCT Items.name
FROM Items INNER JOIN Bids
ON Items.Iid = Bids.Iid
• Find all items and the total number of bids on them
SELECT Items.Iid, Count(Bids.Bid)
FROM Items LEFT OUTER JOIN Bids
ON Items.Iid = Bids.Iid
GROUP BY Items.Iid
Fall 2001 Database Systems 24
Left Outer Join Result
• The result of the left outer join in the preceding query is:
 ¢¡¤£ ¥§¦ ¨ © ¢¦ ¨ ¥¢¤¤¨ !$#¤% '(¦ ¨ )01!$ 2¤#3¦ #¤%
4§5 687 91@¤A1B C§7 48D 7FE G(7 HPIQ1R S¤T 4U¤TVWU1X
4§5 6¢D 91@¤A1B C1Y 48E `§E GaA b1UcedfQTfT gQ1R RQ T
4§5 6¢A 91@¤A§7 C§7 41Y 7F9 G(7 HPIQ1R S¤T 4U¤TVWU1X
4§5 6Y h¢@7 C¢` 48E ipE G(7 q¤5 dS 4U¤TVWU1X
4§5 6¢E h¢@7 C§7 48D D1B G(7 HPIQ1R S¤T 4U¤TVWU1X
4§5 61` h¢@¤D C1Y 41Y i8B GaA b1UcedfQTfT gQ1R RQ T
4§5 6i h¢@¤D C8D 4§7 Y GaD gr5 dd5 Xfst4 urUvfTVWU¢X
4§5 6¢9 h¢@¤A C8D 48A E GaD gr5 dd5 Xfst4 urUvfTVWU¢X
C8A GaD w§S§R STIUdfS x0S1y€pU‚ƒ
C8E G0Y q¤5 IQTfTfU HS¤QVVR S
Result of items
inner join bids
The additional
tuples resulting from
the left outer join
SQL
13
Fall 2001 Database Systems 25
 ¢¡¤£¦¥¨§¨©   £ ¨ 
 !#¦$%' Smith@aol.com
¢( )10243'5 Johns@geocities.com
¢6 78902@A3 Brown@netmail.com
CB D#89E'F Gray@microsoft.com
G¨HAIAP1Q¨R G¨SUT1VW X¦Y4`¦a P¨`CY1V b
c1d eAf'gUh Gray@microsoft,com
cpi qpr4stf'uUv Robert432@aol.com
cpw xyr2€19v Johns@somewhere.com
c‚ xygƒvƒr¨ Jj@yahoo.com
cp„ …‡†2ˆ‰t Duke@jazz.com
2‘C’1“” ¨•– —˜•– ™¦d4eCf g2h‰i‰dj‰•h4k
l¨m n˜m oApUq¨rs2t u4v‰tUwUv¨x
l4y ny z|{ }2}2{ x'~u1{ €ƒ ‚v2ƒUtƒwUv4x
l4„ ny …#s1rs2t‰pƒv}ƒs †¦s‡‰ˆ#v2€‰Š
l‰‹ n„ Œpv2¢}ƒqUtƒt z#q4r rqUt
l4Ž nC‹  ƒvƒ‰€¢‘pv’‰“4”
{ pUqƒtUtƒv
o#sqUwUwrs
l• n˜m { }Us u4v‰tUwUv¨x –#—‡˜C™ –Aš › ˜Cœ12ž —#š › –‡Ÿ y—1› ¡˜¢£1Ÿ4¤4
¥|¦ §¨ ©‡ªp«1¬ ­‡¨ ¥|® ¨ƒ¯
¥|¦ §|® ©‡ªp«1¬ ­p° ¥|¯ ±|¯
¥|¦ §|« ©‡ªp«A¨ ­‡¨ ¥#° ¨U©
¥|¦ §#° ²‡ª1¨ ­y± ¥|¯ ³¯
¥|¦ §|¯ ²‡ª1¨ ­‡¨ ¥|® ®y¬
¥|¦ §A± ²‡ªp® ­p° ¥#° ³‡¬
¥|¦ §‡³ ²‡ªp® ­#® ¥¨ °
¥|¦ §A© ²‡ªp« ­#® ¥|« ¯
Auction Database
Fall 2001 Database Systems 26
Example 1
• Find the name and email of owners of items located in
“Boston”
SELECT O.Name, O.Email
FROM Owners O, Items I
WHERE I.Location = ‘Boston’ AND
I.Oid = O.Oid
SQL
14
Fall 2001 Database Systems 27
Example 2
• Find the identifiers and amount of bids placed by
buyer “Roberts”
SELECT B.Bid, B.Amount
FROM Bids B, Buyers Buy
WHERE Buy.Name = ‘Roberts’ AND
Buy.Buyid = B.Buyid
Fall 2001 Database Systems 28
Example 3
• Find the names of buyers who placed a bid on an
item owned by “Brown”
SELECT DISTINCT Buy.Name
FROM Bids B, Buyers Buy, Owners O, Items I
WHERE O.Name = ‘Brown’ AND
O.Oid = I.Oid AND
I.Iid = B.Iid AND
B.Buyid = Buy.Buyid
SQL
15
Fall 2001 Database Systems 29
Example 4
• Find the identifier of items with more than one bid
SELECT B.Iid
FROM Bids B
GROUP BY B.Iid HAVING count(B.Bid)  1
or
SELECT DISTINCT B.Iid
FROM Bids B
WHERE EXISTS (
SELECT *
FROM Bids B2
WHERE B2.Iid = B.Iid AND B2.Bid  B.Bid)
Fall 2001 Database Systems 30
Example 5
• Find the names of items all buyers placed a bid on
Find the names of items for which there does not exist a buyer
who did not place a bid on this item
SELECT I.name
FROM Items I
WHERE NOT EXISTS (
SELECT *
FROM Buyers Buy
WHERE NOT EXISTS (
SELECT *
FROM Bids B
WHERE Buy.Buyid = B.Buyid AND
B.Iid = I.Iid ))
SQL
16
Fall 2001 Database Systems 31
Expressions
• Each predicate in the WHERE clause can evaluate to
– TRUE
– FALSE
– UNKNOWN
• Example:
SELECT B.*
FROM Bids B
WHERE B.amount  (SELECT max(B1.amount)
FROM Bids B1, Items I
WHERE B1.iid=I.iid AND
I.location = “Boston”)
What if there are no tuples
in this expression?
Fall 2001 Database Systems 32
Unknown expressions
• Expressions that compare a value against a possibly null
value return the truth value “Unknown” (U).
• Logically Unknown is different than True or False.
– Not U = U
– U and True = U
– U and U = U
– U and False = False
– U or True = True
– U or U = U
– U or False = U
SQL
17
Fall 2001 Database Systems 33
Unknown Expressions
• Unknown valued tuples do not contribute to a query result
SELECT B.*
FROM Bids B
WHERE B.amount ALL (SELECT B1.amount
FROM Bids B1, Items I
WHERE B1.iid=I.iid AND
I.location = “Boston”)
This query will not return any tuples if the amount for
some bids in Boston is “null”.
Fall 2001 Database Systems 34
Data Management
• Creating tables, inserting and deleting tuples, and updating
column values can be done by SQL statement.
CREATE TABLE tablename
( colname1 TYPE [NOT NULL],
…,
PRIMARY KEY(colname1,…)
)
INSERT INTO tablename(colname1,colname2,…)
VALUES (…)
SQL
18
Fall 2001 Database Systems 35
More Data Management
• Insert can also involve a SELECT condition
INSERT INTO BUYERS(buyid,name, email)
SELECT next(buyid), O.name, O.email
FROM Owners O
WHERE NOT EXISTS (SELECT * FROM Buyers B
WHERE B.name=O.name
AND B.email=O.email)
Fall 2001 Database Systems 36
Delete
• The Delete statement involves a “WHERE” condition that
specifies the range of tuples that should be deleted from the
relation.
DELETE FROM Bids
WHERE Bids.Iid IN (SELECT I.iid FROM Items I
WHERE I.oid = ‘O1’)
SQL
19
Fall 2001 Database Systems 37
Update
• The Update statement includes a range condition
(WHERE) and a set condition for the new values.
• For each tuple t that satisfies the where condition,
run the expression in the set statement.
UPDATE items
SET avgbid = (SELECT avg(B.amount)
FROM Bids B
WHERE B.iid = items.iid)
WHERE items.location = ‘Boston’

More Related Content

Viewers also liked

[Www.pkbulk.blogspot.com]dbms09
[Www.pkbulk.blogspot.com]dbms09[Www.pkbulk.blogspot.com]dbms09
[Www.pkbulk.blogspot.com]dbms09
AnusAhmad
 
[Www.pkbulk.blogspot.com]dbms05
[Www.pkbulk.blogspot.com]dbms05[Www.pkbulk.blogspot.com]dbms05
[Www.pkbulk.blogspot.com]dbms05
AnusAhmad
 
[Www.pkbulk.blogspot.com]dbms01
[Www.pkbulk.blogspot.com]dbms01[Www.pkbulk.blogspot.com]dbms01
[Www.pkbulk.blogspot.com]dbms01
AnusAhmad
 
[Www.pkbulk.blogspot.com]dbms04
[Www.pkbulk.blogspot.com]dbms04[Www.pkbulk.blogspot.com]dbms04
[Www.pkbulk.blogspot.com]dbms04
AnusAhmad
 
[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07
AnusAhmad
 
[Www.pkbulk.blogspot.com]dbms02
[Www.pkbulk.blogspot.com]dbms02[Www.pkbulk.blogspot.com]dbms02
[Www.pkbulk.blogspot.com]dbms02
AnusAhmad
 

Viewers also liked (6)

[Www.pkbulk.blogspot.com]dbms09
[Www.pkbulk.blogspot.com]dbms09[Www.pkbulk.blogspot.com]dbms09
[Www.pkbulk.blogspot.com]dbms09
 
[Www.pkbulk.blogspot.com]dbms05
[Www.pkbulk.blogspot.com]dbms05[Www.pkbulk.blogspot.com]dbms05
[Www.pkbulk.blogspot.com]dbms05
 
[Www.pkbulk.blogspot.com]dbms01
[Www.pkbulk.blogspot.com]dbms01[Www.pkbulk.blogspot.com]dbms01
[Www.pkbulk.blogspot.com]dbms01
 
[Www.pkbulk.blogspot.com]dbms04
[Www.pkbulk.blogspot.com]dbms04[Www.pkbulk.blogspot.com]dbms04
[Www.pkbulk.blogspot.com]dbms04
 
[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07
 
[Www.pkbulk.blogspot.com]dbms02
[Www.pkbulk.blogspot.com]dbms02[Www.pkbulk.blogspot.com]dbms02
[Www.pkbulk.blogspot.com]dbms02
 

Similar to [Www.pkbulk.blogspot.com]dbms06

45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview Questions
Best SEO Tampa
 
2018 03 27_biological_databases_part4_v_upload
2018 03 27_biological_databases_part4_v_upload2018 03 27_biological_databases_part4_v_upload
2018 03 27_biological_databases_part4_v_upload
Prof. Wim Van Criekinge
 
Sql 99 and_some_techniques
Sql 99 and_some_techniquesSql 99 and_some_techniques
Sql 99 and_some_techniques
Alexey Kiselyov
 
CS121Lec05.pdf
CS121Lec05.pdfCS121Lec05.pdf
CS121Lec05.pdf
georgejustymirobi1
 
SQL Queries .pdf
SQL Queries .pdfSQL Queries .pdf
SQL Queries .pdf
srinathpurushotham
 
Database Management System - SQL Advanced Training
Database Management System - SQL Advanced TrainingDatabase Management System - SQL Advanced Training
Database Management System - SQL Advanced Training
Moutasm Tamimi
 
advanced sql(database)
advanced sql(database)advanced sql(database)
advanced sql(database)
welcometofacebook
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearch
Ryosuke Nakamura
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
HAMEEDHUSSAINBU21CSE
 
Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101
IDERA Software
 
SQL
SQLSQL
Hybrid rule engines (rulesfest 2010)
Hybrid rule engines (rulesfest 2010)Hybrid rule engines (rulesfest 2010)
Hybrid rule engines (rulesfest 2010)
Geoffrey De Smet
 
I.Multiple Choice 20 Questions worth 1 point each..docx
I.Multiple Choice 20 Questions worth 1 point each..docxI.Multiple Choice 20 Questions worth 1 point each..docx
I.Multiple Choice 20 Questions worth 1 point each..docx
jewisonantone
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
oysteing
 
CS 542 Database Index Structures
CS 542 Database Index StructuresCS 542 Database Index Structures
CS 542 Database Index Structures
J Singh
 
Αλγόριθμοι
Αλγόριθμοι Αλγόριθμοι
on SQL Managment studio(For the following exercise, use the Week 5.pdf
on SQL Managment studio(For the following exercise, use the Week 5.pdfon SQL Managment studio(For the following exercise, use the Week 5.pdf
on SQL Managment studio(For the following exercise, use the Week 5.pdf
formaxekochi
 
SQL Tuning and VST
SQL Tuning and VST SQL Tuning and VST
SQL Tuning and VST
Kyle Hailey
 
Bis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-newBis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-new
assignmentcloud85
 
Divide and Be Conquered?
Divide and Be Conquered?Divide and Be Conquered?
Divide and Be Conquered?
brooksaix
 

Similar to [Www.pkbulk.blogspot.com]dbms06 (20)

45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview Questions
 
2018 03 27_biological_databases_part4_v_upload
2018 03 27_biological_databases_part4_v_upload2018 03 27_biological_databases_part4_v_upload
2018 03 27_biological_databases_part4_v_upload
 
Sql 99 and_some_techniques
Sql 99 and_some_techniquesSql 99 and_some_techniques
Sql 99 and_some_techniques
 
CS121Lec05.pdf
CS121Lec05.pdfCS121Lec05.pdf
CS121Lec05.pdf
 
SQL Queries .pdf
SQL Queries .pdfSQL Queries .pdf
SQL Queries .pdf
 
Database Management System - SQL Advanced Training
Database Management System - SQL Advanced TrainingDatabase Management System - SQL Advanced Training
Database Management System - SQL Advanced Training
 
advanced sql(database)
advanced sql(database)advanced sql(database)
advanced sql(database)
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearch
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
 
Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101
 
SQL
SQLSQL
SQL
 
Hybrid rule engines (rulesfest 2010)
Hybrid rule engines (rulesfest 2010)Hybrid rule engines (rulesfest 2010)
Hybrid rule engines (rulesfest 2010)
 
I.Multiple Choice 20 Questions worth 1 point each..docx
I.Multiple Choice 20 Questions worth 1 point each..docxI.Multiple Choice 20 Questions worth 1 point each..docx
I.Multiple Choice 20 Questions worth 1 point each..docx
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
 
CS 542 Database Index Structures
CS 542 Database Index StructuresCS 542 Database Index Structures
CS 542 Database Index Structures
 
Αλγόριθμοι
Αλγόριθμοι Αλγόριθμοι
Αλγόριθμοι
 
on SQL Managment studio(For the following exercise, use the Week 5.pdf
on SQL Managment studio(For the following exercise, use the Week 5.pdfon SQL Managment studio(For the following exercise, use the Week 5.pdf
on SQL Managment studio(For the following exercise, use the Week 5.pdf
 
SQL Tuning and VST
SQL Tuning and VST SQL Tuning and VST
SQL Tuning and VST
 
Bis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-newBis 345-final-exam-guide-set-1-new
Bis 345-final-exam-guide-set-1-new
 
Divide and Be Conquered?
Divide and Be Conquered?Divide and Be Conquered?
Divide and Be Conquered?
 

More from AnusAhmad

[Www.pkbulk.blogspot.com]file and indexing
[Www.pkbulk.blogspot.com]file and indexing[Www.pkbulk.blogspot.com]file and indexing
[Www.pkbulk.blogspot.com]file and indexing
AnusAhmad
 
[Www.pkbulk.blogspot.com]dbms12
[Www.pkbulk.blogspot.com]dbms12[Www.pkbulk.blogspot.com]dbms12
[Www.pkbulk.blogspot.com]dbms12
AnusAhmad
 
[Www.pkbulk.blogspot.com]dbms11
[Www.pkbulk.blogspot.com]dbms11[Www.pkbulk.blogspot.com]dbms11
[Www.pkbulk.blogspot.com]dbms11
AnusAhmad
 
[Www.pkbulk.blogspot.com]dbms10
[Www.pkbulk.blogspot.com]dbms10[Www.pkbulk.blogspot.com]dbms10
[Www.pkbulk.blogspot.com]dbms10
AnusAhmad
 
[Www.pkbulk.blogspot.com]dbms03
[Www.pkbulk.blogspot.com]dbms03[Www.pkbulk.blogspot.com]dbms03
[Www.pkbulk.blogspot.com]dbms03
AnusAhmad
 
[Www.pkbulk.blogspot.com]dbms13
[Www.pkbulk.blogspot.com]dbms13[Www.pkbulk.blogspot.com]dbms13
[Www.pkbulk.blogspot.com]dbms13
AnusAhmad
 
9. java server faces
9. java server faces9. java server faces
9. java server faces
AnusAhmad
 
8. java script
8. java script8. java script
8. java script
AnusAhmad
 
7. struts
7. struts7. struts
7. struts
AnusAhmad
 
5. servlets
5. servlets5. servlets
5. servlets
AnusAhmad
 
4. jsp
4. jsp4. jsp
4. jsp
AnusAhmad
 
3. applets
3. applets3. applets
3. applets
AnusAhmad
 
2. http, html
2. http, html2. http, html
2. http, html
AnusAhmad
 
1. intro
1. intro1. intro
1. intro
AnusAhmad
 
6. hibernate
6. hibernate6. hibernate
6. hibernate
AnusAhmad
 

More from AnusAhmad (15)

[Www.pkbulk.blogspot.com]file and indexing
[Www.pkbulk.blogspot.com]file and indexing[Www.pkbulk.blogspot.com]file and indexing
[Www.pkbulk.blogspot.com]file and indexing
 
[Www.pkbulk.blogspot.com]dbms12
[Www.pkbulk.blogspot.com]dbms12[Www.pkbulk.blogspot.com]dbms12
[Www.pkbulk.blogspot.com]dbms12
 
[Www.pkbulk.blogspot.com]dbms11
[Www.pkbulk.blogspot.com]dbms11[Www.pkbulk.blogspot.com]dbms11
[Www.pkbulk.blogspot.com]dbms11
 
[Www.pkbulk.blogspot.com]dbms10
[Www.pkbulk.blogspot.com]dbms10[Www.pkbulk.blogspot.com]dbms10
[Www.pkbulk.blogspot.com]dbms10
 
[Www.pkbulk.blogspot.com]dbms03
[Www.pkbulk.blogspot.com]dbms03[Www.pkbulk.blogspot.com]dbms03
[Www.pkbulk.blogspot.com]dbms03
 
[Www.pkbulk.blogspot.com]dbms13
[Www.pkbulk.blogspot.com]dbms13[Www.pkbulk.blogspot.com]dbms13
[Www.pkbulk.blogspot.com]dbms13
 
9. java server faces
9. java server faces9. java server faces
9. java server faces
 
8. java script
8. java script8. java script
8. java script
 
7. struts
7. struts7. struts
7. struts
 
5. servlets
5. servlets5. servlets
5. servlets
 
4. jsp
4. jsp4. jsp
4. jsp
 
3. applets
3. applets3. applets
3. applets
 
2. http, html
2. http, html2. http, html
2. http, html
 
1. intro
1. intro1. intro
1. intro
 
6. hibernate
6. hibernate6. hibernate
6. hibernate
 

Recently uploaded

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 

Recently uploaded (20)

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 

[Www.pkbulk.blogspot.com]dbms06

  • 1. SQL 1 Fall 2001 Database Systems 1 Nested Structures SELECT Name FROM Items WHERE Iid IN (SELECT DISTINCT Iid FROM Bids)  ¢¡¤£¦¥¨§  © ¨© !!#%$#© ¢ ')( ( 02143)5 6%7 89!7A@49B 'C C DFE G¢G%E BIH 8¦E P Q RF9¢S47#@49B 'T C UV6V5 6¢7!1A9¢G#6 WX6%Y `V9%P4a '¢b T c9%dGA3#7A7 DV35 53 #7 'e b f g#9¢S!P h9i#p%q g%E 1A3#7A7A9 0)6¢3¢@I@¢5 6 'r ( g%E G#6 89!7A@49B stVuXv s2w x uXy%€! t¦w x sV‚!ƒtx „†…†‡‚ˆ#€ ‰¦ ‘2’ “”•– —V’ ˜ ’4™ ‰¦ ‘2˜ “”•– —%d ™ e2™ ‰¦ ‘¦• “”•¦’ —V’ d ’4“ ‰¦ ‘d f)”)’ —e ™ gh™ ‰¦ ‘¦™ f)”)’ —V’ ˜ ˜– ‰¦ ‘Ve f)”)˜ —%d • gV– ‰¦ ‘)g f)”)˜ —V˜ ’ d ‰¦ ‘¦“ f)”• —V˜ • ™ Fall 2001 Database Systems 2 Let Temp = SELECT Iid FROM Bids i†j¦kml nVo p q¦r qs qVt q¦u SELECT Name FROM Items WHERE Iid IN (SELECT Iid FROM Bids) Is Iid IN Temp? Is I1 IN Temp? v!wFxy{z v| } ~¨| } X€%‚ ƒ!„!…#€¢†#| „¢‡ ˆ)‰ ‰ Š2‹4Œ Ž¢ %‘!A’ ‘“ Pick a tuple from Items YES! Put this tuple in the output v!wFxy{z v| } ~¨| } X€%‚ ƒ!„!…#€¢†#| „¢‡ ˆ” ” •¤– —!—%– “4˜™š ›¤‘¢œAA’I‘“ Is Iid IN Temp? Is I2 IN Temp? Pick another tuple from Items YES! Put this tuple in the output v!wFxy{z v| } ~¨| } X€%‚ ƒ!„!…#€¢†#| „¢‡ ˆ ” ž)Ž) Ž¢#‹4‘%—AŽ ŸX  Is Iid IN Temp? Is I3 IN Temp? Pick another tuple from Items NO! Remove this tuple from the output
  • 2. SQL 2 Fall 2001 Database Systems 3 Nested Structures – correlation SELECT I.Name FROM Items I WHERE I.Iid IN (SELECT DISTINCT B.Iid FROM Bids B WHERE B.Buyid = I.Oid)  ¢¡¤£¦¥¨§  © © !¢¢#$%$© ' (0) ) 132546 78 9@¢8BA @C Pick a tuple from Items Compute the nested relation DFEHGPI Q¤R S T U Is I1 in Temp? No, remove this tuple! V¢W¤X¦Y¨` Va b ca b def g h¢i¢p$eq$a ir s¦t t uwv x¢xv y5€‚¦ƒ „w…†B‡Bˆ‰…y Pick a tuple from Items Compute the nested relation ’‘H“•” –¤— ˜ ™ d Is I2 in Temp? No, remove this tuple! Fall 2001 Database Systems 4 Nested Structures – correlation SELECT I.Name FROM Items I WHERE I.Iid IN (SELECT DISTINCT B.Iid FROM Bids B WHERE B.Buyid = I.Oid) e$f¤g0hi ej k lmj k nopq r¢s¢t$ou$j sv w¦x y z0{0| {}$~5€B{ {‚Fƒ3„ Pick a tuple from Items Compute the nested relation …’†H‡•ˆ ‰¤Š ‹ Œ  Is I3 in Temp? No, remove this tuple! Ž¢¤¦‘¨’ Ž“ ” •H“ ” –¤—˜™ š¢›$œ$—$$“ ›¢ž Ÿ  ¡ ¢£¢¤¤¥$¦$§‰§ ¨©¦ª ª¦ B§ Pick a tuple from Items Compute the nested relation «’¬ ­P® ¯¤° ± ²´³ ²µ Is I4 in Temp? YES, keep this tuple!
  • 3. SQL 3 Fall 2001 Database Systems 5 SQL - Nested Statements • A nested sub-expression may refer to the tuples from the outer relation SELECT Buy.Name FROM Buyers Buy WHERE Buy.BuyId IN ( SELECT B.BuyID FROM Bids B WHERE B.Amount 20 AND B.BuyID NOT IN ( SELECT B2.BuyID FROM Bids B2 WHERE B2.Amount = 20 AND B2.Iid = B.Iid)) Names of buyers with a bid greater than 20 on an item for which the buyer has no other bids less than 20. Fall 2001 Database Systems 6 Nested Structures - Exists SELECT FROM WHERE (NOT) EXISTS (SELECT * FROM R1, ..., Rm WHERE P) • Semantics: For each tuple of the outer query, execute the inner query; if there is at least one (no) tuple in the result of the inner query, then retrieve that tuple of the outer query. • This accounts for the “there exists” type of queries
  • 4. SQL 4 Fall 2001 Database Systems 7 SQL Nested Statements • Two more nested conditions: – EXISTS ( SELECT … FROM … WHERE … ) is true if the SFW clause returns a non-empty relation – NOT EXISTS ( SELECT … FROM … WHERE … ) is true if the SFW clause returns an empty relation • Find the name of all buyers who have no bids with an amount less than 50 SELECT Buy.Name FROM Buyers Buy WHERE NOT EXISTS (SELECT B.BuyId FROM Bids B WHERE B.Amount 50 AND B.BuyID=Buy.BuyId) Fall 2001 Database Systems 8 SQL - EXISTS • Find the names of buyers who placed a bid on all items with one or more bids SELECT Buy.Name FROM Buyers Buy WHERE EXISTS ( SELECT B.Iid FROM Bids B WHERE Buy.BuyId = B.BuyId) Is this correct? Names of buyers who have bid on an item Find buyers such that there does not exist an item with bids on which these buyers did not bid
  • 5. SQL 5 Fall 2001 Database Systems 9 SQL - EXISTS • Find the names of buyers who placed a bid on all items with one or more bids SELECT Buy.Name FROM Buyers Buy WHERE NOT EXISTS ( SELECT B.Iid FROM Bids B WHERE NOT EXISTS ( SELECT * FROM Bids B2 WHERE B.Iid = B2.Iid AND B2.Buyid = Buy.BuyId) ) bids for this item by this buyer items with bids, but not by this buyer either there are no bids or this buyer has a bid on everything Fall 2001 Database Systems 10 Things to Think About • Don’t use nested queries if they are not needed. – they make the query unnecessarily complicated – they make optimization harder • If a query requires only “IN” and “EXISTS”, then it probably can be done without nesting.
  • 6. SQL 6 Fall 2001 Database Systems 11 Aggregate functions • Count, Max, Min, Sum, and Avg are aggregate functions that apply to the values of a set of tuples • Find the total number of bids in the bids table SELECT Count(*) FROM Bids • Find the average bid for the “Dipping Bird”. SELECT Avg(B.Amount) AS AvgAmt FROM Bids B, Items I WHERE B.Iid = I.Iid AND I.Name = ‘Dipping Bird’ Fall 2001 Database Systems 12 Aggregate Functions • Find the buyer(s) who placed the maximum bid on the “Dipping Bird” SELECT DISTINCT B2.Buyid FROM Bids B2, Items I2 WHERE B2.Iid=I2.Iid AND I2.Name = ‘Dipping Bird’ AND B2.Amount = (SELECT Max(B.Amount) FROM Bids B, Items I WHEREB.Iid = I.Iid AND I.Name = ‘Dipping Bird’)
  • 7. SQL 7 Fall 2001 Database Systems 13 Grouping SELECT a FROM b WHERE c GROUP BY d Create the Cartesian product of the relations in b Find all tuples from the Cartesian product that satisfy the condition in c (including any subexpressions) For all distinct values of the columns listed in d, create a group that contains the set of tuples with that value for “d” Create the columns in a, which should start with columns in d and end with aggregates that will apply to each group separately. Fall 2001 Database Systems 14 Group by • Find the average bid amount for each item, list the item name, the average bid and the number of bids SELECT I.name, avg(B.Amount) AS AvgBid, count(*) AS Cnt FROM Items I, Bids B WHERE I.Iid = B.Iid GROUP BY I.name
  • 8. SQL 8 Fall 2001 Database Systems 15  ¢¡¤£ ¥§¦ ¨ © ¦ ¨ ¥¢ !¨ $#%¤'( )0¦ ¨ 123# 4¤%¤5¤ ¦ %!' 687 9§@ A¢BC3D E@ 68F @HG IP@ QRTSU VW 63X¤WTYX3` 687 9§F A¢BC3D E!a 6G b8G IcC dXegf S(W(W hiS!U US W 687 98C A¢BC8@ E@ 63a @HA IP@ QRTSU VW 63X¤WTYX3` 687 9¢a pB!@ E3b 6G q§G IP@ r7 f V 63X¤WTYX3` 687 98G pB!@ E@ 68F F!D IP@ QRTSU VW 63X¤WTYX3` 687 9b pB!F E!a 63a qD IcC dXegf S(W(W hiS!U US W 687 9¢q pB!F EF 68@ a IPF h$7 f¤f!7 `tsu6 vwXx WTYX3` 687 98A pBC EF 6C G IPF h$7 f¤f!7 `tsu6 vwXx WTYX3` First Step • After the FROM/WHERE clause before grouping Fall 2001 Database Systems 16 y€ ‚$ƒ „†…‡§ˆ ‰ƒ y‘ ƒt’ “”ƒ •—–˜†™ de y8f¢e3g(f§h y€ ‚‡ „†…‡ƒ ‰ƒ y†i ƒ„ “”ƒ •—–˜†™ de y8f¢e3g(f§h y€ ‚i’ j…§ƒ ‰ƒ y‘ ‘8ˆ “”ƒ •—–˜†™ de y8f¢e3g(f§h kl m†n op8q rq ks t uwv x2y zz8y {¤|~} c€8!‚!ƒ(€†{ }y „i… †‡ˆ ‰v }iˆ Š uwv x2y zz8y {¤|~} c€8!‚!ƒ(€†{ ‹Œ $Ž †¢‘§’ “¢” ‹†• –i• —˜‘ ™š¢›œž!ŸŸ  $ž†¡ ¡ž 3Ÿ ‹Œ – ¢Ž “¢” ‹8” £’ —˜‘ ™š¢›œž!ŸŸ  $ž†¡ ¡ž 3Ÿ ¤¥ ¦†§ ¨†©8ª «§¬ ¤†­ ®i­ ¯wª °8¥ ±² ¤†³!´!µ ³8¶ Second Step • After the group by
  • 9. SQL 9 Fall 2001 Database Systems 17 Third Step • The SELECT statement creates a single tuple for each group. • The aggregates functions are applied to each group individually.  ¢¡¤£¦¥¨§© ¦!$# % $'!( )103254 687 9A@$B C¤@ D E¦F GHG8F I!PRQ S B T U V5WYXG 27!7 C¤@$B T U ` F G 6 @$T 9 Fall 2001 Database Systems 18 GROUP BY HAVING • Find the items with 2 or more bids and with the average bid greater than $20, for each item list the item name SELECT I.name FROM Items I, Bids B WHERE I.Iid = B.Iid GROUP BY I.name HAVING avg(B.Amount) 20 AND count(DISTINCT B.Bid) = 2 • Find all buyer and item pairs where the buyer placed more than one bid on the same item SELECT B.buyid, B.iid FROM Bids B GROUP BY B.buyid, B.iid HAVING count(B.bid) 1
  • 10. SQL 10 Fall 2001 Database Systems 19 • Find owners who have at least two items with average bids greater than $20 for each item. List the owner identifiers and the names of the items. SELECT I2.Oid, I2.Name FROM Items I2 WHERE I2.Oid IN ( SELECT I.Oid FROM Items I WHERE I.Iid IN ( SELECT B.Iid FROM Bids B GROUP BY B.Iid HAVING Avg(Amount) 20) GROUP BY I.Oid HAVING Count(*) = 2) AND I2.Iid IN ( SELECT B.Iid FROM Bids B GROUP BY B.Iid HAVING Avg(Amount) 20) Fall 2001 Database Systems 20 ORDER BY • Follows group by and orders the results in ascending or descending order • Example: Find buyer and item pairs in which the buyer has more than one bid for the item, order the result in descending order of buyer ids SELECT B.buyid, B.iid FROM Bids B GROUP BY B.buyid, B.iid HAVING count(B.bid) 1 ORDER BY B.buyid DESC
  • 11. SQL 11 Fall 2001 Database Systems 21 SELECT complete • First the Cartesian product of all tables in the from clause is formed • From this, rows not satisfying the where condition are eliminated • The remaining rows are grouped in accordance with the group by clause • Groups not satisfying the having clause then eliminated • The expressions of the select clause target list are evaluated • If the key word distinct is present, duplicate rows are now eliminated • The union is taken after each Subselect is evaluated • Finally, the set of all result rows is sorted if an order by clause is present. Fall 2001 Database Systems 22 Join types • It is possible to define a join in the from clause – INNER JOIN: regular join from relational algebra where only tuples from the two tables satisfying the join condition are chosen – LEFT OUTER JOIN: (regular join) union (tuples from the table to the left that did not join with any tuples, padded with null values for the new attributes) – RIGHT OUTER JOIN: (regular join) union (tuples from the table to the right that did not join with any tuples, padded with null values for the new attributes) – FULL JOIN = LEFT RIGHT OUTER JOIN
  • 12. SQL 12 Fall 2001 Database Systems 23 Join Examples • Find names of all items with bids SELECT DISTINCT Items.name FROM Items INNER JOIN Bids ON Items.Iid = Bids.Iid • Find all items and the total number of bids on them SELECT Items.Iid, Count(Bids.Bid) FROM Items LEFT OUTER JOIN Bids ON Items.Iid = Bids.Iid GROUP BY Items.Iid Fall 2001 Database Systems 24 Left Outer Join Result • The result of the left outer join in the preceding query is:  ¢¡¤£ ¥§¦ ¨ © ¢¦ ¨ ¥¢¤¤¨ !$#¤% '(¦ ¨ )01!$ 2¤#3¦ #¤% 4§5 687 91@¤A1B C§7 48D 7FE G(7 HPIQ1R S¤T 4U¤TVWU1X 4§5 6¢D 91@¤A1B C1Y 48E `§E GaA b1UcedfQTfT gQ1R RQ T 4§5 6¢A 91@¤A§7 C§7 41Y 7F9 G(7 HPIQ1R S¤T 4U¤TVWU1X 4§5 6Y h¢@7 C¢` 48E ipE G(7 q¤5 dS 4U¤TVWU1X 4§5 6¢E h¢@7 C§7 48D D1B G(7 HPIQ1R S¤T 4U¤TVWU1X 4§5 61` h¢@¤D C1Y 41Y i8B GaA b1UcedfQTfT gQ1R RQ T 4§5 6i h¢@¤D C8D 4§7 Y GaD gr5 dd5 Xfst4 urUvfTVWU¢X 4§5 6¢9 h¢@¤A C8D 48A E GaD gr5 dd5 Xfst4 urUvfTVWU¢X C8A GaD w§S§R STIUdfS x0S1y€pU‚ƒ C8E G0Y q¤5 IQTfTfU HS¤QVVR S Result of items inner join bids The additional tuples resulting from the left outer join
  • 13. SQL 13 Fall 2001 Database Systems 25  ¢¡¤£¦¥¨§¨©   £ ¨ !#¦$%' Smith@aol.com ¢( )10243'5 Johns@geocities.com ¢6 78902@A3 Brown@netmail.com CB D#89E'F Gray@microsoft.com G¨HAIAP1Q¨R G¨SUT1VW X¦Y4`¦a P¨`CY1V b c1d eAf'gUh Gray@microsoft,com cpi qpr4stf'uUv Robert432@aol.com cpw xyr2€19v Johns@somewhere.com c‚ xygƒvƒr¨ Jj@yahoo.com cp„ …‡†2ˆ‰t Duke@jazz.com 2‘C’1“” ¨•– —˜•– ™¦d4eCf g2h‰i‰dj‰•h4k l¨m n˜m oApUq¨rs2t u4v‰tUwUv¨x l4y ny z|{ }2}2{ x'~u1{ €ƒ ‚v2ƒUtƒwUv4x l4„ ny …#s1rs2t‰pƒv}ƒs †¦s‡‰ˆ#v2€‰Š l‰‹ n„ Œpv2¢}ƒqUtƒt z#q4r rqUt l4Ž nC‹  ƒvƒ‰€¢‘pv’‰“4” { pUqƒtUtƒv o#sqUwUwrs l• n˜m { }Us u4v‰tUwUv¨x –#—‡˜C™ –Aš › ˜Cœ12ž —#š › –‡Ÿ y—1› ¡˜¢£1Ÿ4¤4 ¥|¦ §¨ ©‡ªp«1¬ ­‡¨ ¥|® ¨ƒ¯ ¥|¦ §|® ©‡ªp«1¬ ­p° ¥|¯ ±|¯ ¥|¦ §|« ©‡ªp«A¨ ­‡¨ ¥#° ¨U© ¥|¦ §#° ²‡ª1¨ ­y± ¥|¯ ³¯ ¥|¦ §|¯ ²‡ª1¨ ­‡¨ ¥|® ®y¬ ¥|¦ §A± ²‡ªp® ­p° ¥#° ³‡¬ ¥|¦ §‡³ ²‡ªp® ­#® ¥¨ ° ¥|¦ §A© ²‡ªp« ­#® ¥|« ¯ Auction Database Fall 2001 Database Systems 26 Example 1 • Find the name and email of owners of items located in “Boston” SELECT O.Name, O.Email FROM Owners O, Items I WHERE I.Location = ‘Boston’ AND I.Oid = O.Oid
  • 14. SQL 14 Fall 2001 Database Systems 27 Example 2 • Find the identifiers and amount of bids placed by buyer “Roberts” SELECT B.Bid, B.Amount FROM Bids B, Buyers Buy WHERE Buy.Name = ‘Roberts’ AND Buy.Buyid = B.Buyid Fall 2001 Database Systems 28 Example 3 • Find the names of buyers who placed a bid on an item owned by “Brown” SELECT DISTINCT Buy.Name FROM Bids B, Buyers Buy, Owners O, Items I WHERE O.Name = ‘Brown’ AND O.Oid = I.Oid AND I.Iid = B.Iid AND B.Buyid = Buy.Buyid
  • 15. SQL 15 Fall 2001 Database Systems 29 Example 4 • Find the identifier of items with more than one bid SELECT B.Iid FROM Bids B GROUP BY B.Iid HAVING count(B.Bid) 1 or SELECT DISTINCT B.Iid FROM Bids B WHERE EXISTS ( SELECT * FROM Bids B2 WHERE B2.Iid = B.Iid AND B2.Bid B.Bid) Fall 2001 Database Systems 30 Example 5 • Find the names of items all buyers placed a bid on Find the names of items for which there does not exist a buyer who did not place a bid on this item SELECT I.name FROM Items I WHERE NOT EXISTS ( SELECT * FROM Buyers Buy WHERE NOT EXISTS ( SELECT * FROM Bids B WHERE Buy.Buyid = B.Buyid AND B.Iid = I.Iid ))
  • 16. SQL 16 Fall 2001 Database Systems 31 Expressions • Each predicate in the WHERE clause can evaluate to – TRUE – FALSE – UNKNOWN • Example: SELECT B.* FROM Bids B WHERE B.amount (SELECT max(B1.amount) FROM Bids B1, Items I WHERE B1.iid=I.iid AND I.location = “Boston”) What if there are no tuples in this expression? Fall 2001 Database Systems 32 Unknown expressions • Expressions that compare a value against a possibly null value return the truth value “Unknown” (U). • Logically Unknown is different than True or False. – Not U = U – U and True = U – U and U = U – U and False = False – U or True = True – U or U = U – U or False = U
  • 17. SQL 17 Fall 2001 Database Systems 33 Unknown Expressions • Unknown valued tuples do not contribute to a query result SELECT B.* FROM Bids B WHERE B.amount ALL (SELECT B1.amount FROM Bids B1, Items I WHERE B1.iid=I.iid AND I.location = “Boston”) This query will not return any tuples if the amount for some bids in Boston is “null”. Fall 2001 Database Systems 34 Data Management • Creating tables, inserting and deleting tuples, and updating column values can be done by SQL statement. CREATE TABLE tablename ( colname1 TYPE [NOT NULL], …, PRIMARY KEY(colname1,…) ) INSERT INTO tablename(colname1,colname2,…) VALUES (…)
  • 18. SQL 18 Fall 2001 Database Systems 35 More Data Management • Insert can also involve a SELECT condition INSERT INTO BUYERS(buyid,name, email) SELECT next(buyid), O.name, O.email FROM Owners O WHERE NOT EXISTS (SELECT * FROM Buyers B WHERE B.name=O.name AND B.email=O.email) Fall 2001 Database Systems 36 Delete • The Delete statement involves a “WHERE” condition that specifies the range of tuples that should be deleted from the relation. DELETE FROM Bids WHERE Bids.Iid IN (SELECT I.iid FROM Items I WHERE I.oid = ‘O1’)
  • 19. SQL 19 Fall 2001 Database Systems 37 Update • The Update statement includes a range condition (WHERE) and a set condition for the new values. • For each tuple t that satisfies the where condition, run the expression in the set statement. UPDATE items SET avgbid = (SELECT avg(B.amount) FROM Bids B WHERE B.iid = items.iid) WHERE items.location = ‘Boston’