You are on page 1of 39

Types of Joins

CONALLY AND BEGG


JOINS
◦ Binary operations

◦ Combination of Cartesian product + Selection criteria

◦ A Join operation pairs two tuples from different relations, if and only if a given join condition is
satisfied

◦ Similar to Cartesian product except the fact that in Cartesian product, we get all the possible
combinations of relations while in join only those combinations can be formed that satisfies some
matching conditions
Joins
To combine columns from several tables into a result table, we need to use a join operation
The SQL join operation combines information from two tables by forming pairs of related rows
from the two tables
The row pairs that make up the joined table are those where the matching columns in each of
the two tables have the same value
Joins cont..
More than one table name in the FROM clause, using a comma as a separator
WHERE clause to specify the join column(s)
Possible to use an alias for a table named in the FROM clause
◦ An alias can be used to qualify a column name whenever there is ambiguity regarding the source of the column name

Steps followed by join operations


Cartesian Product
Certain Selection Criteria
Remove Duplicates
Joins Example Foreign
Primary Key
◦ EMPLOYEE Key DEPARTMENT

Emp_Id Emp_Name Address Dep_Id Dep_Name Emp_Id


1 Subhan Multan D1 HR 1
2 Sikandar Sargodha D2 IT 2
3 Sultan Faisalabad D3 Marketing 4
4 Salar Karachi D4 Finance 5
5 Ahmad Lahore D5 HR 3

Find emp_name from Employee who is working in HR Department


Classification of Joins
We can classify joins basically into two types
INNER JOINS: These joins are the one that has the tuples that satisfy some conditions and rest are
discarded . Further they are classified as
◦ Theta join
◦ Equi join
◦ Natural join

OUTER JOINS: These have all the tuples from either or both the relations. Further they are classified
as
◦ Left outer join
◦ Right outer join
◦ Full outer join
Classification of Joins
Cross Join
The SQL CROSS JOIN produces a result set which is the number of rows in the
first table multiplied by the number of rows in the second table
If no WHERE clause is used along with CROSS JOIN. This kind of result is called as
Cartesian Product
If WHERE clause is used with CROSS JOIN, it functions like an INNER JOIN
Syntax
◦ SELECT *
◦ FROM table1
◦ CROSS JOIN table2;
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v

s105d01
... 9
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x

s105d01
... 10
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y

s105d01
... 11
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v

s105d01
... 12
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x

s105d01
... 13
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y

s105d01
... 14
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v

s105d01
... 15
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x

s105d01
... 16
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y

s105d01
... 17
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v
s105d01
... 18
Table One Table Two
X A X B
1 a 2 x
4 d 3 rows 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v

... 20
Table One Table Two
X A X B
1 a 2 x
4 d 3 rows 3 rows 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v

... 21
Table One Table Two
X A X B
1 a 2 x
4 d 3 rows X 3 rows 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y 9 rows
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v

22
Cross Join cont…
The number of rows in a Cartesian product is the product of the number of rows in the
contributing tables.
3x3=9
1,000 x 1,000 = 1,000,000
100,000 x 100,000 = 10,000,000,000

A Cartesian product is rarely the desired result of a query.

23
Example
Cross Join
Theta Join
Notation:
Theta Join allows you to merge two tables based on the condition represented by theta.
The Theta join operation defines a relation that contains tuples satisfying the predicate F from the
Cartesian product of r and S
We apply the condition through select on one relation and then only those rows are used in the cross
product with the second relation
We can rewrite the Theta join in terms of the basic Selection and Cartesian product
operations:

Steps
◦ Cross Product
◦ Check Predicate
Theta Join ExampleCID Course TItle FId
Find the data where Rank=AP
C234 Operating System F234
5 5
C345 Database Systems F345
6 6
C456 Financial Management F434
7
FacId Fname Dept Salar Ran 7
C567 Money and Capital
y k F347
8 Market IT
F234 Umar 8765 AP 3
5
F345 Tahir Man 4343 Lect
6 g
Theta Join Example cont…
FacId Fname Dept Salary Ran CID C_TItl FId
k e
F234 Umar IT 8765 AP C234 OS F234
5 5 5
F234 Umar IT 8765 AP C345 DS F345
5 6 6
F234 Umar IT 8765 AP C456 FM F434
5 7 7
F234 Umar IT 8765 AP C567 MC F347
5 8 &M 3
F567 Sama IT 7546 AP C234 OS F234
8 d 5 5
Equi Join
In the case where the predicate F contains only equality (5), the term Equijoin is used instead

EQUI JOIN performs a JOIN against equality or matching column(s) values of the associated
tables
An equal sign (=) is used as comparison operator in the where clause to refer equality
Syntax:
◦ SELECT column_list
◦ FROM table1, table2....
◦ WHERE table1.column_name = table2.column_name;
Equi Join
Example
Natural Join
The Natural join is an Equijoin of the two relations r and S over all common attributes x. One
occurrence of each common attribute is eliminated from the result.

Natural Join: Guidelines


- The associated tables have one or more pairs of identically named columns.
- The columns must be the same data type.
Natural Join
Example
Outer Join
We may want tuples from one of the relations to appear in the result even when there are no
matching values in the other relation
LEFT OUTER JOIN:
The (left) Outer join is a join in which tuples from r that do not have matching values in the
common attributes of S are also included in the result relation. Missing values in the second
relation are set to null.
LEFT OUTER JOIN (Example)
OUTER JOIN Cont…
RIGHT OUTER JOIN: Right Outer join that keeps every tuple in the right-hand
relation in the result.

Full Outer join that keeps all tuples in both relations, padding tuples with nulls
when no matching tuples are found.
Relational Algebra Operations
Relational Algebra Operations
Inner Join vs Natural Join
Compare Inner Joins And Outer Joins
The following table is a comparison of inner and outer join syntax and limitations:

Key Point Inner Join Outer Join


Table Limit 256 2
Join Behavior Returns matching rows Returns matching and
only nonmatching rows
Join Options Matching rows only LEFT, FULL, RIGHT
Syntax changes  Multiple tables in the ON clause that
FROM clause specifies join criteria
 WHERE clause that
specifies join criteria

39

You might also like