Relational Algebra Binary Ops
Relational Algebra Binary Ops
LOGO
Relational Algebra
and Relational Calculus
relation relation
SQL statement:
SELECT *
FROM instructor, teaches;
3
CROSS PRODUCT Operation
Instructor teaches
Cardinality(Instructor) = |Instructor| = 12
Cardinality(teaches) = |teaches| =15
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?
=
=
=
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)
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 )