You are on page 1of 62

Relational

Algebra
Outline (1)

 Introduction

 Unary Relational Operation


 Select Operation
 Project Operation
 Sequence of Operation and Rename Operation

 Relational Algebra Operations from Set Theory


 Union Operation
 Intersection Operation
 Minus Operation
 Cartesian Product
Outline (2)
 Binary Relational Operations
 Join Operation
 Variation of Join: Equijoin Operation
 Variation of Join: Natural Join Operation
 A Complete Set of Relational Algebra
 Division Operation

 Query Tree

 Additional Relational Operations


 Generalized Projection
 Aggregate Functions and Grouping
 Recursive Closure Operations
 Outer Join Operation
 Outer Union Operation
Introduction (1)

 A data model must include a set of operations to


manipulate the database (aside from defining database
structure and constraints)

 Relational algebra is the basic set of operations for the


relational model.
 These operations specify basic retrieval requests.
 The result is a new relation

 A sequence of relational algebra operations forms a


relational algebra expression.
 The result is also a relation that represents the result of
database query (or retrieval request)
Introduction (2)

 The importance of relational algebra:


 Provides a formal foundation for relational model
operations.
 Used as a basis for implementing and optimizing queries
in RDBMS.
 Some of its concepts are incorporated into the SQL
standar query language for RDBMSs

 So, relational algebra oftens considered to be an integral


part of the relational data model.
Introduction (3)

 Relational algebra‟s operations can be divide into two


groups:
 Group one includes set operations from mathematical
set theory (UNION, INTERSECTION, SET,
DIFFERENCE, and CARTESIAN PRODUCT)
 Group two consists of operations developed specifically
for relational databases (SELECT, PROJECT, and
JOIN)

 Additional operations are added into the original relational


algebra because there are some common database
requests that cannot be performed with the original one.
(e.g., aggregate functions: operations that summarize
data from tables)

 Those operations are added into the original relational


algebra due to their importance to many database
applications.
Unary Relational Operation
[SELECT Operation] (1)

 It is called unary relation operation because the


operation operates on single relations

 SELECT operation selects a subset of the tuples from a


relation that satisfies a certain selection condition.

 It filters the tuples and keeps only tuples that satisfy the
selection condition.

 SELECT operation is a horizontal partition.

column1 column2 … columnN


row1
row2

rowN
Unary Relational Operation
[SELECT Operation] (2)

 The relational algebra notation for SELECT operation:

σ <selection condition> (R)


 The σ (sigma) symbol is to denote the SELECT operation.
 The <selection condition> is a Boolean expression
specified on the attributes of relation R.
 The Boolean expression inside the <selection condition>
is made up of a number of clauses of the form:
<attribute name><comparison op><constant value>
or
<attribute name><comparison op><attribute name>
Unary Relational Operation
[SELECT Operation] (3)

 The <attribute name> is the name of an attribute of R.

 The <comparison op> usually one of these operators:

{=,<,≤,>,≥,≠}
 The <constant value> is a constant value from the attribute
domain.

 Those clauses can be connected using Boolean operators,


such as AND, OR, and NOT.

 Interpretation of Boolean operators:


 (cond1 AND cond2) is TRUE if both (cond1) and (cond1)
are TRUE; otherwise, it is FALSE.
 (cond1 OR cond2) is TRUE if either (cond1) or (cond2) or
both are TRUE; otherwise, it is FALSE.
 (NOT cond) is TRUE if cond is FALSE; otherwise, it is
FALSE.
Unary Relational Operation
[SELECT Operation] (4)

 Examples of Relational Algebra for SELECT Operation:

σDno=4(EMPLOYEE) ……..(ex.01)
 In ex.01, we want to select (σ) from a EMPLOYEE relation
with a selection condition of “Dno=4”.

 Inside the selection condition:


 The attribute name is “Dno” ;
 The comparison op is “=“ and;
 The constant value is “4”.

 Ex.01 equals to SQL Statement of:

SELECT * FROM employee WHERE Dno=4;


Unary Relational Operation
[SELECT Operation] (5)

 Examples of Relational Algebra for SELECT Operation:

