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.

Slides:



Advertisements
Similar presentations
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Animation of Algorithm Goal: To understand an algorithm by animating its execution,
Advertisements

1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Relational Algebra Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
1 Database Systems ( 資料庫系統 ) October 22/24, 2007 Lecture #5.
1 Lecture 11: Basic SQL, Integrity constraints
CS 166: Database Management Systems
1 Relational Calculus Chapter 4 – Part II. 2 Formal Relational Query Languages  Two mathematical Query Languages form the basis for “real” languages.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
SQL.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
CS 405G: Introduction to Database Systems
SQL Review.
ICS 624 Spring 2011 Overview of DB & IR Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/12/20111Lipyeow.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Relational Calculus CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth   We will occasionally use this arrow notation unless there is danger of.
1 Rewriting Minus Queries Using Not In SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ MINUS.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 sidsnameratingage 22dustin745 29brutus133.
1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
UNIT – III Form of basic SQL Query Examples of Basic SQL Queries
Relational Calculus R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion. Ronald Graham Elements of Ramsey.
1 Advanced SQL. 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name,
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Chapter 5.  Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert, delete and modify rows.  Data Definition.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
10/20/20151 Introduction to SQL and the Relational Model ICOS Big Data Summer Camp June 2, 2015 Mike Anderson (slides from Mike Cafarella)
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
SQL 2 Introduction Structured Query Language (SQL): the most widely used commercial relational database language Originally.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
Introduction to Query Optimization, R. Ramakrishnan and J. Gehrke 1 Introduction to Query Optimization Chapter 13.
The Oracle Database System. Connecting to the Database At the command line prompt, write: sqlplus In the beginning your password.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Introduction to Query Optimization Chapter 13.
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
Relational Calculus Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
SQL CS 186, Spring 2007, Lecture 7 R&G, Chapter 5 Mary Roth   The important thing is not to stop questioning. Albert Einstein Life is just a bowl of.
Spring 2011 ITCS3160: Database Design and Implementation Hands-on Learning.
1 CS122A: Introduction to Data Management Lecture #7 Relational Algebra I Instructor: Chen Li.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
DataBase - Check 01 DataBase 2 nd year Computer Science & Engineer 1.
Relational Calculus Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Basic SQL Queries Go over example queries, like 10 > ALL.
COP Introduction to Database Structures
© פרופ' יהושע שגיב, האוניברסיטה העברית
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
SQL The Query Language R & G - Chapter 5
Database Systems October 14, 2009 Lecture #5.
Introduction to Query Optimization
Database Systems (資料庫系統)
Basic SQL Lecture 6 Fall
Introduction to Database Systems
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
Examples of Physical Query Plan Alternatives
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
CS 405G: Introduction to Database Systems
SQL: The Query Language Part 1
SQL: Structured Query Language
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Calculus Chapter 4 – Part II.
Presentation transcript:

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

LeongHW, SoC, NUS (UIT2201: Database) Page 2 © Leong Hon Wai, Sailors ( sid: integer, sname: string, rating: integer, age: real ) Reserves ( sid: integer, bid: integer, day: date ) An instance S of Sailors sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 sidbidday /10/ /12/02 An instance R of Reserves

LeongHW, SoC, NUS (UIT2201: Database) Page 3 © Leong Hon Wai, Q1. Find the names and ages of all sailors. sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 SELECT S.sname, S.age FROM Sailors S The corresponding SQL query. Now, animate the execution of the SQL query! S (instance of Sailors)

LeongHW, SoC, NUS (UIT2201: Database) Page 4 © Leong Hon Wai, Result snameage sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 5 © Leong Hon Wai, Result snameage sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 6 © Leong Hon Wai, Result snameage sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 7 © Leong Hon Wai, Result snameage sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 8 © Leong Hon Wai, Result snameage sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 9 © Leong Hon Wai, Result snameage sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 10 © Leong Hon Wai, Result snameage sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 11 © Leong Hon Wai, 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

LeongHW, SoC, NUS (UIT2201: Database) Page 12 © Leong Hon Wai, Q2. Find all sailors with a rating above 7. SELECT S.sid, S.sname FROM Sailors S WHERE (S.rating > 7) sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 The corresponding SQL query. Now, animate the execution of the SQL query! S (instance of Sailors)

LeongHW, SoC, NUS (UIT2201: Database) Page 13 © Leong Hon Wai, 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 22Dustin Lubber Rusty Zorba Horatio940 CPU sidsname Query result is also a database table. S (instance of Sailors)

LeongHW, SoC, NUS (UIT2201: Database) Page 14 © Leong Hon Wai, sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 15 © Leong Hon Wai, sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 16 © Leong Hon Wai, sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 17 © Leong Hon Wai, sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 18 © Leong Hon Wai, sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 19 © Leong Hon Wai, sidsnameratingage 22Dustin Lubber Rusty Zorba Horatio940 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)

LeongHW, SoC, NUS (UIT2201: Database) Page 20 © Leong Hon Wai, 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

LeongHW, SoC, NUS (UIT2201: Database) Page 21 © Leong Hon Wai, SELECT S.name FROM Sailors S, Reserves R WHERE (S.sid = R.sid) AND (R.bid = 103) sidsnameratingage 22Dustin Lubber Rusty1035 sidbidday /10/ /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.

LeongHW, SoC, NUS (UIT2201: Database) Page 22 © Leong Hon Wai, 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 22Dustin Lubber Rusty1035 sidbidday /10/ /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.

LeongHW, SoC, NUS (UIT2201: Database) Page 23 © Leong Hon Wai, S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin Lubber Rusty1035 sidbidday /10/ /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.

LeongHW, SoC, NUS (UIT2201: Database) Page 24 © Leong Hon Wai, S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin Lubber Rusty1035 sidbidday /10/ /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.

LeongHW, SoC, NUS (UIT2201: Database) Page 25 © Leong Hon Wai, S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin Lubber Rusty1035 sidbidday /10/ /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.

LeongHW, SoC, NUS (UIT2201: Database) Page 26 © Leong Hon Wai, S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin Lubber Rusty1035 sidbidday /10/ /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.

LeongHW, SoC, NUS (UIT2201: Database) Page 27 © Leong Hon Wai, S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin Lubber Rusty1035 sidbidday /10/ /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.

LeongHW, SoC, NUS (UIT2201: Database) Page 28 © Leong Hon Wai, S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin Lubber Rusty1035 sidbidday /10/ /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

LeongHW, SoC, NUS (UIT2201: Database) Page 29 © Leong Hon Wai, S (instance of Sailors) R (instance of Reserves) sidsnameratingage 22Dustin Lubber Rusty1035 sidbidday /10/ /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

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