0% found this document useful (0 votes)
142 views110 pages

11-14 - Relational Algebra MDFD

DBMS Ruas Slide

Uploaded by

Pritam Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views110 pages

11-14 - Relational Algebra MDFD

DBMS Ruas Slide

Uploaded by

Pritam Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Relational Algebra

CSC311A - Database Systems


B. Tech. 2017

Course Leader:
Ami Rai E., Gp Capt N Rath VSM
amirai.cs.et@msruas.ac.in, rathvsm.cs.et@msruas.ac.in

1
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
OBJECTIVES

• At the end of this lecture, student will be able to


explain what
 is Relational algebra
 are Different Types Of Operations In Relational
Algebra

2
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
TOPICS

• Relational Algebra
• Unary Relational Operations
• Relational Algebra Operations From Set Theory
• Binary Relational Operations
• Additional Relational Operations
• Examples of Queries in Relational Algebra

3
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Languages for Relational Model

• Two formal languages for the relational model


1. The Relational Algebra
2. The Relational Calculus

• In contrast, practical language for the relational


model
– The SQL standard
4
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Languages for Relational Model (Contd…

• Historically, the Relational Agebra and


Calculus were developed before the SQL
language

• SQL is based on the concepts from both the


R Algebra and the Calculus
5
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Relational Algebra
• Relational algebra
The basic set of operations for the Relational Model

• These operations enable a user to specify basic


retrieval requests (or queries)

• The result of an operation is a new Relation, which


may have been formed from one or more input
relations 6
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Relational Algebra (Contd…

• Relational Algebra Expression:


A sequence of Relational Algebraic operations
Result of a Relational Algebra Expression is a
Relation
Result of a Database Query (or Retrieval Request) is
also a Relation
Relational Algebraic Expression
Data Base Query
7
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Relational Algebra (Contd…
• The relational algebra is very important for several
reasons
1. It provides a formal foundation for relational model
operations
2. It is used as a basis for implementing and optimizing
queries in the query processing and optimization
modules
3. Some of its concepts are incorporated into the SQL
standard query language for RDBMSs
8
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Relational Algebra (Contd…

The core operations and functions in


the internal modules of most
Relational Systems are based on
Relational Algebra Operations

9
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Brief History of Origins of Algebra

It.
• Muhammad ibn Musa al-Khwarizmi (800-847 CE) wrote a book titled al-jabr about
arithmetic of variables
– Book was translated into Latin

e ad
– Its title (al-jabr) gave Algebra its name

• Al-Khwarizmi called variables “shay”


– “Shay” is Arabic for “thing”
– Spanish transliterated “shay” as “xay” (“x” was “sh” in Spain)

R
– In time this word was abbreviated as x

• Where does the word Algorithm come from?


Pl
– Algorithm originates from “al-Khwarizmi"
– Reference: PBS (http://www.pbs.org/empires/islam/innoalgebra.html)
10
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Pl Write down the Slide
Operations in Relational Algebra
• Relational Algebra consists of several groups of operations
1. Unary Relational Operations
a. SELECT (symbol:  (sigma))
b. PROJECT (symbol:  (pi))
c. RENAME (symbol:  (rho))
2. Relational Algebra Operations From Set Theory
a. UNION (  ), INTERSECTION (  ), DIFFERENCE (or MINUS, – )
b. CARTESIAN PRODUCT ( x )
3. Binary Relational Operations
a. JOIN - EQUIJOIN and NATURAL JOIN
b. DIVISION
4. Additional Relational Operations
a. Generalized PROJECTION
b. AGGREGATE FUNCTIONS and GROUPING
c. Recursive Closure
d. OUTER JOINS, OUTER UNION
11
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Database Schema for COMPANY n
s i o
s s
e e
is
h aba s
t
n at
d i d
s s e Y
cu A N
d is M P
l es CO
m p h e
xa o t
l l e r t
A refe 12
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Populated Database State for COMPANY

13
Faculty
©M. S. of
Ramaiah
Engineering
University
& Technology
of Applied Sciences © Ramaiah University of Applied Sciences
SELECT
14
Faculty
©M. S. of
Ramaiah
Engineering
University
& Technology
of Applied Sciences © Ramaiah University of Applied Sciences
Unary Relational Operations: SELECT
• The SELECT operation (denoted by  (sigma)) is used to select a subset of the
tuples from a relation based on a selection condition
• The selection condition acts as a filter
• It can also be visualized as a horizontal partition of the relation into two sets of
tuples
1. Tuples satisfying the condition are selected
2. The other tuples are discarded (filtered out)
• Examples
– Select the EMPLOYEE tuples whose department number is 4
 DNO = 4 (EMPLOYEE)
– Select the employee tuples whose salary is greater than $30,000
–  SALARY > 30,000 (EMPLOYEE)
15
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The General Form of The SELECT Operation
 In general, the select operation is denoted by

 <selection condition>(R)
where
• the symbol  (sigma) is used to denote the select operator
• the selection condition is a Boolean expression specified on the attributes of
relation R
• tuples that make the condition true are selected
– appear in the result of the operation
• tuples that make the condition false are filtered out
– discarded from the result of the operation
16
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The General Form of The SELECT Operation [Contd…
• The Boolean expression specified in <selection condition> is made up of
a number of clauses of the form
<attribute name> <comparison op> <constant value> e.g. Dno = 5;
Salary > 30000
or
<attribute name> <comparison op> <attribute name> e.g. ssn = Super_ssn

where
– <attribute name> is the name of an attribute of R
– <comparison op> is normally one of the operators {=, <, ≤, >, ≥, ≠}, and
– <constant value> is a constant value from the attribute domain
17
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The General Form of The SELECT Operation [Contd…
• Clauses can be connected by the standard Boolean operators and, or, and not to
form a general selection condition
• Example : Select the tuples for all employees who either work in department 4 and
make over $25,000 per year, or work in department 5 and make over $30,000

Pl Write the R Algebra


Statement Now
σ( Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000)(EMPLOYEE)

18
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The General Form of The SELECT Operation [Contd…
• Clauses can be connected by the standard Boolean operators and, or, and not to
form a general selection condition
• Example : Select the tuples for all employees who either work in department 4 and
make over $25,000 per year, or work in department 5 and make over $30,000
σ( (Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000))(EMPLOYEE)

• Results of applying SELECT Operation

19
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
SELECT Operation - Properties
• The SELECT operator is unary
– it is applied to a single relation

• The selection operation is applied to each tuple individually


– selection conditions cannot involve more than one tuple

– The relation resulting from the SELECT operation has the same attributes
of R

– The number of tuples in the result of a SELECT is


• less than (or equal to) the number of tuples in the input relation R 20
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
SELECT Operation – Properties [Contd…
– SELECT  is commutative
 <condition1>( < condition2> (R)) =  <condition2> ( < condition1> (R))

– Because of commutativity property, a cascade of SELECT


operations may be applied in any order
<cond1>(<cond2> (<cond3> (R)) = <cond2> (<cond3> (<cond1> ( R)))

21
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Relational Algebra Vs. SQL
• In SQL, the SELECT condition is typically specified in the WHERE clause of
a query

• For example, the following operation:


σDno=4 AND Salary>25000 (EMPLOYEE)

• would correspond to the following SQL query


SELECT *
FROM EMPLOYEE
WHERE Dno=4 AND Salary>25000;
22
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Unary Relational Operations: PROJECT

• PROJECT Operation is denoted by  (pi)


• This operation keeps certain columns (attributes) from a
relation and discards the other columns
• PROJECT creates a vertical partitioning of the relation into
two relations
1. the needed columns (attributes) and contains the result of the
operation
2. the other contains the discarded columns

23
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Unary Relational Operations: PROJECT (Contd…)

• The general form of the project operation


<attribute list>(R)
–  (pi) is the symbol used to represent the project
operation
– <attribute list> is the desired list of attributes from
relation R

24
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
PROJECT Operation - Example

• Example
– List each employee’s first name, last name and salary
From Company
LNAME, FNAME,SALARY(EMPLOYEE)

25
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Populated Database State for COMPANY

26
Faculty
©M. S. of
Ramaiah
Engineering
University
& Technology
of Applied Sciences © Ramaiah University of Applied Sciences
PROJECT Operation – Example (Contd…)
• Example
– List each employee’s first name, last name and salary
From Company’s Table
LNAME, FNAME,SALARY(EMPLOYEE)

27
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
PROJECT Operation – Duplicate Elimination
• The project operation removes any duplicate tuples
 If the attribute list includes only nonkey attributes of R, duplicate tuples are
likely to occur
 The result of the project operation must be a set of distinct tuples
 Mathematical sets do not allow duplicate elements
25K should have appeared thrice
(twice for F and once for M). But it appears once for F.
• Consider the following PROJECT operation
π Sex, Salary (EMPLOYEE)

Hence Duplicate Elimination


28
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
PROJECT Operation - Properties

s
om NO T i
– The number of tuples in the result of projection <list>(R) is

ve
• always less than or equal to the number of tuples in R

E C
• If the list of attributes includes a key of R, then the number of tuples

ati
T
OJ
in the result of PROJECT is equal to the number of tuples in R

ut
PR

Faculty of Engineering & Technology m © Ramaiah University of Applied Sciences


29
Relational Algebra (PROJECT) Vs. SQL
• In SQL, the PROJECT attribute list is specified in the SELECT clause of a query

• Example
– The following operation
π Sex, Salary (EMPLOYEE)
– would correspond to the following SQL query
SELECT DISTINCT Sex, Salary
FROM EMPLOYEE

• If we remove the keyword DISTINCT from this SQL query, then duplicates will not
be eliminated
• This option is not available in the formal Relational Algebra
30
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Sequence of Relational Operations
• For most queries, we may need to apply several relational algebra
operations one after the other

• Two ways
1. Write the operations as a single relational algebra expression by
nesting the operations, or
2. Apply one operation at a time and create intermediate result
relations

• In the latter case, we must give names to the relations that hold the
intermediate results 31
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Single Expression Vs Sequence of Operations

• Consider the Query


Retrieve the first name, last name, and salary of all employees who
work in Department number 5

Write the RA Expression


1. Single relational algebra expression:
Now
FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))

2. Sequence of operations, giving a name to each intermediate relation


DEP5_EMPS   DNO=5(EMPLOYEE)
RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)
32
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Single Expression Vs Sequence of Operations

• Consider the Query


Retrieve the first name, last name, and salary of all employees who
work in Department number 5
Re latio n s
ed i ate
Interm
1. Single relational algebra expression:
FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))

2. Sequence of operations, giving a name to each intermediate relation


DEP5_EMPS   DNO=5(EMPLOYEE)
RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)
33
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
RENAME
34
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Rename the Attributes in A Relation
– If we write
RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)
• RESULT will have the same attribute names as DEP5_EMPS (same attributes as
EMPLOYEE)

• For convenience, we also use a shorthand for renaming attributes in


an intermediate relation:
• Simply list the new attribute names in parentheses
TEMP ← σ Dno=5 (EMPLOYEE)
R(First_name, Last_name, Salary) ← π Fname, Lname, Salary (TEMP)

35
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example of Applying Multiple Operations and RENAME

RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)

TEMP ← σ Dno=5 (EMPLOYEE)

R(First_name, Last_name, Salary) ← π Fname, Lname, Salary (TEMP)

36
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Unary Relational Operations: RENAME
• The RENAME operator is denoted by  (rho)
• In some cases, we may want to rename the attributes of a relation or the relation
name or both

• The general RENAME operation  can be expressed by any of the following forms
1. S(B1, B2, …, Bn )(R) changes both
• the relation name to S, and
• the column (attribute) names to B1, B2, …..Bn
2. S(R) changes
• the relation name to S
3. (B1, B2, …, Bn )(R) changes
• the column (attribute) names to B1, B2, …..Bn
37
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Relational Algebra (RENAME) Vs. SQL
• In SQL, a single query typically represents a complex relational algebra
expression

• Renaming in SQL is accomplished by aliasing using AS

• Example
SELECT E.Fname AS First_name, E.Lname AS Last_name, E.Salary AS Salary
FROM EMPLOYEE AS E
WHERE E.Dno=5

38
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Relational Algebra Operations
From
Set Theory

39
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Relational Algebra Operations from Set Theory
• Includes the operations which are the standard mathematical operations
on sets

• Set Theory operations used to merge the elements of two sets in various
ways
1. UNION
2. INTERSECTION
3. SET DIFFERENCE (also called MINUS or EXCEPT)

• These are binary operations


– Each is applied to two sets (of tuples) 40
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Union Compatibility Or Type Compatibility
• Union compatibility or type compatibility
 When these operations are adapted to relational databases, the two relations on
which any of these three operations are applied must have the same type of
tuples
meaning
• Two relations R(A1, A2, ..., An) and S(B1, B2, ..., Bn) are said to be union
compatible (or type compatible) if

they have the same degree n and if dom(Ai) = dom(Bi) for 1≤ i≤ n


the two relations have the same number of attributes and each
corresponding pair of attributes has the same domain 41
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
UNION Operation
• UNION is Denoted by 

• The result of this 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

• The two operand relations R and S must be ‘’type compatible” (or


UNION compatible)
42
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The Result of a UNION Operation - Example
• Consider two relations STUDENT and • The new relation after applying UNION
INSTRUCTOR which are type compatible operation
• Their tuples represent the names of students
and the names of instructors, respectively STUDENT ∪ INSTRUCTOR

tes
li ca
up
o D
N
43
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
UNION Operation – Example
• Consider the query
To retrieve the Social Security numbers of all employees who either work in
department 5 or directly supervise an employee who works in department 5

Pl Write Now
DEP5_EMPS  DNO=5 (EMPLOYEE)
RESULT1   Ssn(DEP5_EMPS)
RESULT2(SSN)  Super_SSN(DEP5_EMPS)
RESULT  RESULT1  RESULT2

44
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
INTERSECTION Operation

• INTERSECTION is denoted by 

• The result of this operation, denoted by R ∩ S, is a relation


that includes all tuples that are in both R and S

• The two operand relations R and S must be “type compatible”

45
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The Result of a INTERSECTION Operation - Example
• Consider two relations STUDENT and • The new relation after applying
INSTRUCTOR which are type compatible INTERSECTION operation
• Their tuples represent the names of students
and the names of instructors, respectively STUDENT ∩ INSTRUCTOR

Pl Write Now

46
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
SET DIFFERENCE Operation

• SET DIFFERENCE (also called MINUS or EXCEPT) is denoted by


• The result of this operation, denoted by R – S, is a relation


that includes all tuples that are in R but not in S

• The two operand relations R and S must be “type compatible”

47
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The Result of a MINUS Operation - Example
• Consider two relations STUDENT and • The new relation after applying MINUS
INSTRUCTOR which are type compatible operation
• Their tuples represent the names of students STUDENT − INSTRUCTOR
and the names of instructors, respectively

Pl Write
INSTRUCTOR − STUDENT

Pl Write
48
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Properties of UNION, INTERSECT, and DIFFERENCE
• Both union and intersection are commutative operations
RS=SR
RS=SR

• Both union and intersection are associative operations


 R  (S  T) = (R  S)  T
 (R  S)  T = R  (S  T)

• The minus operation is not commutative


R–S≠S–R

• INTERSECTION can be expressed in terms of union and set difference


 R ∩ S = ((R ∪ S ) − (R − S )) − (S − R)
49
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Relational Algebra Operations from Set Theory
CARTESIAN PRODUCT

50
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Database Schema for COMPANY

You have it.


51
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Relational Algebra Operations from Set Theory:
CARTESIAN PRODUCT
• CARTESIAN PRODUCT Operation is also known as CROSS PRODUCT or CROSS JOIN
• It is denoted by ×
• The relations on which it is applied do not have to be union compatible

• This operation produces a new element by combining every member (tuple) from
one relation (set) with every member (tuple) from the other relation (set)

• The n-ary CARTESIAN PRODUCT operation


– produces new tuples by concatenating all possible combinations of tuples from n underlying
relations
52
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
CARTESIAN PRODUCT - Example
5 Tuples

4 Tuples

20 (5X4) Tuples

53
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
CARTESIAN PRODUCT Operation
• Denoted by R (A1, A2, . . ., An) x S (B1, B2, . . ., Bm)

• Result is a relation Q with degree n + m attributes


Q (A1, A2, . . ., An, B1, B2, . . ., Bm), in that order

• The resulting relation Q has one tuple for each combination of tuples -
one from R and one from S

• If R has nR tuples (denoted as |R| = nR ), and S has nS tuples,


then R x S will have
nR * nS tuples
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
54
CARTESIAN PRODUCT Operation contd.

• Generally, CROSS PRODUCT is not a meaningful operation


• It is mostly useful when followed by a selection that matches values of attributes
coming from the component relations

• Example
Retrieve a list of names of each female employees and their dependents

55
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example of Applying CARTESIAN PRODUCT

Pl write the
RA Expression
for
“Retrieve a list of names of each female employees
and their dependents”

56
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
CARTESIAN PRODUCT Operation contd.

• Generally, CROSS PRODUCT is not a meaningful operation


• It is mostly useful when followed by a selection that matches values of attributes
coming from the component relations

• Example
Retrieve a list of names of each female employees and their dependents

FEMALE_EMPS   SEX=’F’(EMPLOYEE)
EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)
EMP_DEPENDENTS  EMPNAMES x DEPENDENT

57
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example of Applying CARTESIAN PRODUCT
FEMALE_EMPS   SEX=’F’(EMPLOYEE)

EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)

