Professional Documents
Culture Documents
Types of Joins: Conally and Begg
Types of Joins: Conally and Begg
◦ 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
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
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.
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:
39