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

Elational Lgebra

The document discusses relational algebra operations. There are two main types of operations - retrieval and update. Relational algebra uses operations like selection, projection, union, difference, rename, cartesian product, join, and division to specify retrieval queries. An expression combining multiple relational algebra operations can be used to retrieve data from relations in a relational database.

Uploaded by

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

Elational Lgebra

The document discusses relational algebra operations. There are two main types of operations - retrieval and update. Relational algebra uses operations like selection, projection, union, difference, rename, cartesian product, join, and division to specify retrieval queries. An expression combining multiple relational algebra operations can be used to retrieve data from relations in a relational database.

Uploaded by

Abdul Haseeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

RELATIONAL ALGEBRA

RELATIONAL ALGEBRA
There are two types of operations in RDBMS
⚫ Retrieval
⚫ Update

The set of operations for specifying retrieval requests


(or queries) in relational model is called Relational
Algebra.

A sequence of relational algebra operations forms a


relational algebra expression.
COMPANY DATABASE
CONSIDERED IN EXAMPLES
SELECT OPERATION(UNARY OPERATION)

This operation selects a subset of tuples from a relation that


satisfy a selection condition.
Select is denoted by : σ <selection condition>(R)
EXAMPLES : SELECT OPERATION

Select the employees whose department number is 4:


σ DNO = 4 (EMPLOYEE)
Select all the projects in department 5

Select the employees whose salary is greater than $35,000


Slide 5- 6
5.6
SELECT OPERATION
Selection condition is a Boolean expression specified on the attributes of
relation R
⚫ It can include boolean operators AND, OR, NOT applied on relational operators <, >
<=,>=, !=, =

Select σ is commutative:
σ <condition1>(σ < condition2> (R)) = σ <condition2> (σ < condition1> (R))
Cascade of Select operations
σ<cond1>(σ< cond2> (σ<cond3>(R)) = σ <cond1> AND < cond2> AND < cond3>(R)))
PROJECT OPERATION (UNARY OPERATION)

This operation selects a subset of columns from the existing relation.


Project operation is denoted by π<attribute list>R
It removes duplicate tuples, the result of project is set of tuples
Example:
RESULT← π LNAME, FNAME, SALARY (EMPLOYEE)
DN← π DNAME, DNUMBER (DEPARTMENT)
PROJECT OPERATION

Project operation is not commutative


π <list1> (π <list2> (R) ) = π <list1> (R) as long as <list2> contains the
attributes in <list1>

No of Tuples in the result of projection π <list>(R)


⚫ less or equal to the number of tuples in R
⚫ If the list of attributes includes a key of R, then the no of is equal to the
no of tuples in R
RELATIONAL ALGEBRA EXPRESSIONS
We may want to apply several relational algebra
operations one after the other
1. We can write the operations as a single relational
algebra expression by nesting the operations, or
2. We can apply one operation at a time and create
intermediate result relations.
EXAMPLE: SEQUENCE OF OPERATIONS
To retrieve the first name, last name, and salary of all
employees who work in Department 5

Result of sequence of operations:


⚫ πFNAME, LNAME, SALARY(σ DNO=5(EMPLOYEE))
Using intermediate relation:
⚫ D5 ← σ DNO=5(EMPLOYEE)
⚫ RESULT ← π FNAME, LNAME, SALARY (D5)

Renaming of attributes
⚫ D5 ← σ DNO=5(EMPLOYEE)
⚫ R (FirstName,LastName,Salary) ← π FNAME, LNAME, SALARY (D5)
EXAMPLE OF APPLYING MULTIPLE OPERATIONS AND
RENAME

πFNAME, LNAME, SALARY(σ DNO=5(EMPLOYEE))

D5 ← σ DNO=5(EMPLOYEE)
R (First_name,Last_name,Salary) ← π Fname, Lname, Salary (D5)
RENAME OPEARATION
Rename operator is denoted by ρ (rho)
Rename operation ρ can be expressed as:
⚫ ρS(R) rename the relation to S
⚫ ρ(B1, B2, …, Bn )(R) rename the attributes to B1, B2, …..Bn
⚫ ρS (B1, B2, …, Bn )(R) rename both relation to S, and attributes to B1, B1,
…..Bn
Example:
⚫ ρ RESULT (First_Name,Last_Name, DNO)(D5)
UNION (BINARY OPERATION)
The result of 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 relations R and S must be “type compatible” (or Union
compatible)
R and S must have same number of attributes
Each pair of corresponding attributes must have same or compatible
domains
UNION EXAMPLE
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
DEP5_EMPS ← σDNO=5 (EMPLOYEE)
RESULT1 ← π SSN(DEP5_EMPS)
RESULT2(SSN) ← πSUPERSSN(DEP5_EMPS)
RESULT ← RESULT1 ∪ RESULT2
INTERSECTION AND SET DIFFERENCE (BINARY
OPERATIONS)