σ(Dno=4 AND Salary>25000) OR (Dno=5 AND


Salary>30000)(EMPLOYEE) ……..(ex.02)

 In ex.02, we want to select (σ) from a EMPLOYEE relation


with a selection condition of “(Dno=4 AND Salary>25000)
OR (Dno=5 AND Salary>30000)”.

 In ex.02, we connected the clauses inside the selection


condition with these Boolean operators: AND and OR.

 Ex.02 equals to SQL Statement of:

SELECT * FROM employee WHERE (Dno=4 AND


Salary>25000) OR (Dno=5 AND Salary>30000);
Unary Relational Operation
[SELECT Operation] (5)

 Inside the selection condition, consists of two clauses:

 The first clause: (Dno=4 AND Salary>25000)


 The attribute name are “Dno” and “Salary” ;
 The comparison op is “=“ and “>” ;
 The constant value is “4” and “25000”.

 The second clause: (Dno=5 AND Salary>30000)


 The attribute name are “Dno” and “Salary” ;
 The comparison op is “=” and “>” ;
 The constant value is “5” and “30000”.
Unary Relational Operation
[SELECT Operation] (6)

 Keep in mind that comparison operators in the set


{=,<,≤,>,≥,≠} only apply to domains of ordered values,
such as numeric or date domains.

 For domains of unordered values, the comparison


operators that can be apply is the set of {=,≠}. Example of
unordered values domain is the domain of Color = {„red‟,
„blue‟, „green‟, „white‟, „yellow‟, …}.

 Some domains allow additional types of comparison


operators, such as in domain of character string may allow
the comparison operator SUBSTRING_OF.
Unary Relational Operation
[SELECT Operation] (7)

 The degree of the relation resulting from a SELECT


Operation (its number of attributes) is the same as the
degree of R because SELECT Operation is a horizontal
partition.

 The number of tuples in the resulting relation is always


less than or equal to the number of tuples in R.

|σc (R)| ≤ |R|


 The nature of SELECT Operation is commutative:

σ<cond1>(σ<cond2>(R)) = σ<cond2>(σ<cond1>(R))
Unary Relational Operation
[SELECT Operation] (8)

 Based on that nature, a sequence of SELECTs can be


applied in any order and any cascade of SELECT
operations can then be combine into a single SELECT
operations with conjunctive (AND) condition.

σ<cond1>(σ<cond2>(…(σ<condn>(R))…)) = σ<cond1> AND σ<cond2>


AND … ANDσ<cond2>(R)
Unary Relational Operation
[PROJECT Operation] (1)

 PROJECT Operation selects certain columns from the


table and discards other columns, unlike the SELECT
Operation that selects some rows and discard other rows.

 PROJECT Operation aims to select certain attributes of a


relation.

 PROJECT Operations is a vertical partition

column1 column2 … columnN


row1
row2

rowN
Unary Relational Operation
[PROJECT Operation] (2)

 The relational algebra notation of PROJECT Operation:

π<attribute list>(R)
 The pi (π) symbol is to denote the PROJECT Operation.

 The <attribute list> is the desired list of attributes from


attributes of relation R.

 The result of PROJECT Operations has only the attributes


specified in the <attribute list> and represented in the
same order as they appear in the list.
Unary Relational Operation
[PROJECT Operation] (3)

 Example of Relational Algebra for PROJECT Operation:

Π (EMPLOYEE)…(ex.03)
Lname, Fname, Salary

 In ex.03, we want to project (select) Lname, Fname, Salary


attributes from relation R.

 Inside the <attribute list> consists of Lname, Fname,


Salary. These are the attributes that we want to project
(select)

 Ex.03 equals to SQL Statement of:

SELECT DISTINCT(Lname, Fname, Salary) FROM


employee;
Unary Relational Operation
[PROJECT Operation] (4)

 The degree of PROJECT Operations is equal to the


number of attributes in <attribute list>.

 The PROJECT Operation removes any duplicate


tuples, which is known as duplicate elimination.

 Duplicate elimination involves sorting to detect


