March 30th – intro to joins

Slides:



Advertisements
Similar presentations
1 Lecture 02: SQL. 2 Outline Data in SQL Simple Queries in SQL (6.1) Queries with more than one relation (6.2) Recomeded reading: Chapter 3, Simple Queries.
Advertisements

1 Lecture 12: SQL Friday, October 26, Outline Simple Queries in SQL (5.1) Queries with more than one relation (5.2) Subqueries (5.3) Duplicates.
Introduction to Structured Query Language (SQL)
1 INTERSECT and EXCEPT: (may no be in MySQL) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A,
1 Lecture 02: Basic SQL. 2 Outline Data in SQL Simple Queries in SQL Queries with more than one relation Reading: Chapter 3, “Simple Queries” from SQL.
Introduction to Structured Query Language (SQL)
1 Information Systems Chapter 6 Database Queries.
1. Midterm summary Types of data on the web: unstructured, semi- structured, structured Scale and uncertainty are key features Main goals are to model,
Intro. to SQL DSC340 Mike Pangburn. Learning Objectives Understand the data-representation terminology underlying relational databases Understand core.
More SQL: Complex Queries, Triggers, Views, and Schema Modification UMM AL QURA UNIVERSITY College of Computer Dr. Ali Al Najjar 1.
1 Lecture 04: SQL Wednesday, January 11, Outline Two Examples Nulls (6.1.6) Outer joins (6.3.8) Database Modifications (6.5)
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
1 Introduction to Database Systems CSE 444 Lecture 02: SQL September 28, 2007.
1 Lecture 02: SQL Friday, September 30, Administrivia Homework 1 is out. Due: Wed., Oct. 12 Did you login on IISQLSRV ? Did you change your password.
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
1 Introduction to Database Systems CSE 444 Lecture 04: SQL April 7, 2008.
Hassan Tariq INTRODUCTION TO SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query. – A query is a user–request.
CSE 326: Data Structures Lecture #22 Databases and Sorting Alon Halevy Spring Quarter 2001.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
The Relational Data Model Database Model (ODL, E/R) Relational Schema Physical storage ODL definitions Diagrams (E/R) Tables: row names: attributes rows:
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL.
Lectures 2&3: Introduction to SQL
Modeling Constraints Extracting constraints is what modeling is all about. But how do we express them? Examples: Keys: social security number uniquely.
Rob Gleasure robgleasure.com
Lecture 05: SQL Wednesday, January 12, 2005.
Cours 7: Advanced SQL.
Lecture 04: SQL Monday, January 10, 2005.
Relational Algebra at a Glance
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Programming with Android:
Cse 344 April 4th – Subqueries.
Monday 3/27 and Wednesday 3/29, 2006
Introduction to Database Systems CSE 444 Lecture 04: SQL
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
Cse 344 May 14th – Entities.
Cse 344 January 12th –joins.
Cse 344 January 10th –joins.
January 19th – Subqueries 2 and relational algebra
Lecture 4: Advanced SQL – Part II
Introduction to SQL Wenhao Zhang October 5, 2018.
Lecture 06: SQL Systems Aspects
Lecture 06 Data Modeling: E/R Diagrams
CSE544 SQL Wednesday, March 31, 2004.
Rob Gleasure robgleasure.com
Lecture 05 Views, Constraints
Lectures 3: Introduction to SQL Part II
Lectures 7: Introduction to SQL 6
Lectures 3: Introduction to SQL 2
Introduction to Database Systems CSE 444 Lecture 02: SQL
Lecture 4: SQL Thursday, January 11, 2001.
Lecture 5: The Relational Data Model
Relational Database Design
CS639: Data Management for Data Science
Lecture 06: SQL Monday, October 11, 2004.
Lecture 4: SQL Wednesday, April 10, 2002.
Rob Gleasure robgleasure.com
Lecture 03: SQL Friday, October 3, 2003.
Terminology Product Attribute names Name Price Category Manufacturer
Lectures 2: Introduction to SQL 1
Syllabus Introduction Website Management Systems
DATABASE MANAGEMENT SYSTEM
Lecture 08: E/R Diagrams and Functional Dependencies
Lecture 04: SQL Monday, October 6, 2003.
Lecture 05: SQL Wednesday, October 9, 2002.
Presentation transcript:

March 30th – intro to joins Cse 344 March 30th – intro to joins

Administrative minutiae Online Quizzes First quiz out Due next Friday (11:00 pm) Coding HW Due next Wednesday (11:30 pm) HW2 out next Wednesday Office hours Listed on course website

Demo 1 What operations should we expect SQLite (or any DBMS) to support just on what we know right now? create table insert into select delete from What sorts of inputs do these functions need to have? create table: table name, schema insert into: table name, tuple select: table name, attributes delete from: table name, condition

Demo 1 Common Syntax CREATE TABLE [tablename] ([att1] [type1], [att2] [type2]…); INSERT INTO [tablename] VALUES ([val1],[val2]…); SELECT [att1],[att2],… FROM [tablename] WHERE [condition] DELETE FROM [tablename] WHERE [condition]

Demo 1

Discussion Two other operations we want to support ALTER TABLE: Adds a new attribute to the table UPDATE: Change the attribute for a particular tuple in the table. Common Syntax ALTER TABLE [tablename] ADD [attname] [atttype] UPDATE [tablename] SET [attname]=[value]

Discussion Two other operations we want to support ALTER TABLE: Adds a new attribute to the table UPDATE: Change the attribute for a particular tuple in the table. Common Syntax ALTER TABLE [tablename] ADD [attname] [atttype] UPDATE [tablename] SET [attname]=[value] WHERE [condition]

Demo 2

Discussion Tables are NOT ordered they are sets or multisets (bags) Tables are FLAT No nested attributes Tables DO NOT prescribe how they are implemented / stored on disk This is called physical data independence

Discussion Tables may not be ordered, but data can be returned in an order with the ORDER BY modifier

Discussion Tables may not be ordered, but data can be returned in an order with the ORDER BY modifier Whew, today’s been a lot of coding... I know what you’re thinking…

Theory break

Theory break We can think of accessing information through queries as some combination of functions

Theory break We can think of accessing information through queries as some combination of functions Consider a table of UW students (with all relevant info):

Theory break We can think of accessing information through queries as some combination of functions Consider a table of UW students (with all relevant info): How would we need to get the birth year of all UWBW students from California? 1953 Boston Globe – “Hepster Lingo”

Theory break We can think of accessing information through queries as some combination of functions Consider a table of UW students (with all relevant info): How would we need to get the birth year of all UWBW students from California? Think of the file as a set of tuples

Theory break We can think of accessing information through queries as some combination of functions Consider a table of UW students (with all relevant info): How would we need to get the birth year of all UWBW students from California? Think of the file as a set of tuples Find the set of UWBW students and the set of students from California; Find the intersection of these sets, return just the year from the birthday values of this set

Theory break We can think of accessing information through queries as some combination of functions Consider a table of UW students (with all relevant info): How would we need to get the birth year of all UWBW students from California? Think of the file as a set of tuples Find the set of UWBW students and the set of students from California; Find the intersection of these sets, return just the year from the birthday values of this set What does this return?

Theory break We can think of accessing information through queries as some combination of functions Consider a table of UW students (with all relevant info): How would we need to get the birth year of all UWBW students from California? Think of the file as a set of tuples Find the set of UWBW students and the set of students from California; Find the intersection of these sets, return just the year from the birthday values of this set What does this return? Years, but with many duplicates. Even though sets don’t allow duplicates, the objects are unique.

Theory Break If we only want to return unique elements, we can use the DISTINCT modifier Even if we hide some attributes from the output, the data is all still there. When we select a subset of the attributes, this function is called a projection

Theory Break This was all for a single table. Data models specify how our data are stored and how the data are related Need to utilize these relations, or the database was pointless This involves a JOIN

Join: Intro The JOIN is the way we indicate in a query how multiple tables are related. Example, if we want all of the products and their relevant company information, we need to join those two tables. The result of the join is all of the relevant information from both tables Join occurs based on the join condition. This allows us to access information that comes from multiple tables

