30
LeongHW, SoC, NUS (UIT2201: Database) Page 1 © Leong Hon Wai, 2003-2008 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”

Animation of SQL Queries

  • Upload
    teo

  • View
    94

  • Download
    9

Embed Size (px)

DESCRIPTION

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”. - PowerPoint PPT Presentation

Citation preview

Page 1: Animation of SQL Queries

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

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”

Page 2: Animation of SQL Queries

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

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

An instance S of Sailors

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

sid bid day

22 101 10/10/02

58 103 11/12/02

An instance R of Reserves

Page 3: Animation of SQL Queries

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

Q1. Find the names and ages of all sailors.

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

SELECT S.sname, S.ageFROM Sailors S

The correspondingSQL query.

Now, animate the execution of the SQL query!

S (instance of Sailors)

Page 4: Animation of SQL Queries

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

Result

sname agesid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Q1. Find the names and ages of all sailors. [Step 0]

SELECT S.sname, S.ageFROM Sailors S

Query result is also a database table.

S (instance of Sailors)

Page 5: Animation of SQL Queries

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

Result

sname agesid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Dustin 45.0

SELECT S.sname, S.ageFROM Sailors S

Q1. Find the names and ages of all sailors. [Step 1]

Output only the required fields

in this entry.

S (instance of Sailors)

Page 6: Animation of SQL Queries

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

Result

sname agesid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Dustin 45.0

SELECT S.sname, S.ageFROM Sailors S

Lubber 55.5

Q1. Find the names and ages of all sailors. [Step 2]

S (instance of Sailors)

Page 7: Animation of SQL Queries

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

Result

sname agesid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Dustin 45.0

Lubber 55.5

SELECT S.sname, S.ageFROM Sailors S

Rusty 35

Q1. Find the names and ages of all sailors. [Step 3]

S (instance of Sailors)

Page 8: Animation of SQL Queries

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

Result

sname agesid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Dustin 45.0

Lubber 55.5

Rusty 35

SELECT S.sname, S.ageFROM Sailors S

Zorba 16

Q1. Find the names and ages of all sailors. [Step 4]

S (instance of Sailors)

Page 9: Animation of SQL Queries

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

Result

sname agesid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Dustin 45.0

Lubber 55.5

Rusty 35

Zorba 16

SELECT S.sname, S.ageFROM Sailors S

Horatio 40

Q1. Find the names and ages of all sailors. [Step 5]

S (instance of Sailors)

Page 10: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 10© Leong Hon Wai, 2003-2008

Result

sname agesid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Dustin 45.0

Lubber 55.5

Rusty 35

Zorba 16

Horatio 40

SELECT S.sname, S.ageFROM Sailors S

Q1. Find the names and ages of all sailors. [Step 6]

End of Algorithm

S (instance of Sailors)

Page 11: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 11© Leong Hon Wai, 2003-2008

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

Page 12: Animation of SQL Queries

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

Q2. Find all sailors with a rating above 7.

SELECT S.sid, S.snameFROM Sailors SWHERE (S.rating > 7)

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

The correspondingSQL query.

Now, animate the execution of the SQL query!

S (instance of Sailors)

Page 13: Animation of SQL Queries

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

SELECT S.sid, S.snameFROM Sailors SWHERE (S.rating > 7)

Result

Q2. Find all sailors with a rating above 7. [Step 0]

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

CPU

sid sname

Query result is also a database table.

S (instance of Sailors)

Page 14: Animation of SQL Queries

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

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Q2. Find all sailors with a rating above 7. [Step 1]

Result

7 > 7?No!

CPUSELECT S.sid, S.snameFROM Sailors SWHERE (S.rating > 7)

sid sname

Condition is falseDo not output

this entry.

S (instance of Sailors)

Page 15: Animation of SQL Queries

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

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Q2. Find all sailors with a rating above 7. [Step 2]

Result

sid sname

8 > 7?Yes.

CPU

31 Lubber

SELECT S.sid, S.snameFROM Sailors SWHERE (S.rating > 7)

Condition is trueOutput this entry.

S (instance of Sailors)

Page 16: Animation of SQL Queries

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

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Q2. Find all sailors with a rating above 7. [Step 3]

Result

sid sname

10 > 7?Yes.

CPU

31 Lubber

SELECT S.sid, S.snameFROM Sailors SWHERE (S.rating > 7)

58 Rusty

Condition is trueOutput this entry.

S (instance of Sailors)

Page 17: Animation of SQL Queries

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

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Q2. Find all sailors with a rating above 7. [Step 4]

Result

sid sname

10 > 7?Yes.

CPU

31 Lubber

58 Rusty

SELECT S.sid, S.snameFROM Sailors SWHERE (S.rating > 7)

71 Zorba

S (instance of Sailors)

Page 18: Animation of SQL Queries

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

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Q2. Find all sailors with a rating above 7. [Step 5]

Result

sid sname

9 > 7?Yes.

CPU

31 Lubber

58 Rusty

71 Zorba

SELECT S.sid, S.snameFROM Sailors SWHERE (S.rating > 7)

74 Horatio

S (instance of Sailors)

Page 19: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 19© Leong Hon Wai, 2003-2008

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

71 Zorba 10 16

74 Horatio 9 40

Q2. Find all sailors with a rating above 7. [Step 6]

