Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Key Areas
What are Joins Types of Joins Examples of Joins Potential problems with Joins
A concept in relational database theory Technique for accessing 2+ tables in a single answer set Each answer set row may contain data from each table Joined on join column
ANSI 89 Standard
ANSI 92 Standard
Requires a Join command Requires a From clause All factors equal, performance the same
Types Of Joins
We will discuss the following Joins: Inner Join Outer Joins Full Left Right Cross Join Self Join
Inner Joins
Outer Joins
Full: Both tables used to qualify and both extended with Nulls Left: Left table used to qualify and right table has Nulls for non-matching rows Right: Right table used to qualify and left table has Nulls for non-matching rows
Example:
Select employee.lname, employee.fname From employee Full Outer Join department On employee.departmentnum=department.departmentnum;
Example:
Select employee.lname, employee.fname From employee Left Outer Join department On employee.departmentnum = department.departmentnum;
Example:
Select employee.lname, employee.fname From employee Right Outer Join department On employee.departmentnum = department.departmentnum;
Cross Join
Each row of one table matched with each row of another table (no ON clause) Example:
Select employee.lname, employee.fname From employee Cross Join department Where employee.employee_number = 1008;
Self Join
Alias Names
Temporary name for Table or View Defined in From clause Useful as abbreviation Required in Self Join Once defined, must be used in SQL where table name required
A join can have up to 64 participating tables An n-table join requires n-1 join conditions Omitting a join condition will result in a Cartesian Product Join
Example:
Select employee.lname, employee.fname From employee Inner Join department On employee.departmentnum = department.departmentnum Inner Join job On employee.jobcode = job.jobcode;
How:
Cross Join with no Where clause Improper aliasing Omitting a Join condition Improper Join condition
Improper aliasing:
Select employee.lname, employee.fname From employee emp Inner Join department dep On emp.departmentnum = dep.departmentnum
Subquery answer set only displays from outer query table Subquery easier to write (?) All things equal, Joins are usually better performers
Summary
Inner Join Full Outer Join Left Outer Join Right Outer Join Cross Join Self Join
Summary
Potential problems