Retrieve all Japanese products that cost < $150 Product(pname, price, category, manufacturer) Company(cname, country) Joins in SQL pname price category manufacturer MultiTouch 199.99 gadget Canon SingleTouch 49.99 photography Gizom 50 GizmoWorks SuperGizmo 250.00 cname country GizmoWorks USA Canon Japan Hitachi Retrieve all Japanese products that cost < $150 Joins differ from selection in that we have join predicates that relate two attributes (rather than a single attribute with a constant)

Retrieve all Japanese products that cost < $150 Product(pname, price, category, manufacturer) Company(cname, country) Joins in SQL pname price category manufacturer MultiTouch 199.99 gadget Canon SingleTouch 49.99 photography Gizom 50 GizmoWorks SuperGizmo 250.00 cname country GizmoWorks USA Canon Japan Hitachi Retrieve all Japanese products that cost < $150 Joins differ from selection in that we have join predicates that relate two attributes (rather than a single attribute with a constant) SELECT pname, price FROM Product, Company WHERE ...

Retrieve all Japanese products that cost < $150 Product(pname, price, category, manufacturer) Company(cname, country) Joins in SQL pname price category manufacturer MultiTouch 199.99 gadget Canon SingleTouch 49.99 photography Gizom 50 GizmoWorks SuperGizmo 250.00 cname country GizmoWorks USA Canon Japan Hitachi Retrieve all Japanese products that cost < $150 Joins differ from selection in that we have join predicates that relate two attributes (rather than a single attribute with a constant) SELECT pname, price FROM Product, Company WHERE manufacturer=cname AND country='Japan' AND price < 150

Retrieve all USA companies that manufacture “gadget” products Product(pname, price, category, manufacturer) Company(cname, country) Joins in SQL pname price category manufacturer MultiTouch 199.99 gadget Canon SingleTouch 49.99 photography Gizom 50 GizmoWorks SuperGizmo 250.00 cname country GizmoWorks USA Canon Japan Hitachi Retrieve all USA companies that manufacture “gadget” products Gill is not shown! Why?

Retrieve all USA companies that manufacture “gadget” products Product(pname, price, category, manufacturer) Company(cname, country) Joins in SQL pname price category manufacturer MultiTouch 199.99 gadget Canon SingleTouch 49.99 photography Gizom 50 GizmoWorks SuperGizmo 250.00 cname country GizmoWorks USA Canon Japan Hitachi Retrieve all USA companies that manufacture “gadget” products Gill is not shown! Why? Why DISTINCT? SELECT DISTINCT cname FROM Product, Company WHERE country='USA' AND category = 'gadget' AND manufacturer = cname

Joins in SQL The standard join in SQL is called an inner join Each row in the result must come from both tables in the join Sometimes we want to include rows from only one of the two table: outer join

Retrieve employees and their sales Employee(id, name) Sales(employeeID, productID) Inner Join Employee id name 1 Joe 2 Jack 3 Jill Sales employeeID productID 1 344 355 2 544 Retrieve employees and their sales Jill is not shown! Why?

Retrieve employees and their sales Employee(id, name) Sales(employeeID, productID) Inner Join Employee id name 1 Joe 2 Jack 3 Jill Sales employeeID productID 1 344 355 2 544 Retrieve employees and their sales Jill is not shown! Why? SELECT * FROM Employee E, Sales S WHERE E.id = S.employeeID

Retrieve employees and their sales Employee(id, name) Sales(employeeID, productID) Inner Join Employee id name 1 Joe 2 Jack 3 Jill Sales employeeID productID 1 344 355 2 544 Retrieve employees and their sales Jill is not shown! Why? SELECT * FROM Employee E, Sales S WHERE E.id = S.employeeID id name empolyeeID productID 1 Joe 344 355 2 Jack 544

Retrieve employees and their sales Employee(id, name) Sales(employeeID, productID) Inner Join Employee id name 1 Joe 2 Jack 3 Jill Sales employeeID productID 1 344 355 2 544 Jill is missing Retrieve employees and their sales Jill is not shown! Why? SELECT * FROM Employee E, Sales S WHERE E.id = S.employeeID id name empolyeeID productID 1 Joe 344 355 2 Jack 544