Result

sid sname

CPU

31 Lubber

58 Rusty

71 Zorba

74 Horatio

SELECT S.sid, S.snameFROM Sailors SWHERE (S.rating > 7)

End of Algorithm

S (instance of Sailors)

Page 20: Animation of SQL Queries

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

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

Page 21: Animation of SQL Queries

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

SELECT S.nameFROM Sailors S, Reserves RWHERE (S.sid = R.sid) AND (R.bid = 103)

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

sid bid day

22 101 10/10/02

58 103 11/12/02

DB (2 tables)

An instance S of Sailors

An instance R of Reserves

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

The correspondingSQL query.

Q3. Find the names of sailors who have reserved boat number 103.

IMPT: This specifies how S and Rare to be joined together.

Page 22: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 22© Leong Hon Wai, 2003-2008

S (instance of Sailors)

R (instance of Reserves)

Overview:

A JOIN operation works as follows:• for each row in table S; + try to “join” with each row in R (match the “where” conditions)

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

sid bid day

22 101 10/10/02

58 103 11/12/02

Q3. Find the names of sailors who have reserved boat number 103.

SELECT S.nameFROM Sailors S, Reserves RWHERE (S.sid = R.sid) AND (R.bid = 103)

Analysis:

So, a JOIN takes O(nm) row operations where n = size of table S, and m = size of table R.

Page 23: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 23© Leong Hon Wai, 2003-2008

S (instance of Sailors)

R (instance of Reserves)

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

sid bid day

22 101 10/10/02

58 103 11/12/02

Q3. Find the names of sailors who have reserved boat number 103.

SELECT S.nameFROM Sailors S, Reserves RWHERE (S.sid = R.sid) AND (R.bid = 103)

Result

sname

S.sid = 22

R.sid = 22

(S.sid = R.sid)

R.bid = 101

(R.bid ≠ 103) !

CPU

Condition is falseDo not output

this entry.

Page 24: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 24© Leong Hon Wai, 2003-2008

S (instance of Sailors)

R (instance of Reserves)

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

sid bid day

22 101 10/10/02

58 103 11/12/02

Q3. Find the names of sailors who have reserved boat number 103.

SELECT S.nameFROM Sailors S, Reserves RWHERE (S.sid = R.sid) AND (R.bid = 103)

Result

sname

S.sid = 22

R.sid = 58

(S.sid ≠ R.sid) !

CPU

Condition is falseDo not output

this entry.

Page 25: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 25© Leong Hon Wai, 2003-2008

S (instance of Sailors)

R (instance of Reserves)

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

sid bid day

22 101 10/10/02

58 103 11/12/02

Q3. Find the names of sailors who have reserved boat number 103.

SELECT S.nameFROM Sailors S, Reserves RWHERE (S.sid = R.sid) AND (R.bid = 103)

Result

sname

S.sid = 31

R.sid = 22

(S.sid ≠ R.sid) !

CPU

Condition is falseDo not output

this entry.

Page 26: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 26© Leong Hon Wai, 2003-2008

S (instance of Sailors)

R (instance of Reserves)

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

sid bid day

22 101 10/10/02

58 103 11/12/02

Q3. Find the names of sailors who have reserved boat number 103.

SELECT S.nameFROM Sailors S, Reserves RWHERE (S.sid = R.sid) AND (R.bid = 103)

Result

sname

S.sid = 31

R.sid = 58

(S.sid ≠ R.sid) !

CPU

Condition is falseDo not output

this entry.

Page 27: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 27© Leong Hon Wai, 2003-2008

S (instance of Sailors)

R (instance of Reserves)

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

sid bid day

22 101 10/10/02

58 103 11/12/02

Q3. Find the names of sailors who have reserved boat number 103.

SELECT S.nameFROM Sailors S, Reserves RWHERE (S.sid = R.sid) AND (R.bid = 103)

Result

sname

S.sid = 58

R.sid = 22

(S.sid ≠ R.sid) !

CPU

Condition is falseDo not output

this entry.

Page 28: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 28© Leong Hon Wai, 2003-2008

S (instance of Sailors)

R (instance of Reserves)

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

sid bid day

22 101 10/10/02

58 103 11/12/02

Q3. Find the names of sailors who have reserved boat number 103.

SELECT S.nameFROM Sailors S, Reserves RWHERE (S.sid = R.sid) AND (R.bid = 103)

Result

sname

S.sid = 58

R.sid = 58

(S.sid = R.sid) !

CPU

Condition is trueOutput this entry.

R.bid = 103

(R.bid = 103) !

Rusty

Page 29: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 29© Leong Hon Wai, 2003-2008

S (instance of Sailors)

R (instance of Reserves)

sid sname rating age

22 Dustin 7 45.0

31 Lubber 8 55.5

58 Rusty 10 35

sid bid day

22 101 10/10/02

58 103 11/12/02

Q3. Find the names of sailors who have reserved boat number 103.

SELECT S.nameFROM Sailors S, Reserves RWHERE (S.sid = R.sid) AND (R.bid = 103)

Result

sname

CPU

Rusty

End of Algorithm

Page 30: Animation of SQL Queries

LeongHW, SoC, NUS(UIT2201: Database) Page 30© Leong Hon Wai, 2003-2008

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