duplicates.

 If duplicates are not eliminated, the result would be a


multiset or bag of tuples rather than a set. Multiset or
bag of tuples are not permitted in formal relation model
but permitted in practice.
Unary Relational Operation
[PROJECT Operation] (5)

 The number of tuples resulting from a PROJECT


Operation is always less than or equal to the number of
tuples in R.

 If the projection list the superkey of relation R then the


resulting relation has the same number of tuples as R.

 The nature of PROJECT Operation:

π<list1> (π<list2>(R)) = π<list1>(R)

 That nature holds as long as <list2> contains attributes in


<list1>, otherwise the left-hand side is an incorrect
expression.

 PROJECT Operation is not commutative.


Unary Relational Operation
[Sequence of Operations and the RENAME
Operation] (1)

 In general, we may want to apply several relational algebra


operations one after the other.
 These can be achieved in a single relational algebra
expression by nesting the opeations.
πFname, Lname, Salary(σDno=5(EMPLOYEE))
or
 Apply one operation at a time and create intermediate
result relations.
DEP5_EMPS  σDno=5(EMPLOYEE)
RESULT  πFname, Lname, Salary(DEP5_EMPS)
 For the latter example, we must assign a name for each
operation.
Unary Relational Operation
[Sequence of Operations and the RENAME
Operation] (2)
 We could also rename the attributes in the intermediate and
result relations
TEMP  σDno=5(EMPLOYEE)
R(First_name, Last_name, Salary)  πFname, Lname, Salary(TEMP)

 The RENAME Operation is useful in connecting with more


complex operation, such as UNION and JOIN.

 Formal form of RENAME Operation has three forms:

ρS(B1, B2, …, Bn)(R) …(ro.1)


OR

ρS(R) …(ro.2)
OR

ρ(B1, B2, …, Bn)(R) …(ro.3)


Unary Relational Operation
[Sequence of Operations and the RENAME
Operation] (3)

 The ρ (rho) symbol is used to denote RENAME


Operation.

 S is the new relation.

 B1, B2, …, Bn are the new attribute names.

 Expression ro.1 renames both the relation and its


attributes.

 Expression ro.2 renames the relation only.

 Expression ro.3 renames the attributes only.


Relational Algebra Operations from Set
Theory
[UNION, INTERSECTION, MINUS Operations] (1)

 UNION, INTERSECTION, and MINUS are standard


mathematical operations on sets.

 Those are binary operations that is applied to two sets.

 In relational databases, two relations that used one of


those operations must have the same type of tuples
(known as union compatibility).

 Relations R(A1,A2, …,An) and S(B1, B2, …,Bn) are union


compatible if they have the same degree n and if dom(Ai)
= dom(Bi) for 1 ≤ i ≤ n (same number of attributes and each
pair has the same domain).
Relational Algebra Operations from Set
Theory
[UNION, INTERSECTION, MINUS Operations] (2)

 Definition of UNION Operation on two union-compatible


relations R and S:
 Notation: R S
 The result includes all tuples that are either in R or in S
or in both R and S. (Duplicates are eliminated).

 Definition of INTERSECTION Operation on two union-



compatible relations R and S:
 Notation: R S
 The result includes all tuples that are in both R and S.
 Defintion of MINUS Operation on two union-compatible
relations R and S:

 Notation: R − S
 The result includes all tuples that are in R but not in S.
Relational Algebra Operations from Set
Theory
[UNION, INTERSECTION, MINUS Operations] (3)

 The nature of both UNION and INTERSECTION are


commutative operations:

R S  S  R and R S  S  R
 The nature of MINUS Operation is not commutative:

RS  SR
  INTERSECTION can
be expressed in the form of UNION
and MINUS:

R
 S  R  S  R  S   S  R
Relational Algebra Operations from Set
Theory
[The CARTESIAN PRODUCT] (1)

 Also known as CROSS PRODUCT or CROSS JOIN.

 Denoted by ×

 It‟s also a binary operation, but the relations involved do not


have tobe union compatible.

 The result of this operation is a combination of every element