Retrieve employees and their sales Employee(id, name) Sales(employeeID, productID) Inner Join Employee id name 1 Joe 2 Jack 3 Jill Sales employeeID productID 1 344 355 2 544 Jill is missing Retrieve employees and their sales Alternative syntax Jill is not shown! Why? SELECT * FROM Employee E INNER JOIN Sales S ON E.id = S.employeeID id name empolyeeID productID 1 Joe 344 355 2 Jack 544

Retrieve employees and their sales Employee(id, name) Sales(employeeID, productID) Outer Join Employee id name 1 Joe 2 Jack 3 Jill Sales employeeID productID 1 344 355 2 544 Jill is present Retrieve employees and their sales Jill is not shown! Why? SELECT * FROM Employee E LEFT OUTER JOIN Sales S ON E.id = S.employeeID id name empolyeeID productID 1 Joe 344 355 2 Jack 544 3 Jill NULL

(Inner) joins Product(pname, price, category, manufacturer) Company(cname, country) -- manufacturer is foreign key to Company SELECT DISTINCT cname FROM Product, Company WHERE country='USA' AND category = 'gadget' AND manufacturer = cname We already gone through this in the last lecture, just that they have not heard of the term “inner”

(Inner) joins SELECT DISTINCT cname FROM Product, Company WHERE country='USA' AND category = 'gadget' AND manufacturer = cname Product Company pname category manufacturer Gizmo gadget GizmoWorks Camera Photo Hitachi OneClick cname country GizmoWorks USA Canon Japan Hitachi Here they will see an example of how inner join works

(Inner) joins SELECT DISTINCT cname FROM Product, Company WHERE country='USA' AND category = 'gadget' AND manufacturer = cname Product Company pname category manufacturer Gizmo gadget GizmoWorks Camera Photo Hitachi OneClick cname country GizmoWorks USA Canon Japan Hitachi

(Inner) joins SELECT DISTINCT cname FROM Product, Company WHERE country='USA' AND category = 'gadget' AND manufacturer = cname Product Company pname category manufacturer Gizmo gadget GizmoWorks Camera Photo Hitachi OneClick cname country GizmoWorks USA Canon Japan Hitachi

(Inner) joins SELECT DISTINCT cname FROM Product, Company WHERE country='USA' AND category = 'gadget' AND manufacturer = cname Product Company pname category manufacturer Gizmo gadget GizmoWorks Camera Photo Hitachi OneClick cname country GizmoWorks USA Canon Japan Hitachi pname category manufacturer cname country Gizmo gadget GizmoWorks USA

(Inner) joins SELECT DISTINCT cname FROM Product, Company WHERE country='USA' AND category = 'gadget' AND manufacturer = cname Product Company pname category manufacturer Gizmo gadget GizmoWorks Camera Photo Hitachi OneClick cname country GizmoWorks USA Canon Japan Hitachi

(Inner) joins SELECT DISTINCT cname FROM Product, Company WHERE country='USA' AND category = 'gadget' AND manufacturer = cname Product Company pname category manufacturer Gizmo gadget GizmoWorks Camera Photo Hitachi OneClick cname country GizmoWorks USA Canon Japan Hitachi

(Inner) joins SELECT DISTINCT cname FROM Product, Company WHERE country='USA' AND category = 'gadget' AND manufacturer = cname SELECT DISTINCT cname FROM Product, Company WHERE country='USA' AND category = 'gadget' AND manufacturer = cname SELECT DISTINCT cname FROM Product JOIN Company ON country = 'USA' AND category = 'gadget' AND manufacturer = cname These are totally equivalent, just different syntax. You will see why we need another syntax in a sec (for outer joins)

(Inner) Joins for x1 in R1: for x2 in R2: SELECT x1.a1, x2.a2, … xm.am FROM R1 as x1, R2 as x2, … Rm as xm WHERE Cond for x1 in R1: for x2 in R2: ... for xm in Rm: if Cond(x1, x2…): output(x1.a1, x2.a2, … xm.am) Formally, what is the semantics of joins? It’s actually the same as the following program x1, x2, xm are called TUPLE VARIABLES, they range over every tuple in R1, R2, … You don’t have to use the “.” syntax if it is clear which relation’s a1, a2, ... Am you are referring to Each relation is bound to a variable, and the dot expression iterates through the tuple in each relation, checking whether it should be output or not This is called NESTED LOOP SEMANTICS Note that as we said in the last lecture, SQL is DECLARATIVE in the sense that we didn’t prescribe how join, select, etc are executed. Other ways to execute joins are totally possible, and we will see that in a week or so. This is called nested loop semantics since we are interpreting what a join means using a nested loop

