Cse 344 January 12th –joins.

Slides:



Advertisements
Similar presentations
1 Lecture 03: SQL Friday, January 7, Administrivia Have you logged in IISQLSRV yet ? HAVE YOU CHANGED YOUR PASSWORD ? Homework 1 is now posted.
Advertisements

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.
1 Lecture 2: SQL Wednesday, January 7, Agenda Leftovers from Monday The relational model (very quick) SQL Homework #1 given out later this week.
1 Lecture 3: More SQL Friday, January 9, Agenda Homework #1 on the web site today. Sign up for the mailing list! Next Friday: –In class ‘activity’
1 Information Systems Chapter 6 Database Queries.
CSE544: SQL Monday 3/27 and Wednesday 3/29, 2006.
1. Midterm summary Types of data on the web: unstructured, semi- structured, structured Scale and uncertainty are key features Main goals are to model,
1 SQL cont.. 2 Outline Unions, intersections, differences (6.2.5, 6.4.2) Subqueries (6.3) Aggregations (6.4.3 – 6.4.6) Hint for reading the textbook:
IM433-Industrial Data Systems Management Lecture 5: SQL.
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)
SQL SQL Review. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
1 SQL Additional Notes. 2  1 Group and Aggregation*  2 Execution Order*  3 Join*  4 Find the maximum  5 Line Format SQL Additional Notes *partially.
Hassan Tariq MULTIPLE TABLES: SQL provides a convenient operation to retrieve information from multiple tables.SQL provides a convenient operation to.
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.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
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.
Lecture 05: SQL Wednesday, January 12, 2005.
Cours 7: Advanced SQL.
Lecture 04: SQL Monday, January 10, 2005.
Database Systems Subqueries, Aggregation
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
Class 2 Relational Data Languages
Introduction to Database Systems CSE 444 Lecture 04: SQL
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
March 30th – intro to joins
Cse 344 January 10th –joins.
Introduction to Database Systems CSE 444 Lecture 03: SQL
January 19th – Subqueries 2 and relational algebra
January 17th – Subqueries
Lecture 4: Advanced SQL – Part II
Introduction to SQL Wenhao Zhang October 5, 2018.
Introduction to Database Systems CSE 444 Lecture 03: SQL
CSE544 SQL Wednesday, March 31, 2004.
Lecture 05 Views, Constraints
Lecture 12: SQL Friday, October 20, 2000.
Introduction to Relational Databases
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.
Lectures 6: Introduction to SQL 5
Lecture 3 Monday, April 8, 2002.
Class 2 Relational Data Languages
CS639: Data Management for Data Science
Sampath Jayarathna Cal Poly Pomona
Lecture 06: SQL Monday, October 11, 2004.
Lecture 4: SQL Wednesday, April 10, 2002.
Lecture 03: SQL Friday, October 3, 2003.
Terminology Product Attribute names Name Price Category Manufacturer
Lectures 2: Introduction to SQL 1
Lecture 04: SQL Monday, October 6, 2003.
Lecture 05: SQL Wednesday, October 9, 2002.
Lecture 14: SQL Wednesday, October 31, 2001.
Presentation transcript:

Cse 344 January 12th –joins

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 sometimes 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

Grouping and Aggregation Purchase(product, price, quantity) Find total quantities for all sales over $1, by product.

Grouping and Aggregation Product Price Quantity Bagel 3 20 1.50 Banana 0.5 50 2 10 4 Product TotalSales Bagel 40 Banana 20 First we apply the selection predicate to eliminate the third row, then we form groups of products, and then evaluate the aggregates and projection SELECT product, Sum(quantity) AS TotalSales FROM Purchase WHERE price > 1 GROUP BY product

Other Examples Compare these two queries: SELECT product, count(*) FROM Purchase GROUP BY product SELECT month, count(*) FROM Purchase GROUP BY month SELECT product, sum(quantity) AS SumQuantity, max(price) AS MaxPrice FROM Purchase GROUP BY product What does it return?

Need to be Careful… SELECT product, max(quantity) FROM Purchase GROUP BY product Product Price Quantity Bagel 3 20 1.50 Banana 0.5 50 2 10 4 Can’t project columns not in grouping / aggregate

Need to be Careful… SELECT product, max(quantity) FROM Purchase GROUP BY product Product Price Quantity Bagel 3 20 1.50 Banana 0.5 50 2 10 4 SELECT product, quantity FROM Purchase GROUP BY product -- what does this mean? Can’t project columns not in grouping / aggregate

Need to be Careful… SELECT product, max(quantity) FROM Purchase GROUP BY product Product Price Quantity Bagel 3 20 1.50 Banana 0.5 50 2 10 4 SELECT product, quantity FROM Purchase GROUP BY product -- what does this mean? Can’t project columns not in grouping / aggregate Product Max(quantity) Bagel 20 Banana 50

Need to be Careful… SELECT product, max(quantity) FROM Purchase GROUP BY product Product Price Quantity Bagel 3 20 1.50 Banana 0.5 50 2 10 4 SELECT product, quantity FROM Purchase GROUP BY product -- what does this mean? Can’t project columns not in grouping / aggregate Product Max(quantity) Bagel 20 Banana 50 Product Quantity Bagel 20 Banana ??

Need to be Careful… SELECT product, max(quantity) FROM Purchase Everything in SELECT must be either a GROUP-BY attribute, or an aggregate Need to be Careful… SELECT product, max(quantity) FROM Purchase GROUP BY product Product Price Quantity Bagel 3 20 1.50 Banana 0.5 50 2 10 4 SELECT product, quantity FROM Purchase GROUP BY product -- what does this mean? Can’t project columns not in grouping / aggregate Product Max(quantity) Bagel 20 Banana 50 Product Quantity Bagel 20 Banana ??

Grouping and Aggregation Purchase(product, price, quantity) Find total quantities for all sales over $1, by product. SELECT product, Sum(quantity) AS TotalSales FROM Purchase WHERE price > 1 GROUP BY product How is this query processed?

Grouping and Aggregation Purchase(product, price, quantity) Find total quantities for all sales over $1, by product. SELECT product, Sum(quantity) AS TotalSales FROM Purchase WHERE price > 1 GROUP BY product Do these queries return the same number of rows? Why? SELECT product, Sum(quantity) AS TotalSales FROM Purchase GROUP BY product

Grouping and Aggregation Purchase(product, price, quantity) Find total quantities for all sales over $1, by product. SELECT product, Sum(quantity) AS TotalSales FROM Purchase WHERE price > 1 GROUP BY product Do these queries return the same number of rows? Why? SELECT product, Sum(quantity) AS TotalSales FROM Purchase GROUP BY product Empty groups are removed, hence first query may return fewer groups

Grouping and Aggregation 1. Compute the FROM and WHERE clauses. 2. Group by the attributes in the GROUPBY 3. Compute the SELECT clause: grouped attributes and aggregates. FWGS = From Where Group by Select (I made up that acronym) TM FWGS CSE 344 - 2017au

1,2: From, Where FWGS Product Price Quantity Bagel 3 20 1.50 Banana 0.5 50 2 10 4 WHERE price > 1 SELECT product, Sum(quantity) AS TotalSales FROM Purchase WHERE price > 1 GROUP BY product

3,4. Grouping, Select FWGS Product Price Quantity Bagel 3 20 1.50 Banana 0.5 50 2 10 4 Product TotalSales Bagel 40 Banana 20 SELECT product, Sum(quantity) AS TotalSales FROM Purchase WHERE price > 1 GROUP BY product