member (tuple) from one relation (set) with every member
(tuple) from the other relation (set)

 Example of cross join:

R(A1,A2,…,An) x S(B1,B2,…,Bm)

will give result

A relation Q with degree n+m

Q(A1,A1,…,An,B1,B2,…,Bm)
Relational Algebra Operations from Set
Theory
[The CARTESIAN PRODUCT] (2)

 The resulting relation Q has one tuple for each


combination of tuples (one from R and one from S).

 If R has nR tuples (|R| = nR) and S has nS tuples (|S| = nS)


then RxS will have nR * nS tuples.

 N-ary CARTESIAN PRODUCT operation – an extension


of the previous concept.

 N-ary CARTESIAN PRODUCT will produce new tuples by


concatenating all possible combinations of tuples from n
underlying relations.
Relational Algebra Operations from Set
Theory
[The CARTESIAN PRODUCT] (3)

 In short, CARTESIAN PRODUCT creates tuples with the


combined attributes of two relations.

 We can SELECT related tuples only from two relations by


specifying an appropriate selection condition.

 Sequence of CARTESIAN PRODUCT followed by SELECT is


quite common to identifed and select related tuples from two
relations. Thus, a special operation called JOIN was created to
specify previous sequence as a single operation.
Binary Relational Operations
[JOIN] (1)
 JOIN operation denoted by ><

 JOIN operation is used to combine related tuples from two
relations into single tuples. In database JOIN operation is
important because it allows us to process relationship among
relations. 
 Example of JOIN operation, retrieve name of manager of each
departement:

><
DEPT_MGR  DEPARTEMENTMgr_ssn=SsnEMPLOYEE
RESULT  πDname, Lname, Fname(DEPT_MGR)


Binary Relational Operations
[JOIN] (2)
 The general form of JOIN operation on two relations
R(A1,A2,…,An) and S(B1,B2,…,Bm) is

><
R<join condition>S
 The result 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
satisfies the join condition.
combination

 The difference between CARTESIAN PRODUCT and JOIN is


that JOIN only combines tuples that satisfy the join condition
unlike CARTESIAN PRODUCT that combines all tuples.
Binary Relational Operations
[JOIN] (3)
 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 operator {=,<,≤,>,≥,≠}.

This general join condition is called THETA JOIN. Tuples with join
attributes equal NULL do not appear in the result (does not
necessarily preserve all of the information in the participating
relations).
Binary Relational Operations
[VARIATION of JOIN: The EQUIJOIN] (1)
 A JOIN operation that only use an equal / {=} comparison
operator.

 In EQUIJOIN (and previous JOIN) operation, notice that there is


always one or more pairs of attributes that have identical
values in every tuple. Hence, this is because of the equality join
condition specified on these attributes.

 The appearance of these one or more identical pairs is


superfluous.
Binary Relational Operations
[VARIATION of JOIN: The NATURAL JOIN] (1)
 To get rid of the second (superfluous) attribute found in
EQUIJOIN operation, we introduced the next variation of JOIN
operation called NATURAL JOIN.

 Denoted by *.

 Standard definition: requires two join attributes (or each pair of


join attributes) have the same name in both relations. If this is
not the case, a renaming operation is applied first.

 Example of NATURAL JOIN, DEPARTEMENT table has


Dnumber attribute but in PROJECT table use the name Dnum.
So we need to rename the operation before applying the
NATURAL JOIN operation.
Binary Relational Operations
[VARIATION of JOIN: The NATURAL JOIN] (2)
 Example of NATURAL JOIN, DEPARTEMENT table has
Dnumber attribute but in PROJECT table use the name Dnum.
So we need to rename the operation before applying the
NATURAL JOIN operation.

PROJ_DEPT  PROJECT *
ρ(Dname,Dnum,Mgr_ssn,Mgr_start_date)(DEPARTEMENT)

or in two steps approach:

DEPT ρ(Dname,Dnum,Mgr_ssn,Mgr_start_date)(DEPARTEMENT)

PROJ_DEPT  PROJECT * DEPT

 The attribute Dnum is called join attribute where in the result