58
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example of Applying CARTESIAN PRODUCT [Contd..

EMP_DEPENDENTS  EMPNAMES x DEPENDENT

7X3=21 Tuples

59
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example of Applying CARTESIAN PRODUCT [Contd..
a ch
f e ts .
e s o e n
Query: Retrieve a list of names of each female employees and
d
their dependents

a m p e n
o f n d e
i st e i r
a l th
v e an d EMP_DEPENDENTS  EMPNAMES x DEPENDENT

t r i e e s
: Re oye
e r y p l 7X3=21 Tuples
u
Q al e e m
fe m
60
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example of Applying CARTESIAN PRODUCT [Contd..

n ?
a
their dependents
tio
Query: Retrieve a list of names of each female employees and

Re l
pu t
o u t
e ct EMP_DEPENDENTS  EMPNAMES x DEPENDENT

o rr
e c
i t t h 7X3=21 Tuples
Is
61
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example of Applying CARTESIAN PRODUCT [Contd..

o.
N
Query: Retrieve a list of names of each female employees and
their dependents

o.
O. N EMP_DEPENDENTS  EMPNAMES x DEPENDENT

7X3=21
N
Tuples

62
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
CARTESIAN PRODUCT Operation contd.
• To keep only combinations where the DEPENDENT is related to the EMPLOYEE, add a
SELECT operation as follows

• Example (meaningful)
FEMALE_EMPS   SEX=’F’(EMPLOYEE)
EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)
EMP_DEPENDENTS  EMPNAMES x DEPENDENT
ACTUAL_DEPS   SSN=ESSN(EMP_DEPENDENTS)
RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)

• RESULT will now contain the name of female employees and their dependents

63
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example of Applying CARTESIAN PRODUCT [Contd..

ACTUAL_DEPS   SSN=ESSN(EMP_DEPENDENTS)

RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)

EMP_DEPENDENTS  EMPNAMES x DEPENDENT

64
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example of Applying CARTESIAN PRODUCT [Contd..
i n g
d o
i l e c t
w h u
ACTUAL_DEPS   SSN=ESSN(EMP_DEPENDENTS)

d
u l P r o
ref n
Ca s i a
e te RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)

B Car
EMP_DEPENDENTS  EMPNAMES x DEPENDENT

65
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
The Relational Algebra Symbols

66
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Binary Relational Operations: JOIN

• The JOIN operation is denoted by


• It is used to combine related tuples from two relations into
single “longer” tuples
• This operation is very important for any relational database
with more than a single relation
– because it allows us combine related tuples from various relations

• The join condition is specified on attributes from the two


relations R and S and is evaluated for each combination of
tuples 67
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
JOIN Operation
• The general form of a join operation on two relations
R (A1, A2, . . ., An) and S (B1, B2, . . ., Bm) is
R <join condition> S

• 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

• If R has nR tuples, and S has nS tuples, then the join result will generally have
less than nR * nS tuples 68
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
JOIN Operation - Example

69
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
JOIN Operation - Example

70
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Applying JOIN Operation
• Consider the query
– To retrieve the name of the manager of each department

• To get the manager’s name, we need to Combine each Department tuple with the
Employee tuple whose Ssn value matches the Mgr_ssn value in the department
tuple
• We do this by using the JOIN operation and then projecting the result over the
necessary attributes
DEPT_MGR ← DEPARTMENT Mgr_ssn=Ssn EMPLOYEE

RESULT ← π Dname, Lname, Fname (DEPT_MGR)


• Mgr_ssn is a foreign key of the DEPARTMENT relation that references Ssn, the primary key of the
EMPLOYEE relation
71
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Applying JOIN Operation contd.
• Mgr_ssn=Ssn is the join condition
– Combines each department record with the employee who manages the
department

– The new relation DEPT_MGR


DEPT_MGR ← DEPARTMENT Mgr_ssn=Ssn EMPLOYEE

72
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
CARTESIAN PRODUCT And JOIN Operation
• In JOIN,
– only combinations of tuples satisfying the join condition appear in
the result

• In the CARTESIAN PRODUCT,


– all combinations of tuples are included in the result
– The sequence of CARTESIAN PRODUCT followed by SELECT is used
quite commonly to identify and select related tuples from two
relations

73
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
CARTESIAN PRODUCT And JOIN Operation contd.
• Consider the CARTESIAN PRODECT operation
EMP_DEPENDENTS ← EMPNAMES × DEPENDENT
ACTUAL_DEPENDENTS ← σSsn=Essn(EMP_DEPENDENTS)

• These two operations can be replaced with a single JOIN operation as follows:
ACTUAL_DEPENDENTS ← EMPNAMES Ssn=Essn DEPENDENT

74
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
THETA JOIN Operation
• 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
 θ (theta) is one of the comparison operators {=, <, ≤, >, ≥, ≠}

• THETA JOIN
– A JOIN operation with such a General Join Condition

• Tuples whose join attributes are NULL or for which the join condition is FALSE do
not appear in the result
76
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
EQUIJOIN Operation
• EQUIJOIN
– The JOIN involves join conditions with equality comparisons only
– Only comparison operator used is

• In the result of an EQUIJOIN there is always one or more pairs of


attributes that have identical values in every tuple

77
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
EQUIJOIN Operation Contd…
• Consider the operation
DEPT_MGR ← DEPARTMENT Mgr_ssn=Ssn EMPLOYEE

• The new relation is

• The values of the attributes Mgr_ssn and Ssn are identical in every tuple of
DEPT_MGR (the EQUIJOIN result)
– because the equality join condition specified on these two attributes requires the values
to be identical in every tuple in the result 78
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
NATURAL JOIN Operation
• NATURAL JOIN
– Denoted by *
– Created to get rid of the second (superfluous) attribute in an EQUIJOIN
condition

• The standard definition of NATURAL JOIN requires that the 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

80
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
NATURAL JOIN Operation - Example
• Consider the query
to apply a natural join on the Dnumber attributes of DEPARTMENT and DEPT_LOCATIONS
• We can write as DEPT_LOCS ← DEPARTMENT * DEPT_LOCATIONS
• The resulting relation is shown in the Figure

• In general, the join condition for NATURAL JOIN is constructed by equating each
pair of join attributes that have the same name in the two relations and combining
these conditions with AND.
81
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
NATURAL JOIN Operation – Example Contd…
• Consider the query
Combine each PROJECT tuple with the DEPARTMENT tuple that controls the project

• First we rename the Dnumber attribute of DEPARTMENT to Dnum - so that it has


the same name as the Dnum attribute in PROJECT - and then we apply NATURAL
JOIN
PROJ_DEPT ← PROJECT * ρ( Dname, Dnum, Mgr_ssn, Mgr_start_date )(DEPARTMENT)

• The same query can be done in two steps by creating an intermediate table DEPT
as
DEPT ← ρ( Dname, Dnum, Mgr_ssn, Mgr_start_date )(DEPARTMENT)
PROJ_DEPT ← PROJECT * DEPT
82
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
NATURAL JOIN Operation – Example contd.
• The attribute Dnum is called the join attribute for the NATURAL JOIN operation,
– because it is the only attribute with the same name in both relations

• The resulting relation is illustrated in the Figure

83
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Complete Set of Relational Operations
• A complete set
– The set of relational algebra operations include
• SELECT 
• PROJECT 
• UNION 
• DIFFERENCE -
• RENAME , and
• CARTESIAN PRODUCT X
– Any other relational algebra operations can be expressed as a sequence of operations
from this set
• Example
– R  S = (R  S ) – ((R - S)  (S - R))
– R <join condition>S =  <join condition> (R X S)

– A NATURAL JOIN can be specified as a CARTESIAN PRODUCT preceded by RENAME and


followed by SELECT and PROJECT operations 84
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Binary Relational Operations: DIVISION


The DIVISION operation is denoted by ÷
• The DIVISION operation is applied to two relations R(Z) ÷ S(X) , where X ⊆ Z

• The result of DIVISION is a relation T(Y) that includes a tuple t


if tuples tR appear in R with tR [Y] = t, and with tR [X] = tS for every tuple tS in S

meaning
• for a tuple t to appear in the result T of the DIVISION, the values in t must appear
in R in combination with every tuple in S
85
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
DIVISION Operation - Example
• Consider X = {A}, Y = {B}, and Z = {A, B}

• The tuples (values) b1 and b4 appear in R in


combination with all three tuples in S
– that is why they appear in the resulting
relation T

• All other values of B in R do not appear with all


the tuples in S and are not selected
– b2 does not appear with a2, and b3 does not appear
with a1

86
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
DIVISION Operation - Example
• Consider the query
– Retrieve the names of employees who work on all the projects that ‘John Smith’ works
on

p l
• To express this query using the DIVISION operation, proceed as follows
1. Retrieve the list of project numbers that ‘John Smith’ works on in the intermediate relation

ti
SMITH_PNOS:
SMITH ← σ Fname=‘John’ AND Lname=‘Smith’ (EMPLOYEE)

d
SMITH_PNOS ← π Pno (WORKS_ON Essn=Ssn SMITH)

e a
2. Create a relation that includes a tuple <Pno, Essn> whenever the employee whose Ssn is
Essn works on the project whose number is Pno in the intermediate relation SSN_PNOS:

Faculty of Engineering & Technology R


SSN_PNOS ← π Essn, Pno (WORKS_ON)

© Ramaiah University of Applied Sciences


87
DIVISION Operation – Example contd.
3. Apply the DIVISION operation to the two relations, which gives the desired employees’
Social Security numbers:
SSNS(Ssn) ← SSN_PNOS ÷ SMITH_PNOS
RESULT ← π Fname, Lname (SSNS * EMPLOYEE) 1
2

88
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Recap of Relational Algebra Operations

Guys must go through

89
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Additional Relational Operations – Generalized
Projection

Similar to Column
Manipulations in Excel

• T 90
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Generalized Projection - Example
• Consider the 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

• Then a generalized projection combined with renaming may be used as follows:


REPORT ← ρ (Ssn, Net_salary, Bonus, Tax) (π Ssn, Salary – Deduction, 2000 *Years_service, 0.25 * Salary(EMPLOYEE))

91
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Additional Relational Operations - Aggregate
Functions
• A type of request that cannot be expressed in the basic relational algebra is to
specify mathematical aggregate functions on collections of values from the
database
• Examples of such functions
– retrieving the average or total salary of all employees or the total number of employee
tuples

• Common functions applied to collections of numeric values include


– SUM, AVERAGE, MAXIMUM, MINIMUM and COUNT

• The result of applying an aggregate function is a relation, not a scalar number—


even if it has a single value
92
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Aggregate Function Operation
• The AGGREGATE FUNCTION operation is denoted, using the symbol ℑ (pronounced
script F), to specify these types of requests as follows:

<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 such pair, <function> is one of the allowed functions - such as SUM, AVERAGE,
MAXIMUM, MINIMUM, and COUNT
 <attribute> is an attribute of the relation specified by R

• The resulting relation has the grouping attributes plus one attribute for each
element in the function list 93
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Additional Relational Operations - Grouping
• Grouping the tuples in a relation by the value of some of their attributes and then
applying an aggregate function independently to each group

• Example
– Group EMPLOYEE tuples by Dno, so that each group includes the tuples for employees
working in the same department
– We can then list each Dno value along with, say, the average salary of employees
within the department, or the number of employees who work in the department

94
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Using Grouping with Aggregation
• Grouping can be combined with Aggregate Functions
– Example: For each department, retrieve the DNO, COUNT SSN, and AVERAGE SALARY

• A variation of aggregate operation ℱ allows this


– Grouping attribute placed to left of symbol
– Aggregate functions to right of symbol SELECT DNO, COUNT(*), AVG ( SA L A RY )
FROM EMPLOYEE
GROUP BY DN O;
• Example
DNO ℱCOUNT SSN, AVERAGE Salary (EMPLOYEE)
– Above operation groups employees by DNO (department number) and computes the
count of employees and average salary per department

95
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Examples of Aggregate Functions and Grouping

S E L E C T D N O , C O U N T ( * ) , AVG ( S A L A R Y )
FROM EMPLOYEE
G ROUP BY DNO;
96
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Examples of Aggregate Functions And Grouping
contd.
SELECT DNO, COUNT(SSN) AS NO_OF_EMPLOYEES,
AVG (SALARY) AS AVERAGE SAL
FROM EMPLOYEE GROUP BY DNO;

SELECT DNO, COUNT(SSN), AVG (SALARY) AS


AVERAGE SALARY
FROM EMPLOYEE GROUP BY DNO;
SELECT COUNT(SSN), AVG (SALARY) AS
AVERAGE SALARY FROM EMPLOYEE ;
97
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Additional Relational Operations – Recursive
Closure
• Recursive Closure Operations
– An operation that cannot be specified in the basic original relational algebra
– This operation is applied to a recursive relationship between tuples of the same type

e.g., the relationship between an Employee and a Supervisor

• This relationship is described by the foreign key Super_ssn of the EMPLOYEE


relation, and it relates each employee tuple (in the role of supervisee) to
another employee tuple (in the role of supervisor)

98
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Recursive Closure - Example
• An example of a recursive operation
– to retrieve all SUPERVISEES of an EMPLOYEE e at all levels

– that is, all EMPLOYEE e’ directly supervised by e


– all employees e’’ directly supervised by each employee e’
– all employees e’’’ directly supervised by each employee e’’
– and so on

• Although it is possible to retrieve employees at each level and then take their
UNION, we cannot, in general, specify a query such as “retrieve the supervisees of
‘James Borg’ at all levels” without utilizing a looping mechanism unless we know
the maximum number of levels
99
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Recursive Closure – Example Contd…
• To specify the Ssns of all employees e directly supervised—at level one—by
the employee e whose name is ‘James Borg’, we can apply the following
operation:
BORG_SSN ← π Ssn (σ Fname=‘James’ AND Lname=‘Borg’ (EMPLOYEE))
SUPERVISION(Ssn1, Ssn2) ← π Ssn,Super_ssn (EMPLOYEE)
RESULT1(Ssn) ← π Ssn1 (SUPERVISION Ssn2=Ssn BORG_SSN)

• To retrieve all employees supervised by Borg at level 2, we can apply another


JOIN to the result of the first query, as follows:
RESULT2(Ssn) ← π Ssn1 (SUPERVISION Ssn2=Ssn RESULT1)

• To get both sets of employees supervised at levels 1 and 2 by ‘James Borg’, we


can apply the UNION operation to the two results, as follows: 100
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
A Two-level Recursive Query - Example
BORG_SSN ← π Ssn (σ Fname=‘James’ AND Lname=‘Borg’ (EMPLOYEE))

SUPERVISION(Ssn1, Ssn2) ← π Ssn,Super_ssn (EMPLOYEE)

RESULT1(Ssn) ← π Ssn1 (SUPERVISION Ssn2=Ssn


BORG_SSN)

RESULT2(Ssn) ← π Ssn1 (SUPERVISION Ssn2=Ssn RESULT1)

101
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
102
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Additional Relational Operations – OUTER JOIN
• In NATURAL JOIN and EQUIJOIN,

n ?
– tuples without a matching (or related) tuple are eliminated from the join result

ol
– Tuples with null in the join attributes are also eliminated

S
– This amounts to loss of information
CASE I
• A set of operations, called OUTER joins, can be used when
• we want to keep all the tuples in R, or
• all those in S, or
CASE II
• all those in both relations in the result of the join,
• regardless of whether or not they have matching tuples in the other relation

CASE III 103


Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Types of OUTER JOIN
• LEFT OUTER JOIN
– keeps every tuple in the first, or left, relation R in R S; if no matching tuple is found
in S, then the attributes of S in the join result are filled or padded with NULL values

• RIGHT OUTER JOIN


– keeps every tuple in the second, or right, relation S in the result of R S

• FULL OUTER JOIN


– keeps all tuples in both the left and the right relations when no matching tuples are
found, padding them with NULL values as needed

104
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Result of LEFT OUTER JOIN Operation
• Consider the query
Find a list of all employee names as well as the name of the departments they manage if
they happen to manage a department; if they do not manage one, we can indicate it with
a NULL value
• We can apply an operation LEFT OUTER JOIN, denoted by , to retrieve the result as
follows:
TEMP ← (EMPLOYEE Ssn=Mgr_ssn DEPARTMENT)
RESULT ← π Fname, Minit, Lname, Dname (TEMP)

SELECT E.FNAME, E.MINIT, E.LNAME, D.DNAME


FROM EMPLOYEE AS E, DEPARTMENT AS D
WHERE E.SSN=SUPER_SSN AND E.DNO=D.DNUMBER

105
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Additional Relational Operations - OUTER UNION
• The outer union operation
– developed to take the union of tuples from two relations if the relations are not type
compatible

• This operation will take the 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 type compatible

• The attributes that are type compatible are represented only once in the result,
and those attributes that are not type compatible from either relation are also
kept in the result relation T(X, Y, Z)

106
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
OUTER UNION Operation - Example
• An outer union can be applied to two relations whose schemas are
STUDENT(Name, SSN, Department, Advisor) and
INSTRUCTOR(Name, SSN, Department, Rank)

– Tuples from the two relations are matched based on having the same combination of
values of the shared attributes— Name, SSN, Department
– If a student is also an instructor, both Advisor and Rank will have a value; otherwise, one
of these two attributes will be null

– The result relation STUDENT_OR_INSTRUCTOR will have the following attributes:


STUDENT_OR_INSTRUCTOR (Name, SSN, Department, Advisor, Rank)

107
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Examples of Queries in Relational Algebra
nt
‘Research’ department.
e e
• Query 1: Retrieve the name and address of all employees who work for the

RESEARCH_DEPT  
ir t tem
DNAME=’Research’ (DEPARTMENT)

l w
RESEARCH_EMPS  (RESEARCH_DEPT
t a EMPLOYEE)
DNUMBER= DNO

P T S ow
RESULT   FNAME, LNAME, ADDRESS (RESEARCH_EMPS)

C N
• As a single in-line expression, this query becomes:
E
L i t
π Fname, Lname, Address (σ Dname=‘Research’ (DEPARTMENT Dnumber=Dno (EMPLOYEE))
E r
e S o
t h F
108
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Examples of Queries in Relational Algebra
• Query 1: Retrieve the name and address of all employees who work for the
‘Research’ department.
RESEARCH_DEPT   DNAME=’Research’ (DEPARTMENT)
RESEARCH_EMPS  (RESEARCH_DEPT EMPLOYEE)
DNUMBER= DNO

RESULT   FNAME, LNAME, ADDRESS (RESEARCH_EMPS)

• As a single in-line expression, this query becomes:


π Fname, Lname, Address (σ Dname=‘Research’ (DEPARTMENT Dnumber=Dno (EMPLOYEE))

SELECT E.FNAME,E.LNAME,E.ADDRESS
FROM EMPLOYEE AS E, DEPARTMENT AS D
WHERE E.DNO = D.DNUMBER AND D.DNAME = ‘RESEARCH’;
109
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Examples of Queries in Relational Algebra contd…
• Query 2: For every project located in ‘Stafford’, list the project number, the
controlling department number, and the department manager’s last name, address,
and birth date.
nt
STAFFORD_PROJS ← σ Plocation=‘Stafford’ (PROJECT)
te e
CONTR_DEPTS ← (STAFFORD_PROJS
ir temDnum=Dnumber DEPARTMENT)
PROJ_DEPT_MGRS ← (CONTR_DEPTS
l w ta Mgr_ssn=Ssn EMPLOYEE)

P TS
RESULT ← π Pnumber, Dnum, Lname, Address, Bdate (PROJ_DEPT_MGRS)

EC
EL
e S
th 110
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Summary
• The relational algebra is used to specify a sequence of operations to specify a query

f.
el
• The unary relational operators are SELECT, PROJECT, and RENAME operations

rs
• The binary set theoretic operations requires that relations on which they are applied

u
be union (or type) compatible; these include UNION, INTERSECTION, and SET

Yo
DIFFERENCE
• The CARTESIAN PRODUCT operation is a set operation that can be used to combine

it
tuples from two relations, producing all possible combinations

ad
• CARTESIAN PRODUCT followed by SELECT can be used to define matching tuples
from two relations and leads to the JOIN operation
Re
• Different JOIN operations are THETA JOIN, EQUIJOIN, and NATURAL JOIN
Pl

111
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Summary contd.
• The generalized projection operation extends the projection operation by allowing
functions of attributes to be included in the projection list
• AGGREGATE FUNCTION operation with aggregate types of statistical requests that
summarize the information in the tables
• Recursive queries can be handled in a step-by-step approach
• The OUTER JOIN and OUTER UNION operations, extend JOIN and UNION and allow
all information in source relations to be preserved in the result

112
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences

You might also like