Presentation is loading. Please wait.

Presentation is loading. Please wait.

Texas State Technical College DISCOVER! Inner Joins Let’s get together… yeah yeah yeah!

Similar presentations


Presentation on theme: "Texas State Technical College DISCOVER! Inner Joins Let’s get together… yeah yeah yeah!"— Presentation transcript:

1 Texas State Technical College DISCOVER! Inner Joins Let’s get together… yeah yeah yeah!

2 Texas State Technical College DISCOVER! Inner JoinsOverview When joining multiple tables in a query, there are numerous ways to do so. Cartesian ProductCartesian Product Inner JoinInner Join Outer JoinOuter Join The inner join is, by far, the most common join a developer will utilize. It is called a join because each pair of matching records is joined into a new single record in the return set.

3 Texas State Technical College DISCOVER! An inner join relates the records between two tables by setting the criteria to be where 1.the value in the foreign key of one table matches 2.the value of the primary key in the parent table An inner join only returns a composite or joined record when matching/related records are found in BOTH tables being joined. An inner join is also known as a natural join and an equi- join. Inner JoinsOverview

4 Texas State Technical College DISCOVER! Visualization TABLE: Vehicle PRIMARY KEYForeign Key VehicleIDNameManufacturerEngineID Text SVT-C96CobraFordSVT-V8 TM-C96MustangFordTorrence-V6 [ ENGINE ] TABLE: Engine PRIMARY KEY VolumeCylindersManufacturerConfigurationEngineID DecimalIntegerText 3.66FordV-6Torrence-V6 4.28FordV-8Scofield-V8 4.88FordV-8SVT-V8 2.64Chrysler4 CylinderCRY-4C Inner Joins

5 Texas State Technical College DISCOVER! Visualization PRIMARY KEY Foreign Key VehicleIDNameManufacturerEngineID Text SVT-C96CobraFordSVT-V8TextEngineIDPRIMARY KEY V-8TextConfiguration FordTextManufacturer 8IntegerCylinders 4.8DecimalVolume [ ENGINE ] These two records join where fk = pk! This is an inner join. Inner Joins

6 Texas State Technical College DISCOVER! Visualization TABLE: Vehicle PKFK NameVehicleIDMfgEngineID Text CobraSVT-C96FordSVT-V8 MustangTM-C96FordTorrence-V6 TABLE: EnginePK VolumeCylindersConfigurationEngineID DecimalIntegerText 3.66V-6Torrence-V6 4.28V-8Scofield-V8 4.88V-8SVT-V8 2.644 CylinderCRY-4C [ ENGINE ] INNER JOIN RESULTSPKFKPK VolumeCylindersConfigurationEngine.EngineIDVehicle.EngineIDVehicleIDNameMfg DecimalIntegerText 4.88V-8SVT-V8 SVT-C96CobraFord 3.66V-6Torrence-V6 TM-C96MustangFord 4.28V-8Scofield-V8NO MATCH (EXCLUDED FROM RETURN SET) 2.644 CylinderCRY-4CNO MATCH (EXCLUDED FROM RETURN SET) Inner join only returns two matching records. Inner Joins

7 Texas State Technical College DISCOVER! Traditional Joins The join condition is specified in the WHERE clause. This was the original way implemented by SQL. SyntaxSELECT*FROM table1, table2, table3… WHERE -- join criteria -- join criteria ( (table1.pk = table2.fk) AND (table2.pk = table3.fk) AND … ) ( (table1.pk = table2.fk) AND (table2.pk = table3.fk) AND … ) AND AND -- record selection criteria -- record selection criteria (condition) (condition); Inner Joins

8 Texas State Technical College DISCOVER! For Example This statement creates a simple inner join on the engine and vehicle table to match vehicle records to the engine records that describe their motor. SELECT*FROM engine, vehicle WHERE engine.engineID = vehicle.engineID ; Inner Joins

9 Texas State Technical College DISCOVER! For Example We extend the previous statement to only show joined records where the engine volume is greater than 3.6. SELECT*FROM engine, vehicle WHERE (engine.engineID = vehicle.engineID)-- join AND AND (engine.volume > 3.6)-- selection (engine.volume > 3.6)-- selection; Inner Joins

10 Texas State Technical College DISCOVER! ANSI Inner Joins When SQL was standardized, new keywords were added to join tables. The join criteria was removed from the WHERE clause. SyntaxSELECT*FROM table1 JOIN table2 ON (table1.pk = table2.fk) JOIN table3 ON (table2.pk = table3.fk) JOIN table3 ON (table2.pk = table3.fk) …; Inner Joins

11 Texas State Technical College DISCOVER! ANSI Equi-Joins An alternate form of the ANSI method that can only be used when the primary key and foreign key have the same name. The developer specifies the name that is used for both. SyntaxSELECT*FROM table1 JOIN table2 USING (fieldname) ; This form can be problematic on long or complex joins Inner Joins

12 Texas State Technical College DISCOVER! ANSI Natural Joins An alternate form of the ANSI method that can only be used when the primary key and foreign key have the same name. The database automatically determines the field name based on the constraints. SyntaxSELECT*FROM table1 NATURAL JOIN table2 ; This form can be problematic on long or complex joins Inner Joins

13 Texas State Technical College DISCOVER! For Example This statement creates a simple inner join on the engine and vehicle table to match vehicle records to the engine records that describe their motor. SELECT*FROM engine en JOIN vehicle vh ON (en.engineID = vh.engineID) ; Inner Joins

14 Texas State Technical College DISCOVER! For Example We extend the previous statement to only show joined records where the engine volume is greater than 3.6. SELECT*FROM engine en JOIN vehicle vh ON (en.engineID = vh.engineID) WHERE en.volume > 3.6 ; Now we use the WHERE clause solely for selecting which records to retrieve, not for joining and selecting. Inner Joins

15 Texas State Technical College DISCOVER! In Summary… The inner join is the most common form of join used. It joins together record pairs from two related tables into new composite records. It only creates composite records (joined records) for records that are matched or related between two tables. Non-matching records are excluded. It has two syntax forms: traditional and ANSI. Inner Joins


Download ppt "Texas State Technical College DISCOVER! Inner Joins Let’s get together… yeah yeah yeah!"

Similar presentations


Ads by Google