Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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”

2 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 sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 sidbidday 2210110/10/02 5810311/12/02 An instance R of Reserves

3 LeongHW, SoC, NUS (UIT2201: Database) Page 3 © Leong Hon Wai, 2003-2008 Q1. Find the names and ages of all sailors. sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 SELECT S.sname, S.age FROM Sailors S The corresponding SQL query. Now, animate the execution of the SQL query! S (instance of Sailors)

4 LeongHW, SoC, NUS (UIT2201: Database) Page 4 © Leong Hon Wai, 2003-2008 Result snameage sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Q1. Find the names and ages of all sailors. [Step 0] SELECT S.sname, S.age FROM Sailors S Query result is also a database table. S (instance of Sailors)

5 LeongHW, SoC, NUS (UIT2201: Database) Page 5 © Leong Hon Wai, 2003-2008 Result snameage sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Dustin45.0 SELECT S.sname, S.age FROM 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)

6 LeongHW, SoC, NUS (UIT2201: Database) Page 6 © Leong Hon Wai, 2003-2008 Result snameage sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Dustin45.0 SELECT S.sname, S.age FROM Sailors S Lubber55.5 Q1. Find the names and ages of all sailors. [Step 2] S (instance of Sailors)

7 LeongHW, SoC, NUS (UIT2201: Database) Page 7 © Leong Hon Wai, 2003-2008 Result snameage sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Dustin45.0 Lubber55.5 SELECT S.sname, S.age FROM Sailors S Rusty35 Q1. Find the names and ages of all sailors. [Step 3] S (instance of Sailors)

8 LeongHW, SoC, NUS (UIT2201: Database) Page 8 © Leong Hon Wai, 2003-2008 Result snameage sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Dustin45.0 Lubber55.5 Rusty35 SELECT S.sname, S.age FROM Sailors S Zorba16 Q1. Find the names and ages of all sailors. [Step 4] S (instance of Sailors)

9 LeongHW, SoC, NUS (UIT2201: Database) Page 9 © Leong Hon Wai, 2003-2008 Result snameage sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Dustin45.0 Lubber55.5 Rusty35 Zorba16 SELECT S.sname, S.age FROM Sailors S Horatio40 Q1. Find the names and ages of all sailors. [Step 5] S (instance of Sailors)

10 LeongHW, SoC, NUS (UIT2201: Database) Page 10 © Leong Hon Wai, 2003-2008 Result snameage sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Dustin45.0 Lubber55.5 Rusty35 Zorba16 Horatio40 SELECT S.sname, S.age FROM Sailors S Q1. Find the names and ages of all sailors. [Step 6] End of Algorithm S (instance of Sailors)

11 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

12 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.sname FROM Sailors S WHERE (S.rating > 7) sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 The corresponding SQL query. Now, animate the execution of the SQL query! S (instance of Sailors)

13 LeongHW, SoC, NUS (UIT2201: Database) Page 13 © Leong Hon Wai, 2003-2008 SELECT S.sid, S.sname FROM Sailors S WHERE (S.rating > 7) Result Q2. Find all sailors with a rating above 7. [Step 0] sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 CPU sidsname Query result is also a database table. S (instance of Sailors)

14 LeongHW, SoC, NUS (UIT2201: Database) Page 14 © Leong Hon Wai, 2003-2008 sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Q2. Find all sailors with a rating above 7. [Step 1] Result 7 > 7? No! CPU SELECT S.sid, S.sname FROM Sailors S WHERE (S.rating > 7) sidsname Condition is false Do not output this entry. S (instance of Sailors)

15 LeongHW, SoC, NUS (UIT2201: Database) Page 15 © Leong Hon Wai, 2003-2008 sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Q2. Find all sailors with a rating above 7. [Step 2] Result sidsname 8 > 7? Yes. CPU 31Lubber SELECT S.sid, S.sname FROM Sailors S WHERE (S.rating > 7) Condition is true Output this entry. S (instance of Sailors)

16 LeongHW, SoC, NUS (UIT2201: Database) Page 16 © Leong Hon Wai, 2003-2008 sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Q2. Find all sailors with a rating above 7. [Step 3] Result sidsname 10 > 7? Yes. CPU 31Lubber SELECT S.sid, S.sname FROM Sailors S WHERE (S.rating > 7) 58Rusty Condition is true Output this entry. S (instance of Sailors)