would only one join attribute is kept.
Binary Relational Operations
[VARIATION of JOIN: The NATURAL JOIN] (3)
 In general, NATURAL JOIN is performed by equating all
attribute pairs that have the same name in the two relations.

 There can be a list of join attributes from each relation, and each
corresponding pair must have the same name.

 A more general, but nonstandard definition for NATURAL


JOIN:

Q  R * (<list1>),(<list2>)S

<list1>, a list of i attributes from R, and <list2>, a list of i attributes


from S. The lists are used to form equality comparison
conditions between pairs of corresponding attributes and the
conditions are then ANDed together. Only the list corresponding
to attributes of the first relation R is kept in the result Q.
Binary Relational Operations
[VARIATION of JOIN: The NATURAL JOIN] (4)
 If no combination of tuples satisfies the join condition then the
result of JOIN operation is an empty relation with zero tuples.

 For R has nR tuples and S has nS tuples, the result of R <join


condition>S will have between zero and nR * nS tuples.
><

 Join selectivity, a property of each join condition, is a ratio of
the expected size of the join result divided by the maximum
size nR * nS tuples.

 
If no join condition and all combinations of tuples qualify then
the JOIN operation degenerates into a CARTESIAN
PRODUCT.

 These JOIN operation that we discussed so far is called INNER


JOINS, a JOIN operation that combine data from multiple
relations so that related information can be presented in a single
table.
Binary Relational Operations
[VARIATION of JOIN: The NATURAL JOIN] (5)
 NATURAL JOIN or EQUIJOIN operation can be applied among
multiple tables, leading to an n-way join.

 See this example of 3-way join:



PROJECT >< DEPARTEMENT  ><
Dnum  Dnumber EMPLOYEE 
Mgr _ ssn  Ssn


Binary Relational Operations
[A Complete Set of Relational Algebra
Operation] (1)
 These are the complete set of relational algebra operation:

{,  ,,,}

 Other relational algebra operations besides the original ones are


a sequence of operations from the original set.

  operation can be expressed by UNION and


INTERSECTION
MINUS operation, like so:

R  S  R  S  R  S S  R


 The JOIN operation that we previously discussed can be
expressed as a CARTESIAN PRODUCT followed by a SELECT
operation:

 ><  condition  S    condition  R  S


R
Binary Relational Operations
[The DIVISION Operation] (1)
 The DIVISION operation is useful for a special kind of query that
sometimes occurs in database application.

 Denoted by:

 Example: retrieve the names of employees who work on all the
projects that „John Smith‟ works on.
 First step  get list of project numbers that „John Smith‟
works on:
 Fname ' John' ANDLname ' Smith' EMPLOYEE 
SMITH

SMITH
 _ PNOS  PnoWORKS _ON >< SMITH
Essn  Ssn


Binary Relational Operations
[The DIVISION Operation] (2)
 Second step  create a relation that includes a tuple <Pno,
Essn> where Ssn = Essn and the project number is Pno:

SSN _ PNOS  Essn, PnoWORKS _ON


 Final step  apply DIVISION operation to the two relations,
which gives result the employee‟s Social Security Numbers:

SSNS(Ssn)  SSN _ PNOS  SMITH _ PNOS



RESULT  Fname, LnameSSNS  EMPLOYEE 




Binary Relational Operations
[The DIVISION Operation] (3)
 Generally, DIVISION is applied to two relations R(Z) ÷ S(X),
where X  Z

Let Y  Z  X(and hence Z  X Y), Y is the set of attributes of R


that are not attribute of S.

 The result of DIVISION is a relation T(Y) that includes a tuple t if
 tuples t appear in R with tR[Y] = t, and with tR[X] = tS for every
R
tuple tS in S.

 For a tuple t to appear in the result of DIVISION T, the values in


t must appear in R in combination with every tuple in S.

 The tuples in the denominator relation restrict the numerator


relation by selecting those tuples in the result that match all
values present in the denominator.
Binary Relational Operations
[The DIVISION Operation] (4)
 See picture below, X={A}, Y={B}, and Z={A,B}. Tuples (values)