INTERSECTION operation: the result of R ∩ S, is a relation that


includes all tuples that are in both R and S

SET DIFFERENCE operation: the result of R – S, is a relation


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

Two relations R and S must be “type compatible”

16
RELATIONAL ALGEBRA OPERATIONS FROM SET
THEORY

Both ∪ and ∩ are commutative operations


⚫ R ∪ S = S ∪ R, and R ∩ S = S ∩ R

Both ∪ and ∩ can be treated as n-ary operations


⚫ R ∪ (S ∪ T) = (R ∪ S) ∪ T
⚫ (R ∩ S) ∩ T = R ∩ (S ∩ T)

Minus operation is not commutative


⚫ R–S≠S–R
EXAMPLE TO ILLUSTRATE THE RESULT OF UNION,
INTERSECT, AND DIFFERENCE
CARTESIAN PRODUCT
The result of Cartesian product of two relations R(A1, A2,
. . ., An) x S(B1, B2, . . ., Bm) is given as:
Result(A1, A2, . . ., An, B1, B2, . . ., Bm)

Let |R| = nR and |S| = nS , then |R x S|= nR * nS


R and S may NOT be "type compatible”

Cross Product is a meaningful operation only if it is


followed by other operations
Example (not meaningful):
F
F ← σ SEX=’F’(EMPLOYEE)
EN ← π FNAME, LNAME, SSN (F)
E_DP ← EN x DEPENDENT EN

Problem:
P
Retrieve a list of each female E_D
employee’s dependents

Example (meaningful):

A_DP ← σ SSN=ESSN(E_DP)
R ← π FNAME, LNAME, DEPENDENT_NAME(A_DP)

P
A_D
R
JOIN(BINARY OPERATION)

JOIN denoted by combine related tuples from various


relations

JOIN combines CARTESIAN PRODECT and SELECT into a


single operation

General form of a join operation on two relations R(A1, A2, . .


., An) and S(B1, B2, . . ., Bm) is:

R <join condition>
S

21
EXAMPLE OF JOIN OPERATION
Retrieve the name of the manager of each department.

DEPT_MGR ← DEPARTMENT MGRSSN=SSN


EMPLOYEE
The join condition can also be specified as
DEPARTMENT.MGRSSN= EMPLOYEE.SSN

22
COMPLETE SET OF RELATIONAL OPERATIONS
The set of operations including
⚫ SELECT σ,
⚫ PROJECT π ,
⚫ UNION ∪,
⚫ DIFFERENCE − ,
⚫ RENAME ρ, and
⚫ CARTESIAN PRODUCT X
is called a complete set because any relational
algebra expression can be expressed using these.

For example:
⚫ R ∩ S = (R ∪ S ) – ((R − S) ∪ (S − R))
⚫ R <join condition>
S = σ <join condition> (R X S)
23
SOME PROPERTIES OF JOIN
Consider the following JOIN operation:
⚫ R(A1, A2, . . ., An) S(B1, B2, . . ., Bm)
R.Ai=S.Bj
⚫ Result is a relation Q with degree n + m attributes:
Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.

⚫ Relation Q has one tuple for each combination of tuples—r


from R and s from S, but only if they satisfy the join
condition r[Ai]=s[Bj]

⚫ If R has nR tuples, and S has nS tuples, then no of tuples in


join result < nR * nS . 24
THETA-JOIN
The general case of JOIN operation is called a
Theta-join: R S
theta
Theta is a boolean expression on the attributes of R and
S; for example:
⚫ R.Ai<S.Bj AND (R.Ak=S.Bl OR R.Ap<S.Bq)

Theta can have any comparison operators {=,≠,<,≤,>,≥,}

25
EQUI-JOIN
EQUIJOIN is a join condition that involves only equality operator
=.