Another example Product(pname, price, category, manufacturer) Company(cname, country) -- manufacturer is foreign key to Company Retrieve all USA companies that manufacture products in both ‘gadget’ and ‘photography’ categories

Another example Product(pname, price, category, manufacturer) Company(cname, country) -- manufacturer is foreign key to Company Retrieve all USA companies that manufacture products in both ‘gadget’ and ‘photography’ categories SELECT DISTINCT z.cname FROM Product x, Company z WHERE z.country = ’USA’ AND x.manufacturer = z.cname AND x.category = 'gadget’ AND x.category = 'photography; Does this work?

Another example Product(pname, price, category, manufacturer) Company(cname, country) -- manufacturer is foreign key to Company Retrieve all USA companies that manufacture products in both ‘gadget’ and ‘photography’ categories SELECT DISTINCT z.cname FROM Product x, Company z WHERE z.country = ’USA’ AND x.manufacturer = z.cname AND (x.category = 'gadget’ OR x.category = 'photography); What about this?

Need to include Product twice! Another example Product(pname, price, category, manufacturer) Company(cname, country) -- manufacturer is foreign key to Company Retrieve all USA companies that manufacture products in both ‘gadget’ and ‘photography’ categories SELECT DISTINCT z.cname FROM Product x, Product y, Company z WHERE z.country = ’USA’ AND x.manufacturer = z.cname AND y.manufacturer = z.cname AND x.category = 'gadget’ AND y.category = 'photography; Need to include Product twice!

Self-Joins and Tuple Variables Find USA companies that manufacture both products in the ‘gadgets’ and ‘photo’ category Joining Product with Company is insufficient: need to join Product, with Product, and with Company When a relation occurs twice in the FROM clause we call it a self-join; in that case we must use tuple variables (why?) Why? Because we need to disambiguate which table each attribute comes from!

Self-joins SELECT DISTINCT z.cname FROM Product x, Product y, Company z WHERE z.country = ‘USA’ AND x.category = ‘gadget’ AND y.category = ‘photo’ AND x.manufacturer = z.cname AND y.manufacturer = z.cname; Product Company pname category manufacturer Gizmo gadget GizmoWorks SingleTouch photo Hitachi MultiTouch Photo cname country GizmoWorks USA Hitachi Japan

Self-joins SELECT DISTINCT z.cname FROM Product x, Product y, Company z WHERE z.country = ‘USA’ AND x.category = ‘gadget’ AND y.category = ‘photo’ AND x.manufacturer = z.cname AND y.manufacturer = z.cname; Product Company x pname category manufacturer Gizmo gadget GizmoWorks SingleTouch photo Hitachi MultiTouch Photo cname country GizmoWorks USA Hitachi Japan

Self-joins SELECT DISTINCT z.cname FROM Product x, Product y, Company z WHERE z.country = ‘USA’ AND x.category = ‘gadget’ AND y.category = ‘photo’ AND x.manufacturer = z.cname AND y.manufacturer = z.cname; Product Company x pname category manufacturer Gizmo gadget GizmoWorks SingleTouch photo Hitachi MultiTouch Photo cname country GizmoWorks USA Hitachi Japan y

Self-joins SELECT DISTINCT z.cname FROM Product x, Product y, Company z WHERE z.country = ‘USA’ AND x.category = ‘gadget’ AND y.category = ‘photo’ AND x.manufacturer = z.cname AND y.manufacturer = z.cname; z Product Company x pname category manufacturer Gizmo gadget GizmoWorks SingleTouch photo Hitachi MultiTouch Photo cname country GizmoWorks USA Hitachi Japan y

Self-joins SELECT DISTINCT z.cname FROM Product x, Product y, Company z WHERE z.country = ‘USA’ AND x.category = ‘gadget’ AND y.category = ‘photo’ AND x.manufacturer = z.cname AND y.manufacturer = z.cname; z Product Company x pname category manufacturer Gizmo gadget GizmoWorks SingleTouch photo Hitachi MultiTouch Photo cname country GizmoWorks USA Hitachi Japan y