b1 and b4 appear in R in combination with all three tuples in S,
thus they are the result of DIVISION.
Binary Relational Operations
[The DIVISION Operation] (5)
 The DIVISION operation can be express as a sequence of π,
×, and − as follows:

T1 Y (R)
T2  Y S  T1  R
T  T1 T2


Notation for Query Trees (1)
 Query Tree, or query evaluation tree, or query execution tree is
a notation that is used typically in relational systems to
represent queries internally.

 It includes the relational algebra operations being executed and


it is used as a possible data structure for the internal
representation of the query in an RDBMS.

 Thus query tree represents a tree data structure that


corresponds to a relational algebra expression. As leaf nodes of
the tree is the input relations and the relation algebra operations
is in the internal nodes.

 Example: “For every project located in „Stafford‟, retrieve the


project number, the controlling department number, and the
departement manager‟s last name, address, and birth date‟. The
relational algebra for this is:

Pnumber,Dnum,Lname, Address,Bdate(((Plocation ' Stafford'(PROJECT ))


Dnum  Dnumber(DEPARTEMENT )) >< Mgr _ ssn  Ssn(EMPLOYEE ))
><

Notation for Query Trees (2)
 The query tree for the previous relational algebra is:

P.Pnumber, P.Dnum, E.Lname, E.Address, E.Bdate


(3
><
 ) _ ssn  E.Ssn
D.Mgr

 (2
><
P.Dnum
)  D.Dnumber EMPLOYE
 E E
(1
 P.Plocation
) ' Stafford' DEPARTEMENT
 D

PROJEC
P T

Notation for Query Trees (3)
 In general, a query tree gives a good visual representation and
understanding of the query in terms of the relational operations
it uses.

 Query tree is recommended as an additional means for


expressing queries in relational algebra.
Additional Relational Operations

 Some common database request – specially in commercial


application for RDBMSs – cannot be performed with the original
relational algebra operations that we previously learned.

 There is a need for defining additional operations to express


these requests.

 These operations enhance the expressive power of the original


relational algebra.
Additional Relational Operations
[Generalized Projection] (1)
 Extends the original projection operation which allows function
of attributes to be included in the projection list.

 The generalized form:

F , F ,...,F (R)
1 2 n

where F1,F2,..,Fn are functions over the attributes in relation R and


may involve constants.

 Generalized Projection is useful when developing reports


where computer values have to be produced in columns.


Additional Relational Operations
[Generalized Projection] (2)
 Consider this example, we have a relation:

EMPLOYEE(Ssn, Salary, Deduction, Years_service)

 A report may be required to show:


 Net Salary = Salary – Deduction,
 Bonus = 2000 * Years_service, and
 Tax = 0.25 * Salary

 The Generalized Projection combined with renaming operation


will give result like so:

REPORT  (Ssn, Net _ salary, Bonus,Tax)


(Ssn, Salary Deduction, 2000* Years_ service, 0.25 * Salary(EMPLOYEE ))



Additional Relational Operations
[Aggregate Functions and Grouping] (1)
 Mathematical aggregate functions on collections of values
from the database cannot be specified by the original relational
algebra.

 Aggregate functions are used in simple statistical queries that


summarize information from the database tuples (SUM,
AVERAGE, MAXIMUM, and MINIMUM) and COUNT function
for counting tuples or values.

 Denoted by (pronounced script F) and the form in relational


alegbra is:

 grouping_ attributes  function_ list  (R)


where <grouping_attributes> is a list of attributes of the relation

specified in R, and <function_list> is a list of
(<function><attribute>) pairs. In each pair could be one of these
functions: SUM, AVERAGE, MAXIMUM, MINIMUM, COUNT.


Additional Relational Operations
[Aggregate Functions and Grouping] (2)
 The resulting relation has the grouping attributes plus one
attribute for each element in the function list.

 Example: retrieve each departement number, number of


employees in the departement, and their average salary:
R(Dno, No _ of _ employees, Average _ sal)(DnoCOUNTSsn, AVERAGESalary(EMPLOYEE ))