Example:
⚫ DEPT_MGR ← DEPARTMENT MGRSSN=SSN EMPLOYEE
⚫ Retrieve a list of each female employee’s dependents
F ← σ SEX=’F’(EMPLOYEE)
EN ← π FNAME, LNAME, SSN (F)
E_DP ← EN DEPENDENT
SSN=ESSN

26
ISSUE WITH EQUIJOIN OPERATION

Superfluous column

Result of EQUIJOIN always have one or more pairs of attributes


that have identical values in every tuple.
NATURAL JOIN OPERATION
NATURAL JOIN operation (denoted by *) is created to get rid
of the superfluous attribute in an EQUIJOIN condition.

The two join attributes, or each pair of corresponding join


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

28
NATURAL JOIN OPERATION
Example: To apply a natural join on the DNUMBER attributes of
DEPARTMENT and DEPT_LOCATIONS, it is sufficient to write:

⚫ DEPT_LOCS ← DEPARTMENT * DEPT_LOCATIONS


Only attribute with the same name is DNUMBER
An implicit join condition is created based on this attribute:
DEPARTMENT.DNUMBER=DEPT_LOCATIONS.DNUMBER

29
EXAMPLE: NATURAL JOIN

Another example: Q ← R(A,B,C,D) * S(C,D,E)


⚫ The implicit join condition includes each pair of attributes with the same
name, “AND” together:
R.C=S.C AND R.D=.S.D
⚫ Result keeps only one attribute of each such pair:
Q(A,B,C,D,E)

30
EXAMPLE OF NATURAL JOIN OPERATION

31
DIVISION (BINARY OPERATION)
⚫ The division operation is applied to two relations
R(Z) ÷ S(X), where X ⊂ Z.
⚫ Let Y = Z – X
We have Z = X ∪ Y and Y is a set of attributes of R that are not the
attributes of S.

⚫ The result of DIVISION is a relation T(Y)


⚫ 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.

32
EXAMPLE OF DIVISION
Retrieve all employees who work on all the project that
John Smith works on

⚫ Smith σ fname=‘John’ and lname=‘Smith’ (Employee)

⚫ Smith_Pnos π Pno (Works_on essn=ssn Smith)

⚫ Ssn_Pnos π Essn,Pno (Works_on)

⚫ SSNS(ssn) Ssn_Pnos ÷ Smith_Pnos

33
RECAP OF RELATIONAL ALGEBRA OPERATIONS

34
AGGREGATE FUNCTIONS
Now we specify mathematical aggregate functions on
collections of values from the database.

Examples:
⚫ Retrieve the average or total salary of all employees
⚫ Retrieve total number of employee tuples

Functions applied to collections of numeric values include


⚫ SUM, AVERAGE, MAXIMUM, and MINIMUM.
⚫ COUNT function is used for counting tuples or values.

35
AGGREGATE FUNCTION OPERATION
Use of the Aggregate Functional operation ℱ
⚫ ℱMAX Salary (EMPLOYEE)
⚫ ℱMIN Salary (EMPLOYEE)
⚫ ℱSUM Salary (EMPLOYEE)
⚫ ℱCOUNT SSN, AVERAGE Salary (EMPLOYEE)
computes no of employees and their average salary
Note: count just counts the number of rows, without removing
duplicates

36
USING GROUPING WITH AGGREGATION
Grouping can be combined with Aggregate Functions

Example:
⚫ For each department, retrieve the DNO, COUNT SSN, and
AVERAGE SALARY
⚫ DNO ℱCOUNT SSN, AVERAGE Salary (EMPLOYEE)

37
EXAMPLE: AGGREGATE FUNCTIONS AND GROUPING

38
EXAMPLES OF QUERIES IN RELATIONAL
ALGEBRA
■ Q1: Retrieve the name and address of all employees who work for the
‘Research’ department.
RESEARCH_DEPT ← σ DNAME=’Research’ (DEPARTMENT)
RESEARCH_EMPS ← (RESEARCH_DEPT DNUMBER= DNO
EMPLOYEE)
RESULT ← π FNAME, LNAME, ADDRESS (RESEARCH_EMPS)

39
EXAMPLES OF QUERIES IN RELATIONAL
ALGEBRA
■ Q6: Retrieve the names of employees who have no dependents.
ALL_EMPS ← π SSN(EMPLOYEE)
EMPS_WITH_DEPS(SSN) ← π ESSN(DEPENDENT)
EMPS_WITHOUT_DEPS ← (ALL_EMPS - EMPS_WITH_DEPS)
RESULT ← π LNAME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)

