Beruflich Dokumente
Kultur Dokumente
58 103 11/12/02
31 Lubber 8 55.5
An instance R of Reserves
58 Rusty 10 35
71 Zorba 10 16
74 Horatio 9 40
An instance S of Sailors
22 Dustin 7 45.0
71 Zorba 10 16
74 Horatio 9 40
22 Dustin 7 45.0
31 Lubber 8 55.5
74 Horatio 9 40
71 Zorba 10 16
74 Horatio 9 40
58 Rusty 10 35
71 Zorba 10 16
74 Horatio 9 40
58 Rusty 10 35 Rusty 35
71 Zorba 10 16
74 Horatio 9 40
Rusty 35
58 Rusty 10 35
71 Zorba 10 16 Zorba 16
74 Horatio 9 40
Rusty 35
58 Rusty 10 35
Zorba 16
71 Zorba 10 16
74 Horatio 9 40 Horatio 40
Rusty 35
58 Rusty 10 35
Zorba 16
71 Zorba 10 16
Horatio 40
74 Horatio 9 40
End of
Algorithm
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 10
LeongHW, SoC, NUS
Summary of Q1:
• Result of SQL query
– is another table
– derived from original table.
58 Rusty 10 35
71 Zorba 10 16
74 Horatio 9 40
S (instance of Sailors)
31 Lubber 8 55.5
58 Rusty 10 35
31 Lubber 8 55.5
58 Rusty 10 35
71 Zorba 10 16
74 Horatio 9 40
31 Lubber 8 55.5
31 Lubber
58 Rusty 10 35
71 Zorba 10 16
74 Horatio 9 40
31 Lubber 8 55.5
31 Lubber
58 Rusty 10 35
58 Rusty
71 Zorba 10 16
74 Horatio 9 40
31 Lubber 8 55.5
31 Lubber
58 Rusty 10 35 58 Rusty
71 Zorba 10 16
71 Zorba
74 Horatio 9 40
31 Lubber 8 55.5
31 Lubber
58 Rusty 10 35 58 Rusty
71 Zorba 10 16 71 Zorba
74 Horatio 9 40 74 Horatio
S (instance of Sailors)
31 Lubber 8 55.5
31 Lubber
58 Rusty 10 35 58 Rusty
71 Zorba 10 16 71 Zorba
74 Horatio 9 40 74 Horatio
End of
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 19
Algorithm
LeongHW, SoC, NUS
Summary of Q2:
• Result of SQL query
– is another table
– row-inclusion is determined by where-clause.
22 101 10/10/02
22 Dustin 7 45.0
58 103 11/12/02
31 Lubber 8 55.5
An instance R of Reserves
58 Rusty 10 35
S (instance of Sailors)
Overview:
sid sname rating age
A JOIN operation works as follows:
• for each row in table S;
22 Dustin 7 45.0
+ try to “join” with each row in R
(match the “where” conditions)
31 Lubber 8 55.5
58 Rusty 10 35
Analysis:
R (instance of Reserves)
So, a JOIN takes O(nm) row
sid bid day
operations
22 101 10/10/02 where n = size of table S, and
m = size of table R.
58 103 11/12/02
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 22
LeongHW, SoC, NUS
Q3. Find the names of sailors who have reserved boat number 103.
SELECT S.name
FROM Sailors S, Reserves R
WHERE (S.sid = R.sid) AND (R.bid = 103) CPU
(R.bid ≠ 103) !
58 Rusty 10 35
R (instance of Reserves)
sid bid day
Condition is false
22 101 10/10/02 Do not output
this entry.
58 103 11/12/02
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 23
LeongHW, SoC, NUS
Q3. Find the names of sailors who have reserved boat number 103.
SELECT S.name
FROM Sailors S, Reserves R
WHERE (S.sid = R.sid) AND (R.bid = 103) CPU
31 Lubber 8 55.5
58 Rusty 10 35
R (instance of Reserves)
sid bid day
Condition is false
22 101 10/10/02 Do not output
this entry.
58 103 11/12/02
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 24
LeongHW, SoC, NUS
Q3. Find the names of sailors who have reserved boat number 103.
SELECT S.name
FROM Sailors S, Reserves R
WHERE (S.sid = R.sid) AND (R.bid = 103) CPU
31 Lubber 8 55.5
58 Rusty 10 35
R (instance of Reserves)
sid bid day
Condition is false
22 101 10/10/02 Do not output
this entry.
58 103 11/12/02
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 25
LeongHW, SoC, NUS
Q3. Find the names of sailors who have reserved boat number 103.
SELECT S.name
FROM Sailors S, Reserves R
WHERE (S.sid = R.sid) AND (R.bid = 103) CPU
31 Lubber 8 55.5
58 Rusty 10 35
R (instance of Reserves)
sid bid day
Condition is false
22 101 10/10/02 Do not output
this entry.
58 103 11/12/02
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 26
LeongHW, SoC, NUS
Q3. Find the names of sailors who have reserved boat number 103.
SELECT S.name
FROM Sailors S, Reserves R
WHERE (S.sid = R.sid) AND (R.bid = 103) CPU
31 Lubber 8 55.5
58 Rusty 10 35
R (instance of Reserves)
sid bid day
Condition is false
22 101 10/10/02 Do not output
this entry.
58 103 11/12/02
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 27
LeongHW, SoC, NUS
Q3. Find the names of sailors who have reserved boat number 103.
SELECT S.name
FROM Sailors S, Reserves R
WHERE (S.sid = R.sid) AND (R.bid = 103) CPU
(R.bid = 103) !
58 Rusty 10 35
R (instance of Reserves)
sid bid day
Condition is true
22 101 10/10/02
Output this entry.
58 103 11/12/02
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 28
LeongHW, SoC, NUS
Q3. Find the names of sailors who have reserved boat number 103.
SELECT S.name
FROM Sailors S, Reserves R
WHERE (S.sid = R.sid) AND (R.bid = 103) CPU
31 Lubber 8 55.5
58 Rusty 10 35
End of
R (instance of Reserves) Algorithm
sid bid day
22 101 10/10/02
58 103 11/12/02
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 29
LeongHW, SoC, NUS
Summary of Q3:
• Result of SQL query requires
– information from two tables
– a JOIN operation is necessary