Sie sind auf Seite 1von 30

Animation of SQL Queries

• To illustrate three SQL queries:


– Q1: simple select (one table)
– Q2: select with conditions (one table)
– Q3: select requiring a JOIN operation.

• Observe how they are “implemented”


– Measure the number of “row operations”

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 1


LeongHW, SoC, NUS
Sailors ( sid: integer, sname: string, rating: integer, age: real )
Reserves ( sid: integer, bid: integer, day: date )

sid sname rating age sid bid day

22 Dustin 7 45.0 22 101 10/10/02

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

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 2


LeongHW, SoC, NUS
Q1. Find the names and ages of all sailors.
SELECT S.sname, S.age
FROM Sailors S
The corresponding
S (instance of Sailors) SQL query.
sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5 Now, animate the


execution of the SQL query!
58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 3


LeongHW, SoC, NUS
Q1. Find the names and ages of all sailors. [Step 0]
SELECT S.sname, S.age
FROM Sailors S
Result
S (instance of Sailors)

sid sname rating age sname age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35 Query result is also


a database table.
71 Zorba 10 16

74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 4


LeongHW, SoC, NUS
Q1. Find the names and ages of all sailors. [Step 1]
SELECT S.sname, S.age
FROM Sailors S
Result
S (instance of Sailors)

sid sname rating age sname age

22 Dustin 7 45.0 Dustin 45.0

31 Lubber 8 55.5 Output only the


required fields
58 Rusty 10 35 in this entry.

71 Zorba 10 16

74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 5


LeongHW, SoC, NUS
Q1. Find the names and ages of all sailors. [Step 2]
SELECT S.sname, S.age
FROM Sailors S
Result
S (instance of Sailors)

sid sname rating age sname age

22 Dustin 7 45.0 Dustin 45.0

31 Lubber 8 55.5 Lubber 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 6


LeongHW, SoC, NUS
Q1. Find the names and ages of all sailors. [Step 3]
SELECT S.sname, S.age
FROM Sailors S
Result
S (instance of Sailors)

sid sname rating age sname age

22 Dustin 7 45.0 Dustin 45.0

31 Lubber 8 55.5 Lubber 55.5

58 Rusty 10 35 Rusty 35

71 Zorba 10 16

74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 7


LeongHW, SoC, NUS
Q1. Find the names and ages of all sailors. [Step 4]
SELECT S.sname, S.age
FROM Sailors S
Result
S (instance of Sailors)

sid sname rating age sname age

22 Dustin 7 45.0 Dustin 45.0

31 Lubber 8 55.5 Lubber 55.5

Rusty 35
58 Rusty 10 35

71 Zorba 10 16 Zorba 16

74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 8


LeongHW, SoC, NUS
Q1. Find the names and ages of all sailors. [Step 5]
SELECT S.sname, S.age
FROM Sailors S
Result
S (instance of Sailors)

sid sname rating age sname age

22 Dustin 7 45.0 Dustin 45.0

31 Lubber 8 55.5 Lubber 55.5

Rusty 35
58 Rusty 10 35
Zorba 16
71 Zorba 10 16

74 Horatio 9 40 Horatio 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 9


LeongHW, SoC, NUS
Q1. Find the names and ages of all sailors. [Step 6]
SELECT S.sname, S.age
FROM Sailors S
Result
S (instance of Sailors)

sid sname rating age sname age

22 Dustin 7 45.0 Dustin 45.0

31 Lubber 8 55.5 Lubber 55.5

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.

• A simple analysis shows


– This takes (n) row operations,
where n is size (the number of records) in table S.

• This query is also called a “projection”


– It is the same as the “e-project” primitive
– It simply selected a subset of the columns
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 11
LeongHW, SoC, NUS
Q2. Find all sailors with a rating above 7.
SELECT S.sid, S.sname
FROM Sailors S
WHERE (S.rating > 7) The corresponding
SQL query.
S (instance of Sailors)

sid sname rating age

22 Dustin 7 45.0 Now, animate the


31 Lubber 8 55.5
execution of the SQL query!

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 12


LeongHW, SoC, NUS
Q2. Find all sailors with a rating above 7. [Step 0]
SELECT S.sid, S.sname CPU
FROM Sailors S
WHERE (S.rating > 7)

S (instance of Sailors)

sid sname rating age


Result

22 Dustin 7 45.0 sid sname

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16 Query result is also


a database table.
74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 13


LeongHW, SoC, NUS
Q2. Find all sailors with a rating above 7. [Step 1]
SELECT S.sid, S.sname CPU
FROM Sailors S
WHERE (S.rating > 7) 7 > 7?
No!
S (instance of Sailors) Condition is false
Do not output
sid sname rating age this entry.
Result

