0% found this document useful (0 votes)
11 views26 pages

Ch5 Relational Algebra 2023

Relational algebra is a foundational set of operations for the relational model, enabling users to specify queries that yield new relations from existing ones. It includes unary operations like SELECT, PROJECT, and RENAME, as well as binary operations derived from set theory such as UNION, INTERSECTION, and DIFFERENCE. The document also discusses how to construct relational algebra expressions and provides examples using a COMPANY database.

Uploaded by

veeravijai
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)
11 views26 pages

Ch5 Relational Algebra 2023

Relational algebra is a foundational set of operations for the relational model, enabling users to specify queries that yield new relations from existing ones. It includes unary operations like SELECT, PROJECT, and RENAME, as well as binary operations derived from set theory such as UNION, INTERSECTION, and DIFFERENCE. The document also discusses how to construct relational algebra expressions and provides examples using a COMPANY database.

Uploaded by

veeravijai
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/ 26

CST403 Principles of

Database Systems
Relational Algebra
Overview
• Relational algebra is 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
• A sequence of relational algebra operations forms a relational
algebra expression
• The result of a relational algebra expression is also a relation that represents
the result of a database query (or retrieval request)
• 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 (or MINUS, – )
• 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)
All examples discussed below refer to the
COMPANY database shown here
This is the database
state
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
• Keeps only those tuples that satisfy the qualifying condition
• Tuples satisfying the condition are selected whereas 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)
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
• The list of specified columns (attributes) is kept in each tuple
• The other attributes in each tuple are discarded
• Example: To list each employee’s first and last name and salary, the
following is used:
LNAME, FNAME,SALARY(EMPLOYEE)
Examples of applying SELECT and PROJECT
operations
Example-2:
Select all the student of Team A
Example-2:
Select all the students of department ECE whose fees is greater
than equal to 10000 and belongs to Team other than A.
Relational Algebra Expressions
• We may want to apply several relational algebra operations one after
the other
• Either we can write the operations as a single relational algebra expression
by nesting the operations, or
• We can 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
Single expression versus sequence of
relational operations: Example
• To retrieve the first name, last name, and salary of all employees who
work in department number 5, we must apply a select and a project
operation
• We can write a single relational algebra expression as follows:
• FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))
• OR We can explicitly show the sequence of operations, giving a name
to each intermediate relation:
• DEP5_EMPS   DNO=5(EMPLOYEE)
• RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)
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
• Useful when a query requires multiple operations
• Necessary in some cases (see JOIN operation later)
• If we write:
• RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)
• RESULT will have the same attribute names as DEP5_EMPS (same
attributes as EMPLOYEE)
• If we write:
• RESULT (F, M, L, S, B, A, SX, SAL, SU, DNO)(DEP5_EMPS)
• The 10 attributes of DEP5_EMPS are renamed to F, M, L, S, B, A,
SX, SAL, SU, DNO, respectively in RESULT relation (renamed from
DEP5_EMPS)
• Note: the  symbol is an assignment operator
TEMP   DNO=5 (EMPLOYEE)
R(First_name, Last_name, Salary)   Fname, Lname, Salary (TEMP)
Relational Algebra Operations from
Set Theory: UNION
• Binary operation, denoted by 
• 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 operand 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 be type
compatible (have same or compatible domains)
Example:To retrieve the social security numbers of all employees who either
work in department 5 (RESULT1 below) or directly supervise an employee who
works in department 5 (RESULT2)
• We can use the UNION operation as follows:
DEP5_EMPS  DNO=5 (EMPLOYEE)
RESULT1   SSN(DEP5_EMPS)
RESULT2(SSN)  SUPERSSN(DEP5_EMPS)
RESULT  RESULT1  RESULT2
• The union operation produces the tuples that are in either RESULT1 or RESULT2 or both
Relational Algebra Operations from Set
Theory: INTERSECTION
• INTERSECTION is denoted by 
• The result of the operation R  S, is a relation that includes all tuples
that are in both R and S
• The attribute names in the result will be the same as the attribute names in R
• The two operand relations R and S must be “type compatible”
Relational Algebra Operations from Set
Theory: SET DIFFERENCE
• SET DIFFERENCE (also called MINUS or EXCEPT) is denoted by –
• The result of R – S, is a relation that includes all tuples that are in R
but not in S
• The attribute names in the result will be the same as the attribute names in R
• The two operand relations R and S must be “type compatible”
Example to
illustrate the result
of UNION,
INTERSECT, and
DIFFERENCE
Relational Algebra Operations from Set
Theory: CARTESIAN PRODUCT
• CARTESIAN (or CROSS) PRODUCT Operation
• This operation is used to combine tuples from two relations in a
combinatorial fashion.
• Denoted by R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm)
• The resulting relation state has one tuple for each combination of tuples—
one from R and one from S.
• The two operands do NOT have to be "type compatible”
• Generally, CROSS PRODUCT is not a meaningful operation
• Can become meaningful when followed by other operations
Quiz 1:

a) c)

b) d)
Quiz 2:
The ___________ operation, denoted by −, allows us to
find tuples that are in one relation but are not in another.

a)
c)

b) d)
Extra reading
• https://www.guru99.com/relational-algebra-dbms.html

https://www.tutorialspoint.com/dbms/pdf/relational_algebra.pdf

You might also like