0% found this document useful (0 votes)
10 views

Relational Algebra Binary Ops

Uploaded by

rim.moussa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Relational Algebra Binary Ops

Uploaded by

rim.moussa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Company

LOGO
Relational Algebra
and Relational Calculus

Part II: Binary Operations


Dr. Rim Moussa
Outline
● Unary operations
● SELECT
● PROJECT
● Binary Operations
● CROSS PRODUCT (aka CARTESIAN PRODUCT, CROSS JOIN)
● JOIN
● INNER JOIN (aka EQUIJOIN)
● LEFT | RIGHT OUTER JOIN
● THETA JOIN
● SEMI-JOIN and ANTI-JOIN
● SET Operations
● UNION
● INTERSECTION
● DIFFERENCE (aka MINUS, EXCEPT)
● DIVISION
2● Existential and Universal Quantifiers
CROSS PRODUCT Operation
● The Cartesian-product operation, denoted by a cross (×), allows us
to combine information from any two relations. We write the
Cartesian product of relations r1 and r2 as r1 × r2.
● Query: All combinations of Instructor and teaches

RA Expression: instructor × teaches

relation relation

SQL statement:
SELECT *
FROM instructor, teaches;

3
CROSS PRODUCT Operation
Instructor teaches

Cardinality(Instructor) = |Instructor| = 12
Cardinality(teaches) = |teaches| =15

Cross product (all combinations): 12 x 15 = 180


records
4
CROSS PRODUCT Operation

instructor × teaches

Nota:
● Table.Attribute
notation
● Instructor.ID: ID
attribute in Instructor
relation
● Teaches.ID: ID
attribute in teaches
relation

5
JOIN Operation
● The Cartesian product associates every instructor with every course
that was taught, regardless of whether that instructor taught that
course.

Equal?
=
=
=


● Instructor 10101 teaches courses: CS-101, CS-315, CS-347


● Instructor 10101 doesn't teache courses: FIN-201, MU-199,PHY-101
● The equi-join operation is SELECT (σ) applied to Cartesian product
RA Expression:
σ instructor.ID = teaches.ID (instructor × teaches) or

6 instructor ⋈ instructor.ID = teaches.ID teaches


EQUIJOIN Operation
SQL statement:
SELECT *
FROM instructor, teaches
WHERE instructor.ID = teaches.ID;

7
THETA-JOIN Operation
● A JOIN operation with a general join condition is called a THETA
JOIN .
R(A1, A2, … An)
S(B1, B2, …, Bm)
J = R ⋈ condition S
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 {=, <, ≤, >, ≥, ≠}.

8
SEMI-JOIN
● The result contains the attributes of one table
● Query: Instructors who teach
RA expression:
σ instructor.ID  Π {teaches.ID} (teaches) (instructor)

instructor ⋉ teaches (-->contains only attributes of Instructor)


SQL statement:
SELECT *
FROM instructor
WHERE instructor.ID IN (SELECT teaches.ID FROM teaches);
---
SELECT *
FROM instructor i
WHERE EXISTS (SELECT *
9 FROM teaches WHERE i.ID = t.ID);
ANTI-JOIN
● Query: Instructors who do not teach
RA expression:
σ instructor.ID  Π {teaches.ID} (teaches) (instructor)

instructor ▷ teaches (-->contains only attributes of Instructor)


SQL statement:
SELECT *
FROM instructor
WHERE instructor.ID NOT IN (SELECT teaches.ID FROM teaches);
---
SELECT *
FROM instructor i
WHERE NOT EXISTS (SELECT *
FROM teaches WHERE i.ID = t.ID);
10
AUTO-JOIN
● Query: Pairs of Instructors who are in the same department

RA expression:
i1 ← instructor
i2 ← instructor
result ← σ ( i1 ⋈ i2)
i1.ID < i2.ID i1.dept_name = i2.dept_name

SQL statement:
SELECT i1.ID, i1.name,i2.ID, i2.name, i1.dept_name
FROM instructor i1, instructor i2
WHERE i1.dept_name = i2.dept_name
AND i1.ID < i2.ID;

11
SET UNION Operation
● The result of UNION operation, denoted by R ∪ S, is a relation that
includes all tuples that are either in R or in S or in both R and S.
Duplicate tuples are eliminated.
RA expression R = Π {Fnane,Lname} (Instructor) ∪ Π {Fn, Ln} (Student)
SQL Statement
SELECT Fname, Lname
FROM Instructor
UNION
SELECT Fn, Ln
FROM Student;

∪ =

12
SET INTERSECTION Operation
● The result of INTERSECTION operation, denoted by R ∩ S, is a
relation that includes all tuples that are in both R and S.
RA expression R = Π {Fnane,Lname} (Instructor) ∩ Π {Fn, Ln} (Student)
SQL Statement
SELECT Fname, Lname
FROM Instructor
INTERSECT
SELECT Fn, Ln
FROM Student;

∩ =

13
SET DIFFERENCE Operation
● The result of SET DIFFERENCE (aka MINUS, EXCEPT ), denoted by
R – S, is a relation that includes all tuples that are in R but not in S.
RA expression R = Π {Fnane,Lname} (Instructor) - Π {Fn, Ln} (Student)
SQL Statement
SELECT Fname, Lname
FROM Instructor
EXCEPT
SELECT Fn, Ln
FROM Student;

∩ =

14
SET UNION, INTERSECT, DIFFERENCE Operations
● Type Compatibility
● Two relations R(A1, A2, ... , An) and S(B1, B2 , ... , Bn) are said to be
type compatible if they have the same degree n and if domain(Ai) =
domain(Bi) for 1 ≤ i ≤ n
The two relations have the same number of attributes and each
corresponding pair of attributes has the same domain (Ai-Bi).
● Commutativity
● Both UNION and INTERSECTION are commutative operations; that is,
R∪S=S∪R
R∩S=S∩R
● The MINUS operation is not commutative; that is, in general,
R−S≠S−R
● Associativity
● UNION and INTERSECTION can be treated as n-ary operations
applicable to any number of relations because both are also associative
operations; that is,
R ∪ (S ∪ T ) = (R ∪ S) ∪ T
15
(R ∩ S) ∩ T = R ∩ (S ∩ T )

You might also like