22 Dustin 7 45.0 sid sname

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 14


LeongHW, SoC, NUS
Q2. Find all sailors with a rating above 7. [Step 2]
SELECT S.sid, S.sname CPU
FROM Sailors S
WHERE (S.rating > 7) 8 > 7?
Yes.
S (instance of Sailors)
Condition is true
sid sname rating age Output this entry.
Result

22 Dustin 7 45.0 sid sname

31 Lubber 8 55.5
31 Lubber

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 15


LeongHW, SoC, NUS
Q2. Find all sailors with a rating above 7. [Step 3]
SELECT S.sid, S.sname CPU
FROM Sailors S
WHERE (S.rating > 7) 10 > 7?
Yes.
S (instance of Sailors)
Condition is true
sid sname rating age Output this entry.
Result

22 Dustin 7 45.0 sid sname

31 Lubber 8 55.5
31 Lubber

58 Rusty 10 35
58 Rusty
71 Zorba 10 16

74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 16


LeongHW, SoC, NUS
Q2. Find all sailors with a rating above 7. [Step 4]
SELECT S.sid, S.sname CPU
FROM Sailors S
WHERE (S.rating > 7) 10 > 7?
Yes.
S (instance of Sailors)

sid sname rating age


Result

22 Dustin 7 45.0 sid sname

31 Lubber 8 55.5
31 Lubber

58 Rusty 10 35 58 Rusty

71 Zorba 10 16
71 Zorba

74 Horatio 9 40

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 17


LeongHW, SoC, NUS
Q2. Find all sailors with a rating above 7. [Step 5]
SELECT S.sid, S.sname CPU
FROM Sailors S
WHERE (S.rating > 7) 9 > 7?
Yes.
S (instance of Sailors)

sid sname rating age


Result

22 Dustin 7 45.0 sid sname

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

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 18


LeongHW, SoC, NUS
Q2. Find all sailors with a rating above 7. [Step 6]
SELECT S.sid, S.sname CPU
FROM Sailors S
WHERE (S.rating > 7)

S (instance of Sailors)

sid sname rating age


Result

22 Dustin 7 45.0 sid sname

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.

• A simple analysis shows


– This takes (n) row operations;
where n is size (the number of records) in table S.

• This query can be decomposed into


– an “e-select”, followed by an “e-project” primitives

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 20


LeongHW, SoC, NUS
Q3. Find the names of sailors who have reserved boat number 103.
SELECT S.name
FROM Sailors S, Reserves R The corresponding
WHERE (S.sid = R.sid) AND (R.bid = 103) SQL query.
DB (2 tables)

sid sname rating age sid bid day

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

An instance S of Sailors IMPT: This specifies how S and R


are to be joined together.
This query requires information from both tables S and R.
To answer this query, a JOIN operation needs to be performed.

© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 21


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)

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

S (instance of Sailors) Result


S.sid = 22
sid sname rating age sname
R.sid = 22
22 Dustin 7 45.0 (S.sid = R.sid)

31 Lubber 8 55.5 R.bid = 101

(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

S (instance of Sailors) Result


S.sid = 22
sid sname rating age sname
R.sid = 58
22 Dustin 7 45.0 (S.sid ≠ R.sid) !

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

S (instance of Sailors) Result


S.sid = 31
sid sname rating age sname
R.sid = 22
22 Dustin 7 45.0 (S.sid ≠ R.sid) !

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

S (instance of Sailors) Result


S.sid = 31
sid sname rating age sname
R.sid = 58
22 Dustin 7 45.0 (S.sid ≠ R.sid) !

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

S (instance of Sailors) Result


S.sid = 58
sid sname rating age sname
R.sid = 22
22 Dustin 7 45.0 (S.sid ≠ R.sid) !

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

S (instance of Sailors) Result


S.sid = 58
sid sname rating age sname
R.sid = 58
22 Dustin 7 45.0 Rusty (S.sid = R.sid) !

31 Lubber 8 55.5 R.bid = 103

(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

S (instance of Sailors) Result

sid sname rating age sname

22 Dustin 7 45.0 Rusty

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

• A simple analysis shows


– This takes (nm) row operations;
where n is size (the number of records) of table S,
and m is size (the number of records) of table R.

• Joins are EXPENSIVE operations.

• This query can be decomposed into


– an “e-join”, then “e-select”, “e-project” primitives
© Leong Hon Wai, 2003-2008 (UIT2201: Database) Page 30
LeongHW, SoC, NUS

Das könnte Ihnen auch gefallen