join queries cs 146. introduction: join queries so far, our select queries have retrieved data...

19
Join Queries CS 146

Upload: lauren-mcdowell

Post on 20-Jan-2018

225 views

Category:

Documents


0 download

DESCRIPTION

Main Types of Join Queries  Inner Join  Retrieves all matching fields in joined tables  Also called equijoin or natural join  Outer Join  Retrieves all fields in one table, and matching fields in second table if they exist

TRANSCRIPT

Page 1: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Join Queries

CS 146

Page 2: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Introduction: Join Queries So far, our SELECT queries have retrieved

data from a single table Usually queries combine data from

multiple tables: List how much (pounds) of each product that

was purchased today List the customer name and product name for a

specific purchase Queries that retrieve data from multiple

tables require joining the tables through primary key/foreign key relationships

Page 3: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Main Types of Join Queries Inner Join

Retrieves all matching fields in joined tables Also called equijoin or natural join

Outer Join Retrieves all fields in one table, and matching

fields in second table if they exist

Page 4: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Example Inner JoinCUST_ID CUST_NAME CUST_TYPE CUST_ADDR CUST_ZIP CUST_PHONE USERNAME 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

PROD_ID PROD_DESC PROD_COSTPROD_PRICE1 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$

CANDY_CUSTOMER

CANDY_PURCHASE

CANDY_PRODUCT

Page 5: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Join Query Syntax (ANSI 1992)

The word "INNER" is optional

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

Join condition

Page 6: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Join Query Example (ANSI 1992)

Note: Order of tables in FROM clause doesn’t matter Order of tables in ON condition doesn’t matter

Page 7: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Join Query Syntax (ANSI 1986)

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

Join condition

Page 8: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Join Query Example (ANSI 1986)

Notice the join is specified in the WHERE clause, not in the FROM clause

Page 9: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Advantages of 1992 SyntaxSeparates the join conditions and search

conditionsMakes it impossible to omit a join

condition

Required for this class

Page 10: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Qualifying Field Names What if a join query retrieves a field that

exists in both tables?

Page 11: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Qualifying Field Names You qualify the field name in the SELECT

clause Preface the field name with the name of either

table

Page 12: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Shorthand way to write queries by abbreviating table names

Pros & cons?

Table Aliases

NOTE:Once you createa table alias, youhave to use it everywhere…

Page 13: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

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 Column1, Column2, …FROM Table1 INNER JOIN Table2ON Table1.JoinColumn = Table2.JoinColumnINNER JOIN Table3ON Table2.JoinColumn = Table3.JoinColumnWHERE SearchCondition(s)

Page 14: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

3 Table Inner Join Example

Page 15: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Joining N Tables You can join any number of tables, provided

primary key/foreign key relationships exist Challenge:

Including all necessary tables in the query

Page 16: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

You can join any number of tables, provided primary key/foreign key relationships exist

Challenge: you need to include table in join queries to provide needed links even if you don't include fields in the SELECT clause…

Joining N Tables

Page 17: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Example

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 18: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Designing Complex Join Queries Terminology:

Display field: Retrieved data field Appears in the SELECT clause

Join field Primary or foreign key used to join tables Appears in a join condition

Search field Used in a search condition Appears in the WHERE clause

Join queries must include all tables that contain display, join, or search fields

Page 19: Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from…

Query Design Diagrams Visual way to identify display, join, and

search fields Process:

1. Identify every table in the query2. Identify every involved field in each table

Label whether it is a display, search, or join field3. Create join condition links