Professional Documents
Culture Documents
THEORY (CONT.)
CARTESIAN (or cross product) Operation
The product of two relations is a cartesian product and it is the
concatenation of every tuple of one relation with every tuple of a second
relation.
If the relation R has m tuples and the relation S has n tuple then R × S
have m times n tuples.
The result of R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm) is a relation Q with
degree n + m attributes Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
3 Albert 30 30 HR
BINARY RELATIONAL OPERATIONS
JOIN Operation
Join operation is used to join two tables.
This operation is performed if both tables have similar column.
The join operation is performed by doing the cartesian product,
selection and possibly projection operations.
Slide
4 -4
BINARY RELATIONAL OPERATIONS
JOIN Operation
Letus consider two relation A and B
The join of the relations A and B is as follows :
Form the product A, B i.e. A × B.
Do a selection to eliminate some tuples (the criteria for the selection are specified
Slide
4 -5
The general form of a join operation on two relations
R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R <join⋈condition>S
where R and S can be any relations that result from general relational
algebra expressions.
Q has one tuple for each combination of tuples—one from R and one from
S—whenever the combination satisfies the join condition.
Slide
4 -6
EMP DEPT
Eid Ename Did Deptid Deptname
1 Alex 10 10 Production
2 Alan 20 20 Marketing
3 Albert 30 30 HR
EMP DEPT
(EMP.Did =DEPT.Deptid)
Slide
4 -7
EMP X DEPT
Eid Ename Did Deptid Deptname
1 Alex 10 10 Production
1 Alex 10 20 Marketing
1 Alex 10 30 HR
2 Alan 20 10 Production
2 Alan 20 20 Marketing
2 Alan 20 30 HR
3 Albert 30 10 Production
3 Albert 30 20 Marketing
3 Albert 30 30 HR
EMP DEPT
(EMP.Did =DEPT.Deptid)
Slide
4 -9
A general join condition is of the form:
<condition> AND <condition> AND . . . AND <condition>
Slide
4 -10
TYPES OF JOINS
Slide
Equi Join Full Outer Join 4 -11
TYPES OF JOINS
Inner Joins :
An inner join includes those tuples with matching attributes and the rest
are discarded in the resulting relation.
Theta Join
Equi-join
Natural Join
Slide
4 -12
THETA JOIN
R1 ⋈ θ R2
where R1 and R2 are relations with their
attributes (A1, A2, .., An ) and (B1, B2,.. ,Bn)
Theta join can use all kinds of comparison operators.
Slide
4 -13
Slide
4 -14
A) CROSS PRODUCT
Slide
4 -15
Slide
4 -16
Slide
4 -17
EQUIJOIN OPERATION
Slide
4 -18
EQUIJOIN JOIN - EXAMPLE
Equijoin:
Equijoin Join condition is a conjunction of equalities.
Name,CrsCode(Student Id=StudId Grade=‘A’ (Transcript))
Transcript
19
Student Transcript
Id Name Addr Status
StudId CrsCode Sem
111 John ….. ….. Grade
222 Mary ….. ….. 111 CSE305 S00 B
333 Bill ….. ….. 222 CSE306 S99 A
444 Joe ….. ….. 333 CSE304 F99 A
.
EQUIJOIN JOIN - EXAMPLE
Equijoin:
Equijoin Join condition is a conjunction of equalities.
Name,CrsCode(Student Id=StudId Grade=‘A’ (Transcript))
Transcript
Student Transcript
Id Name Addr Status StudId CrsCode Sem Grade
111 John ….. ….. 111 CSE305 S00 B
222 Mary ….. ….. 222 CSE306 S99 A
333 Bill ….. ….. 333 CSE304 F99 A
444 Joe ….. …..
.
Mary CSE306
Bill CSE304
NATURAL JOIN (*/⋈)
( R*S) OR (R ⋈ S )
Natural join does not use any comparison operator.
a Natural Join is performed only if there is at least
one common attribute that exists between two
relations.
In addition, the attributes must have the same name
and domain.
Natural join acts on those matching attributes where
the values of attributes in both the relations are same.
Slide
4 -21
NATURAL JOIN EXAMPLE
Slide
4 -24
LEFT OUTER JOIN ( )
To implement left outer join, at least one attribute needs to be
common in the relations.
If in case any tuple in left relation does not matches with the
tuple in right relation, NULL value will be displayed against
that tuple in the resulting relation.
Slide
4 -25
Slide
4 -26
RIGHT OUTER JOIN ( )
To implement right outer join, at least one attribute needs to be
common in the relations.
If in case any tuple in right relation does not matches with the
tuple in left relation, NULL value will be displayed against
that tuple in the resulting relation.
Slide
4 -27
Slide
4 -28
FULL OUTER JOIN( )
Full outer join is equal to Left outer join as well as right outer
join i.e. All the tuples from both relations are included in the
resulting relation.
If the tuples failed in the join condition for both relations,
then their respective unmatched attributes are made NULL
Slide
4 -29
Slide
4 -30
COMPLETE SET OF RELATIONAL
OPERATIONS
For example:
Intersection operation can be expressed by using UNION and MINUS
R S = (R S ) – ((R S) (S R))
A Natural Join can be specified as a CARTESIAN PRODUCT preceded and followed by SELECT
and PROJECT operations.
DIVISION Operation
Slide
4 -32
Slide
4 -33
Slide
4 -34
Slide
4 -35
Slide
4 -36
Slide
4 -37
RECAP OF RELATIONAL ALGEBRA OPERATIONS
Slide
4 -38
ADDITIONAL RELATIONAL OPERATIONS
Aggregate Functions and Grouping
Slide
4 -39
Slide
4 -40
Slide
4 -41
ADDITIONAL RELATIONAL OPERATIONS
(CONT.)
Use of the Functional operator ℱ
ℱ MAX Salary (Employee) retrieves the maximum salary value from the
Employee relation
ℱ MIN Salary (Employee) retrieves the minimum Salary value from the
Employee relation
ℱ SUM Salary (Employee) retrieves the sum of the Salary from the
Employee relation