Вы находитесь на странице: 1из 20

TYPES OF JOINS

By: NEHA SHARMA


What is it ?????
___________________________________________________________________________________________________________________________________________________________
___

• JOIN is a means for combining fields from two


tables.

• Using values common to each.

• Most common scenario is a primary key from


one of the tables matches a foreign key in
second table.
TYPES
______________________________________________________________________________________________________________________________________________________________

• Four Types of joins:


1. Inner
2. Outer
3. Left
4. Right

• Self-join-a table can JOIN to itself


Last Name Department ID

Rafferty 31

Jones 33

EMPLOYEE TABLE Steinberg 33

Robinson 34

Smith 34

John NULL

Department ID Department Name

31 Sales
DEPARTMENT
33 Engineering
TABLE
34 Clerical

35 Marketing
INNER JOIN
___________________________________________________________________________________________________________________________________________________________
___

• Query compares each row of A with each row


of B to find all pairs of rows
• Returns only matching rows
• SELECT * FROM employee
INNER JOIN department
ON employee.DepartmentID = department.DepartmentID

• THREE TYPES:
1. EQUI JOIN
2. NATURAL JOIN
3. CROSS JOIN
Employee.DepartmentI Department.Departme Department.Departme
Employee.LastName
D ntName ntID

Robinson 34 Clerical 34

Jones 33 Engineering 33

Smith 34 Clerical 34

Steinberg 33 Engineering 33

Rafferty 31 Sales 31
EQUI JOIN
___________________________________________________________________________________________________________________________________________________________
___

• comparator-based join.
• theta join- that uses only equality
comparisons.
• equal sign as the comparison operator
• < or > disqualifies a join as an equi-join.

• SELECT * FROM employee


INNER JOIN department
USING (DepartmentID)
NATURAL JOIN
___________________________________________________________________________________________________________________________________________________________
___

• specialization of equi-joins

• resulting joined table contains only one


column for each pair of equally-named
columns

• SELECT * FROM employee


NATURAL JOIN department
DepartmentID Employee.LastName Department.DepartmentName

34 Smith Clerical

33 Jones Engineering

34 Robinson Clerical

33 Steinberg Engineering

31 Rafferty Sales
CROSS JOIN
___________________________________________________________________________________________________________________________________________________________
___

• Join returns the cartesian product of the sets


of records from the two joined tables.
• Every row in B with every row in A.
• Number of rows in A times the number of rows
in B.
• SELECT * FROM employee
CROSS JOIN department
OUTER JOIN
___________________________________________________________________________________________________________________________________________________________
___

• Does not require each record in the two joined


tables to have a matching record

• retains each record—even if no other


matching record exists.

• Further divided into:


1. Left outer join
2. Right outer join
3. Full outer join
LEFT OUTER JOIN
_____________________________________________________________________________________________________________________________________________________________________________________

• for table A and B always contains all records of


the "left" table (A)
• even if the join-condition does not find any
matching record in the "right" table (B)
• returns all the values from the left table,
plus matched values from the right table (or
NULL in case of no matching join)
• SELECT * FROM employee
LEFT OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID
Employee.DepartmentI Department.Department Department.Department
Employee.LastName
D Name ID

Jones 33 Engineering 33

Rafferty 31 Sales 31

Robinson 34 Clerical 34

Smith 34 Clerical 34

John NULL NULL NULL

Steinberg 33 Engineering 33
RIGHT OUTER JOINS
___________________________________________________________________________________________________________________________________________________________
___

• Row from the "right" table (B) will appear in


the joined table at least once.

• SELECT * FROM employee


RIGHT OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID
Department.DepartmentN Department.DepartmentI
Employee.LastName Employee.DepartmentID
ame D

Smith 34 Clerical 34

Jones 33 Engineering 33

Robinson 34 Clerical 34

Steinberg 33 Engineering 33

Rafferty 31 Sales 31

NULL NULL Marketing 35


FULL OUTER JOIN
___________________________________________________________________________________________________________________________________________________________
___

• combines the results of both left and right outer


joins
• contain all records from both tables
• fill in NULLs for missing matches on either side.

• SELECT * FROM employee


FULL OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID
Department.Department Department.DepartmentI
Employee.LastName Employee.DepartmentID
Name D

Smith 34 Clerical 34

Jones 33 Engineering 33

Robinson 34 Clerical 34

John NULL NULL NULL

Steinberg 33 Engineering 33

Rafferty 31 Sales 31

NULL NULL Marketing 35


SELF-JOIN
___________________________________________________________________________________________________________________________________________________________
___

• Joining a table to itself


• To find all pairings of two employees in the same
country is desired
EmployeeID LastName Country DepartmentID

123 Rafferty Australia 31

124 Jones Australia 33

145 Steinberg Australia 33

201 Robinson United States 34

305 Smith Germany 34

306 John Germany null


• SELECT F.EmployeeID, F.LastName, S.EmployeeID, S.LastName,
F.Country
FROM Employee F , Employee S
WHERE F.Country = S.Country
AND F.EmployeeID < S.EmployeeID
ORDER BY F.EmployeeID, S.EmployeeID ;
• F and S are aliases for the first and second copies of the employee
table

EmployeeID LastName EmployeeID LastName Country

123 Rafferty 124 Jones Australia

Australia
123 Rafferty 145 Steinberg

Australia
124 Jones 145 Steinberg

305 Smith 306 John Germany


THANKS

Вам также может понравиться