You are on page 1of 44

RELATIONAL ALGEBRA OPERATIONS FROM SET

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.

 If any one of R and S is empty, R × S ⇒ Φ

 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.

 The two operands do NOT have to be "type compatible”


Slide
4 -1
Slide
4 -2
EMPLOYEE DEPARTMENT
Eid Ename Did Deptid Deptname
1 Alex 10 10 Production
2 Alan 20 20 Marketing
X
3 Albert 30 30 HR

perform cartesian product operation on table 'Employee' and table


'Department'

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 Slide
3 Albert 30 20 Marketing 4 -3

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

as part of the join).


 Then optionally remove duplicate attributes

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.

 The result of the JOIN is a relation Q with n + m attributes Q(A1, A2, . . .,


An, B1, B2, . . ., Bm) in that order;

 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)

Eid Ename Did Deptid Deptname


1 Alex 10 10 Production
2 Alan 20 20 Marketing
3 Albert 30 30 HR

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)

Eid Ename Did Deptid Deptname


1 Alex 10 10 Production Slide
4 -8
2 Alan 20 20 Marketing
3 Albert 30 30 HR
 This is the main difference between CARTESIAN
PRODUCT and JOIN:
 in JOIN, only combinations of tuples satisfying the join condition
appear in the result,
 whereas in the CARTESIAN PRODUCT all combinations of
tuples are included in the result.

 The join condition is specified on attributes from the two relations


R and S and is evaluated for each combination of tuples. Each
tuple combination for which the join condition evaluates to true is
included in the resulting relation Q as a single combined tuple.

Slide
4 -9
 A general join condition is of the form:
<condition> AND <condition> AND . . . AND <condition>

where each condition is of the form Ai  Bj, Ai is an attribute of R, Bj is an


attribute of S, Ai and Bj have the same domain, and  (theta) is one of the
comparison operators {=, <, >, }. Tuples whose join attributes are null do
not appear in the result.

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

 θ in Theta join is the join condition. Theta joins combines


tuples from different relations provided they satisfy the theta
condition.
 Notation:

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

 The most common use of join involves join conditions with


equality comparisons only. Such a join, where the only
comparison operator used is =, is called an EQUIJOIN.

 There will be duplication of the joining attributes

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

sid bid day sid sname rating age


22 dustin 7 45.0
22 101 10/10/96
31 lubber 8 55.5
58 103 11/12/96
58 rusty 10 35.0
R1
S1

sid sname rating age bid day


R1 S1 = 22 dustin 7 45.0 101 10/10/96
58 rusty 10 35.0 103 11/12/96
Slide
4 -23
OUTER JOINS

 Outer join overcomes the inability of inner joins of


dropping the tuples which are uncommon among
participating relations. If we want to display those tuples
which are not common, the concept of outer join is used.
 Also, if all the tuples needs to be displayed from all the
participating relations, outer joins can be used.
 They are of three types :
 Left Outer Join,
 Right Outer Join &
 Full Outer Join.

Slide
4 -24
LEFT OUTER JOIN ( )
 To implement left outer join, at least one attribute needs to be
common in the relations.

 All the attributes/tuples present in the left relation are recorded


in the resulting relation along with those which are commonly
present in the right relation.

 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.

 All the attributes/tuples present in the right relation are


recorded in the resulting relation along with those which are
commonly present in the left relation.

 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

 The set of operations including select , project  , union ,


set difference - , and cartesian product X is called a complete
set because any other relational algebra expression can be
expressed by a combination of these five 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.

R <join condition> S = <condition> (R X S)


Slide
4 -31
BINARY RELATIONAL OPERATIONS (CONT.)

 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

DNO ℱ COUNT SSN, AVERAGE Salary (Employee) groups employees by DNO


(department number) and computes the count of employees and
average salary per department. Slide
4 -42
[ Note: count just counts the number of rows, without removing
duplicates]
Slide
4 -43
Slide
4 -44

You might also like