Self-joins SELECT DISTINCT z.cname FROM Product x, Product y, Company z WHERE z.country = ‘USA’ AND x.category = ‘gadget’ AND y.category = ‘photo’ AND x.manufacturer = z.cname AND y.manufacturer = z.cname; z Product Company x pname category manufacturer Gizmo gadget GizmoWorks SingleTouch photo Hitachi MultiTouch Photo cname country GizmoWorks USA Hitachi Japan y

Self-joins SELECT DISTINCT z.cname FROM Product x, Product y, Company z WHERE z.country = ‘USA’ AND x.category = ‘gadget’ AND y.category = ‘photo’ AND x.manufacturer = z.cname AND y.manufacturer = z.cname; z Product Company x pname category manufacturer Gizmo gadget GizmoWorks SingleTouch photo Hitachi MultiTouch Photo cname country GizmoWorks USA Hitachi Japan y

Self-joins SELECT DISTINCT z.cname FROM Product x, Product y, Company z WHERE z.country = ‘USA’ AND x.category = ‘gadget’ AND y.category = ‘photo’ AND x.manufacturer = z.cname AND y.manufacturer = z.cname; z Product Company x pname category manufacturer Gizmo gadget GizmoWorks SingleTouch photo Hitachi MultiTouch Photo cname country GizmoWorks USA Hitachi Japan y

Self-joins SELECT DISTINCT z.cname FROM Product x, Product y, Company z WHERE z.country = ‘USA’ AND x.category = ‘gadget’ AND y.category = ‘photo’ AND x.manufacturer = z.cname AND y.manufacturer = z.cname; z Product Company x pname category manufacturer Gizmo gadget GizmoWorks SingleTouch photo Hitachi MultiTouch Photo cname country GizmoWorks USA Hitachi Japan y x.pname x.category x.manufacturer y.pname y.category y.manufacturer z.cname z.country Gizmo gadget GizmoWorks MultiTouch Photo USA

Self-joins SELECT DISTINCT z.cname FROM Product x, Product y, Company z WHERE z.country = ‘USA’ AND x.category = ‘gadget’ AND y.category = ‘photo’ AND x.manufacturer = z.cname AND y.manufacturer = z.cname; z Product Company x pname category manufacturer Gizmo gadget GizmoWorks SingleTouch photo Hitachi MultiTouch Photo cname country GizmoWorks USA Hitachi Japan y x.pname x.category x.manufacturer y.pname y.category y.manufacturer z.cname z.country Gizmo gadget GizmoWorks MultiTouch Photo USA

Outer joins Product(name, category) Purchase(prodName, store) -- prodName is foreign key SELECT Product.name, Purchase.store FROM Product, Purchase WHERE Product.name = Purchase.prodName There is no match for those products in the purchase table. We want to include products that are never sold, but some are not listed! Why?

Outer joins Product(name, category) Purchase(prodName, store) -- prodName is foreign key SELECT Product.name, Purchase.store FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz Question about WHERE

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz Name Store Gizmo Wiz Output

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz Name Store Gizmo Wiz Output

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz Name Store Gizmo Wiz Output

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz Name Store Gizmo Wiz Output

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz Name Store Gizmo Wiz Camera Ritz Output

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz Name Store Gizmo Wiz Camera Ritz Output

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz Name Store Gizmo Wiz Camera Ritz Output

SELECT Product.name, Purchase.store FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz Name Store Gizmo Wiz Camera Ritz Output

SELECT Product.name, Purchase.store FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz Name Store Gizmo Wiz Camera Ritz OneClick NULL Output

SELECT Product.name, Purchase.store FROM Product FULL OUTER JOIN Purchase ON Product.name = Purchase.prodName Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz Phone Foo Name Store Gizmo Wiz Camera Ritz OneClick NULL Foo Output

Outer Joins Left outer join: Right outer join: Include tuples from tableA even if no match Right outer join: Include tuples from tableB even if no match Full outer join: Include tuples from both even if no match In all cases: Patch tuples without matches using NULL tableA (LEFT/RIGHT/FULL) OUTER JOIN tableB ON p General syntax of joins