join queries
DESCRIPTION
Join Queries. CS 146. 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 - PowerPoint PPT PresentationTRANSCRIPT
Join Queries
CS 146
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
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
Example Inner Join
CUST_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_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$
CANDY_CUSTOMER
CANDY_PURCHASE
CANDY_PRODUCT
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
Join Query Example (ANSI 1992)
Note: Order of tables in FROM clause doesn’t matter Order of tables in ON condition doesn’t matter
Join Query Syntax (ANSI 1986)
SELECT Column1, Column2, …FROM Table1, Table2WHERE Table1.JoinColumn = Table2.JoinColumnAND SearchCondition(s)
Join condition
Join Query Example (ANSI 1986)
Notice the join is specified in the WHERE clause, not in the FROM clause
Advantages of 1992 Syntax
Separates the join conditions and search conditions
Makes it impossible to omit a join condition
Required for this class
Qualifying Field Names
What if a join query retrieves a field that exists in both tables?
Qualifying Field Names You qualify the field name in the SELECT
clause Preface the field name with the name of either
table
Shorthand way to write queries by abbreviating table names
Pros & cons?
Table Aliases
NOTE:Once you createa table alias, youhave to use it everywhere…
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)
3 Table Inner Join Example
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
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
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'
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
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 field
3. Create join condition links