17 LeongHW, SoC, NUS (UIT2201: Database) Page 17 © Leong Hon Wai, 2003-2008 sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Q2. Find all sailors with a rating above 7. [Step 4] Result sidsname 10 > 7? Yes. CPU 31Lubber 58Rusty SELECT S.sid, S.sname FROM Sailors S WHERE (S.rating > 7) 71Zorba S (instance of Sailors)

18 LeongHW, SoC, NUS (UIT2201: Database) Page 18 © Leong Hon Wai, 2003-2008 sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Q2. Find all sailors with a rating above 7. [Step 5] Result sidsname 9 > 7? Yes. CPU 31Lubber 58Rusty 71Zorba SELECT S.sid, S.sname FROM Sailors S WHERE (S.rating > 7) 74Horatio S (instance of Sailors)

19 LeongHW, SoC, NUS (UIT2201: Database) Page 19 © Leong Hon Wai, 2003-2008 sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 71Zorba1016 74Horatio940 Q2. Find all sailors with a rating above 7. [Step 6] Result sidsname CPU 31Lubber 58Rusty 71Zorba 74Horatio SELECT S.sid, S.sname FROM Sailors S WHERE (S.rating > 7) End of Algorithm S (instance of Sailors)

20 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

21 LeongHW, SoC, NUS (UIT2201: Database) Page 21 © Leong Hon Wai, 2003-2008 SELECT S.name FROM Sailors S, Reserves R WHERE (S.sid = R.sid) AND (R.bid = 103) sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 sidbidday 2210110/10/02 5810311/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 corresponding SQL query. Q3. Find the names of sailors who have reserved boat number 103. IMPT: This specifies how S and R are to be joined together.

22 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) sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 sidbidday 2210110/10/02 5810311/12/02 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) Analysis: So, a JOIN takes O(nm) row operations where n = size of table S, and m = size of table R.

23 LeongHW, SoC, NUS (UIT2201: Database) Page 23 © Leong Hon Wai, 2003-2008 S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 sidbidday 2210110/10/02 5810311/12/02 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) Result sname S.sid = 22 R.sid = 22 (S.sid = R.sid) R.bid = 101 (R.bid ≠ 103) ! CPU Condition is false Do not output this entry.

24 LeongHW, SoC, NUS (UIT2201: Database) Page 24 © Leong Hon Wai, 2003-2008 S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 sidbidday 2210110/10/02 5810311/12/02 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) Result sname S.sid = 22 R.sid = 58 (S.sid ≠ R.sid) ! CPU Condition is false Do not output this entry.

25 LeongHW, SoC, NUS (UIT2201: Database) Page 25 © Leong Hon Wai, 2003-2008 S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 sidbidday 2210110/10/02 5810311/12/02 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) Result sname S.sid = 31 R.sid = 22 (S.sid ≠ R.sid) ! CPU Condition is false Do not output this entry.

26 LeongHW, SoC, NUS (UIT2201: Database) Page 26 © Leong Hon Wai, 2003-2008 S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 sidbidday 2210110/10/02 5810311/12/02 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) Result sname S.sid = 31 R.sid = 58 (S.sid ≠ R.sid) ! CPU Condition is false Do not output this entry.

27 LeongHW, SoC, NUS (UIT2201: Database) Page 27 © Leong Hon Wai, 2003-2008 S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 sidbidday 2210110/10/02 5810311/12/02 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) Result sname S.sid = 58 R.sid = 22 (S.sid ≠ R.sid) ! CPU Condition is false Do not output this entry.

28 LeongHW, SoC, NUS (UIT2201: Database) Page 28 © Leong Hon Wai, 2003-2008 S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 sidbidday 2210110/10/02 5810311/12/02 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) Result sname S.sid = 58 R.sid = 58 (S.sid = R.sid) ! CPU Condition is true Output this entry. R.bid = 103 (R.bid = 103) ! Rusty

29 LeongHW, SoC, NUS (UIT2201: Database) Page 29 © Leong Hon Wai, 2003-2008 S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035 sidbidday 2210110/10/02 5810311/12/02 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) Result sname CPU Rusty End of Algorithm

30 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


Download ppt "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."

Similar presentations


Ads by Google