Additional Relational Operations
[Recursive Closure Operations] (1)
 Recursive Closure is another operation that can be specified
using the original relational algebra.

 Applied to a recursive relationship between tuples of the same


type, such as the relationship between an employee and a
supervisor (this relationship described by foreign key Super_ssn
of the EMPLOYEE relation).

 Example: specify the Ssns of all employee e‟ directly supervised


– at level one – by the employee e whose name is „James Borg‟:

BORG _ SSN  Ssn(Fname ' James' ANDLname ' Borg' (EMPLOYEE ))


SUPERVISION (Ssn1, Ssn2)  Ssn, Super_ ssn(EMPLOYEE )
RESULT1(SSN)
  Ssn1(SUPERVISION >< Ssn2  SsnBORG _ SSN)


Additional Relational Operations
[Recursive Closure Operations] (2)
 Example: retrieve all employees supervised by Borg at level 2
(all employee e‟‟ supervised by some employee e‟ who is directly
supervised by Borg). Apply another JOIN to the result of the first
query:

RESULT2(Ssn)
  Ssn1(SUPERVISION >< Ssn2  SsnRESULT1)
 Example: to get both sets of employees supervised at levels 1
and 2 by „James Borg‟  apply UNION operation:

RESULT  RESULT2 RESULT1


Additional Relational Operations
[Recursive Closure Operations] (3)
Additional Relational Operations
[OUTER JOIN Operations] (1)
 On previous JOIN operations:
 Tuples without a matching (or related) tuples are eliminated.
 Tuples with NULL values in the join attributes are also
eliminated.

 If the result of JOIN operation is supposed to be used to


generate a report based on all the information in the component
relations then the loss of information would give an inaccurate
report.

 The OUTER JOIN operation satisfies the need of queries in


which tuples from two tables are to be combined by matching
corresponding rows, but without losing any tuples for lack of
matching values.
Additional Relational Operations
[OUTER JOIN Operations] (2)
 Example: list all employees names and also the name of the
departements they manage (if they happen to manage a
departement, if not then it would be indicated as NULL). Apply
LEFT OUTER JOIN which is denoted by :  ><

TEMP  (EMPLOYEE >< Ssn  Mgr _ ssnDEPARTEMENT )
RESULT
  Fname, Minit, Lname, Dname(TEMP)

 LEFT OUTER JOIN keeps every tuple in the first or left
relation, if no matching tuple is found in the right side then
 the attributes on the right side of the join are filled or padded
with NULL values.
Additional Relational Operations
[OUTER JOIN Operations] (3)

 The RIGHT OUTER JOIN, denoted by ><
keeps every tuple
,
in the second or right side of relation.
 
 The FULL OUTER JOIN, denoted by   ><
 , keeps all tuple in

both the left and the right side relations when no matching
tuples are found, padding them with NULL values as needed.


Additional Relational Operations
[OUTER UNION Operations] (1)
 Developed to take the union of tuples from two relations if the
relations are not union compatible.

 Takes UNION of tuples in two relations R(X,Y) and S(X,Z) that


are partially compatible, meaning that only some of their
attributes, say X, are union compatible.

 The result relation T(X,Y,Z), the attributes that are union


compatible are represented only once in the result, and those
which are not union compatible from either relation are also kept
in the result.

 Two tuples t1 in R and t2 S are said match if t1[X] = t2[X], and are
considered to represent the same entity of relationship instance.
These will be combined into a single tuple in the result T. No
matching tuple in the other relation are padded with NULL.
Additional Relational Operations
[OUTER UNION Operations] (2)
 Example: an OUTER JOIN can be applied to two relations
whose schemas are:
 STUDENT(Name, Ssn, Departement, Advisor)
 INSTRUCTOR(Name, Ssn, Departement, Rank)

 The result of OUTER UNION-ed these relations and renaming it


into STUDENT_OR_INSTRUCTOR:

STUDENT_OR_INSTRUCTOR(Name, Ssn, Departement,


Advisor, Rank)
References

[Elmasri] Elmasri, Navathe. Fundamentals of Database


Systems, 5th edition