11-14 - Relational Algebra MDFD
11-14 - Relational Algebra MDFD
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
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
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
R
– In time this word was abbreviated as x
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
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)
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 relation resulting from the SELECT operation has the same attributes
of 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
23
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Unary Relational Operations: PROJECT (Contd…)
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)
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
• 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
35
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example of Applying Multiple Operations and RENAME
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
• 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)
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
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
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
RS=SR
RS=SR
50
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Database Schema for COMPANY
• This operation produces a new element by combining every member (tuple) from
one relation (set) with every member (tuple) from the other relation (set)
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)
• The resulting relation Q has one tuple for each combination of tuples -
one from R and one from S
• 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.
• 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)
58
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example of Applying CARTESIAN PRODUCT [Contd..
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)
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
• 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
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
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
77
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
EQUIJOIN Operation Contd…
• Consider the operation
DEPT_MGR ← DEPARTMENT Mgr_ssn=Ssn EMPLOYEE
• 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
• 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
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)
•
The DIVISION operation is denoted by ÷
• The DIVISION operation is applied to two relations R(Z) ÷ S(X) , where X ⊆ Z
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}
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:
88
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Recap of Relational Algebra Operations
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)
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
• 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
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;
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
• 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)
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
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)
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
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
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