Multiple Table Queries 1: Inner Joins CS 320
Introduction: Join Queries Usually queries combine data from multiple tables: List how much (pounds) of each product that was purchased on a given date List the customer name, product name, and pounds for a specific purchase Queries that retrieve data from multiple tables require joining the tables through primary key/foreign key relationships This is called in inner join or equijoin
Example Inner Join CANDY_CUSTOMER CANDY_PURCHASE CANDY_PRODUCT
Join Query General Syntax (ANSI 1992) The word "INNER" is optional SELECT field1, field2, … FROM Table1 INNER JOIN Table2 ON Table1.JoinColumn = Table2.JoinColumn WHERE SearchCondition(s)
Join Query Example (ANSI 1992) Notes: Order of tables in FROM clause doesn’t matter Order of tables in ON condition doesn’t matter SELECT purch_id, purch_date, prod_desc FROM candy_purchase JOIN candy_product ON candy_purchase.prod_id = candy_product.prod_id;
Older Join Query Syntax (ANSI 1986) SELECT Column1, Column2, … FROM Table1, Table2 WHERE Table1.JoinColumn = Table2.JoinColumn AND SearchCondition(s)
Older Join Query Syntax Example (ANSI 1986) Note that the join condition is specified in the WHERE clause rather than the FROM clause SELECT purch_id, purch_date, prod_desc FROM candy_purchase, candy_product WHERE candy_purchase.prod_id = candy_product.prod_id;
Advantages of 1992 Syntax Separates the join conditions from the search conditions Makes it impossible to omit a join condition Required for this class
What Happens If You Omit a Join Condition (1986 Syntax)? Creates a Cartesian product Table1 records X Table2 records
Qualifying Field Names If a join query lists a field in the SELECT clause that appears in both tables, an error occurs To avoid this error, you need to qualify the field name: SELECT table_name.field_1, … SELECT candy_product.prod_id, purch_id, prod_desc FROM candy_product JOIN candy_purchase ON candy_product.prod_id = candy_purchase.prod_id;
Provide a shorthand way to write queries by abbreviating table names Once you create a table alias, you have to use it everywhere! Table Aliases SELECT field1, field2, … FROM table1 alias1 JOIN table2 alias2 ON alias1.join_field = alias2.join_field SELECT prod.prod_id, purch_id, prod_desc FROM candy_product prod JOIN candy_purchase purch ON prod.prod_id = purch.prod_id;
Inner Join of 3 Tables General syntax: Note: Placing each INNER JOIN and ON clause on a separate line makes the query easier to read and understand SELECT field1, field2, … FROM table1 JOIN table2 ON table1.join_field_a = table2.join_field_a JOIN table3 ON table2.join_field_b = table3.join_field_b WHERE search_condition(s)
3 Table Inner Join Example SELECT purch_date, cust_name, prod_desc FROM candy_purchase purch JOIN candy_customer cust ON purch.cust_id = cust.cust_id JOIN candy_product prod ON purch.prod_id = prod.prod_id WHERE purch_date = ' ';
Joining N Tables You can join any number of tables, provided primary key/foreign key relationships exist Challenge: determining which tables you need to include in the query SELECT field tables Search field tables Tables that are needed for intermediate joins
Example: Find the product descriptions of all products purchased by customer "Bobby Bon Bons" CANDY_PRODUCT prod_desc (D) prod_id (J) CANDY_CUSTOMER cust_name (S) cust_id (J) CANDY_PURCHASE prod_id (J) cust_id (J) SELECT prod_desc FROM candy_product INNER JOIN candy_purchase ON candy_product.prod_id = candy_purchase.prod_id INNER JOIN candy_customer ON candy_purchase.cust_id = candy_customer.cust_id WHERE cust_name = 'Bobby Bon Bons'
Creating Query Design Diagrams Process: 1. Identify every table in the query that contains a display field or search field 2. Add additional tables containing join fields as needed to link all tables through primary key/foreign key links
Your Turn 1: Create a query design diagram and associated SQL query to retrieve the description of every product purchase by customers of type "Wholesale." Use "Wholesale" (rather than "W") as the search condition.