40
EXAMPLES OF QUERIES IN RELATIONAL
ALGEBRA
■ Q5: Retrieve the names of all employees with two or more dependents.
T1(Ssn, No_of_dependents) ← Essn ℱ COUNT Dependent_name (DEPENDENT)
T2 ← σ No_of_dependents >1(T1)
RESULT ← π LNAME, FNAME (T2 * EMPLOYEE)

41
ASSIGNMENT 2
Section B: Due date 13-Feb 2013

Section C: Due date 16-Feb 2013


OUTER JOIN OPERATION
In INNER JOIN, tuples without a matching are
eliminated from the join result
⚫ Tuples with null are also eliminated
⚫ This amounts to loss of information.

OUTER joins operations are used when we want to


keep
⚫ all the tuples in R in the join result , or
⚫ all tuples in S in the join result, or
⚫ all tuples in both relations R and S in the join result
43
LEFT OUTER JOIN
List the employees name and the department name that they manage.
If they don’t manage one, then indicate this with a null value.
Temp (Employee Ssn=Mgr_Ssn Department)
Result π Fname, Minit, Lname, Dname(Temp)

44
OUTER JOIN OPERATION
Left outer join: keeps every tuple in R, denoted as R
S
⚫ if no matching tuple is found in S, then the attributes of S in
the join result are filled with null values.

Right outer join: keeps every tuple in S in the result of


R S.

Full outer join: keeps all tuples in both the left and the
right relations. It is denoted by

45
FULL OUTER JOIN VS CARTESIAN PRODUCT

Employee Ssn=Mgr_Ssn Department

??
OUTER UNION OPERATIONS

The outer union operation take the union of tuples in two


relations R(X, Y) and S(X, Z) that are partially
compatible,

⚫ Only some of their attributes, say X, are type compatible.

⚫ The attributes that are type compatible are represented only


once in the result

⚫ The attributes that are not type compatible from either


relation are also kept in the result relation T(X, Y, Z).

47
OUTER JOIN EXAMPLE
An outer union can be applied to two relations STUDENT(Name, SSN,
Department, Advisor) and INSTRUCTOR(Name, SSN, Department,
Rank).

⚫ Tuples 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.
⚫ Result relation:
STUDENT_OR_INSTRUCTOR (Name, SSN, Department,
Advisor, Rank)

48
RECURSIVE CLOSURE OPERATION
This can’t be specified in general using Relational Algebra
Example: Retrieve all SUPERVISEES of an EMPLOYEE e at all levels —
that is,
⚫ all employees 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.
We can retrieve employees at each level and then take their union, however,
we cannot specify a query such as
⚫ “retrieve the supervisees of ‘James Borg’ at all levels” without utilizing a looping
mechanism.
The SQL3 standard includes syntax for recursive closure.
49
RECURSIVE CLOSURE OPERATION

50
Example of Query Tree
Query: 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.

51
QUERY TREE
An internal data structure to represent a query
Standard technique to estimate the work done in executing the
query, and the optimization of execution

Nodes stand for operations like selection, projection, join,


renaming, division, ….
Leaf nodes represent base relations

A tree gives a good visual feel of the complexity of the query and
the operations involved
Algebraic Query Optimization consists of rewriting the query or
modifying the query tree into an equivalent tree.
52
RELATIONAL ALGEBRA OPERATORS
Relational Algebra consists of several groups of operations
⚫ Unary Relational Operations
SELECT (symbol: σ (sigma))
PROJECT (symbol: π (pi))
RENAME (symbol: ρ (rho))
⚫ Relational Algebra Operations From Set Theory
UNION ( ∪ ), INTERSECTION ( ∩ ), DIFFERENCE (–)
CARTESIAN PRODUCT ( x )
⚫ Binary Relational Operations
JOIN (several variations of JOIN exist)
DIVISION
⚫ Additional Relational Operations
OUTER JOINS, OUTER UNION
AGGREGATE FUNCTIONS (These compute summary of
information: for example, SUM, COUNT, AVG, MIN, MAX)
CHAPTER SUMMARY
Relational Algebra
⚫ Unary Relational Operations
⚫ Relational Algebra Operations From Set Theory
⚫ Binary Relational Operations
⚫ Additional Relational Operations
⚫ Examples of Queries in Relational Algebra

54

You might also like