Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine...

17
Multiple Table Queries 1: Inner Joins CS 320

Transcript of Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine...

Page 1: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

Multiple Table Queries 1: Inner Joins

CS 320

Page 2: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

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

Page 3: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

PURCH_ID PROD_ID CUST_IDPURCH_ DATE

PURCH_ DELIVERY_DATE

PURCH_ POUNDS

PURCH_ STATUS

1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID

CUST_ID CUST_NAME CUST_TYPE CUST_ADDR CUST_ZIP CUST_PHONE CUST_USERNAME CUST_PASSWORD

1 Jones, Joe P 1234 Main St. 91212 434-1231 jonesj 12342 Armstrong,Inc. R 231 Globe Blvd. 91212 434-7664 armstrong 33333 Sw edish Burgers R 1889 20th N.E. 91213 434-9090 sw edburg 23534 Pickled Pickles R 194 CityView 91289 324-8909 pickpick 53335 The Candy Kid W 2121 Main St. 91212 563-4545 kidcandy 23516 Waterman, Al P 23 Yankee Blvd. 91234 w ateral 89007 Bobby Bon Bons R 12 Nichi Cres. 91212 434-9045 bobbybon 30118 Crow sh, Elias P 7 77th Ave. 91211 434-0007 crow el 10339 Montag, Susie P 981 Montview 91213 456-2091 montags 9633

10 Columberg Sw eets W 239 East Falls 91209 874-9092 columsw e 8399

Example Inner Join CANDY_CUSTOMER

CANDY_PURCHASE

CANDY_PRODUCTPROD_ID PROD_DESC PROD_COSTPROD_PRICE

1 Celestial Cashew Crunch 7.45$ 10.00$

2 Unbrittle Peanut Paradise 5.75$ 9.00$

3 Mystery Melange 7.75$ 10.50$

4 Millionaire’s Macadamia Mix 12.50$ 16.00$

5 Nuts Not Nachos 6.25$ 9.50$

Page 4: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

Join Query General Syntax (ANSI 1992)

The word "INNER" is optional

SELECT field1, field2, …FROM Table1 INNER JOIN Table2ON Table1.JoinColumn = Table2.JoinColumnWHERE SearchCondition(s)

Page 5: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

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_descFROM candy_purchase JOIN candy_productON candy_purchase.prod_id = candy_product.prod_id;

Page 6: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

Older Join Query Syntax (ANSI 1986)

SELECT Column1, Column2, …FROM Table1, Table2WHERE Table1.JoinColumn = Table2.JoinColumnAND SearchCondition(s)

Page 7: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

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_descFROM candy_purchase, candy_productWHERE candy_purchase.prod_id = candy_product.prod_id;

Page 8: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

Advantages of 1992 Syntax Separates the join conditions from the search

conditions Makes it impossible to omit a join condition

Required for this class

Page 9: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

What Happens If You Omit a Join Condition (1986 Syntax)?

Creates a Cartesian productTable1 records X Table2 records

Page 10: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

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_descFROM candy_product JOIN candy_purchaseON candy_product.prod_id = candy_purchase.prod_id;

Page 11: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

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 alias2ON alias1.join_field = alias2.join_field

SELECT prod.prod_id, purch_id, prod_descFROM candy_product prod JOIN candy_purchase purchON prod.prod_id = purch.prod_id;

Page 12: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

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 table2ON table1.join_field_a = table2.join_field_aJOIN table3ON table2.join_field_b = table3.join_field_bWHERE search_condition(s)

Page 13: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

3 Table Inner Join Example

SELECT purch_date, cust_name, prod_descFROM candy_purchase purch JOIN candy_customer custON purch.cust_id = cust.cust_idJOIN candy_product prodON purch.prod_id = prod.prod_idWHERE purch_date = '2004-10-28';

Page 14: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

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 querySELECT field tablesSearch field tablesTables that are needed for intermediate joins

Page 15: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

Example: Find the product descriptions of all products purchased by customer "Bobby Bon Bons"

CANDY_PRODUCTprod_desc (D)prod_id (J)

CANDY_CUSTOMERcust_name (S)cust_id (J)

CANDY_PURCHASEprod_id (J)cust_id (J)

SELECT prod_descFROM candy_product INNER JOIN candy_purchaseON candy_product.prod_id = candy_purchase.prod_idINNER JOIN candy_customerON candy_purchase.cust_id = candy_customer.cust_idWHERE cust_name = 'Bobby Bon Bons'

Page 16: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

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

Page 17: Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

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.