0% found this document useful (0 votes)
17 views35 pages

Relational Algebra

Presents relational algebra for SQL.

Uploaded by

samneric778
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)
17 views35 pages

Relational Algebra

Presents relational algebra for SQL.

Uploaded by

samneric778
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/ 35

RELATIONAL

ALGEBRA &
BASIC SQL
LEARNING
OBJECTIVE

Work with the fundamental relational


algebra operators that are a foundation
for structured query language (SQL)
Demonstrate how to translate relational
algebra expressions to SQL
Presentation title 3

COMPONENTS OF THE RELATIONAL


MODEL (RECAP)

1. Data structure
• Tables (relations), rows, columns
2. Data integrity constraints
• Mechanisms for implementing business rules that maintain integrity of
manipulated data
3. Data manipulation
• Powerful SQL operations for retrieving and modifying data
• Relational algebra is the foundation for SQL operations
Presentation title 4

RELATIONAL ALGEBRA
Relational Algebra: A collection of algebraic operators that
• Are defined on relations;
• Produce relations as results; and
• Can be combined to form complex algebraic expressions.
• Can be grouped into:
• Unary operators: operate on one relation only (select, project, name)
• Operators from set theory: merge the elements of two sets in various
ways (union, cross product, difference)
• Derived operators – join operators (intersection, join, natural join)
Presentation title 5

RELATIONAL ALGEBRA
What is algebra, anyway? Expressions created by combining operands and
operators that satisfy some laws
Operands (values) Expressions
− Variables, constants − Combine operations with parenthesis (explicit)
− Closure property − OR using either precedence (implied)

Operators Laws
+ “Addition” − Identify semantically equivalent expressions
* “Multiplication” − Commutativity, associativity, etc.
- “Subtraction”
/: “Division”
Presentation title 6

RELATIONAL ALGEBRA: UNARY OPERATORS

Unary Relational Operators


• Operate on ONE relation only. They include:
• SELECT (symbol: σ): Removes rows that don’t meet criteria from a
relation (equivalent to SQL WHERE clause)
• PROJECT (symbol: π): Selects desired columns from a relation
(equivalent to SQL SELECT clause)
• RENAME (symbol: ρ): Modifies schema of a relation
Presentation title 7

RELATIONAL ALGEBRA: UNARY OPERATORS


The SELECT operator (σ)
• Returns subset of the tuples from a relation that satisfies a selection
condition:
𝜎 <selection condition>(𝑅) or scondition(table name)
• where <selection condition>
• may have Boolean conditions AND (^), OR (v), and NOT (¬)
• has clauses of the form: <attribute name> <comparison op> <constant value> or
<attribute name> <comparison op> <constant value>
• Applied independently to each individual tuple t in operand
• Tuple selected iff condition evaluates to TRUE
Presentation title 8

RELATIONAL ALGEBRA: UNARY OPERATORS

The SELECT operator (σ): Example


• Given the following employee relation, write relational algebra
expression that selects employees whose job is Professor
ID Name Gender Dept JobType sP(R) outputs tuples of R
1000 Anderson F CS Secretary which satisfy P (predicate)
1001 Chen M INFO Professor • same schema as R
1002 Furyk M INFO Technician • In this case, result is
1003 Miles F CS Professor going to be tuples with
ID 1001 and 1003
Relational algebra: sJobType=“Professor”(employee)
Presentation title 9

RELATIONAL ALGEBRA: UNARY OPERATORS

The SELECT operator (σ): Exercise


• Given the employee relation below, write relational algebra
expression that selects employees whose salary is above 30000.
ID last_name first_name age salary
1000 Anderson F 25 20000
1001 Chen M 40 30000
1002 Furyk M 45 45000
1003 Miles F 42 39000

Relational algebra:
Presentation title 10

RELATIONAL ALGEBRA: UNARY OPERATORS

The SELECT operator (σ)


• The SELECT operation is commutative, that is,
σ p1(σ p2(R)) = σ cp(σ cp(R))

• The degree (number of attributes) of resulting relation from a


Selection operation is same as the degree of the Relation given.
Presentation title 11

RELATIONAL ALGEBRA: UNARY OPERATORS

The SELECT operator (σ): Commutative Example


• Given the employee relation below, write relational algebra expression that
selects employees who are at least 35 years old and whose salary is above 30000.
ID last_name first_name age salary • The two expressions are
equivalent
1000 Anderson F 25 35000
• Both will return tuples with
1001 Chen M 40 30000 ID 1002 and 1003
1002 Furyk M 45 45000 • Note: for nested expressions,
the inner one is evaluated first
1003 Miles F 42 39000
Question: What is the
Relational algebra: ssalary>30000(sage>=35(employee)) intermediate result for
Relational algebra: sage>=35(ssalary>30000(employee)) each expression?
Presentation title 12

RELATIONAL ALGEBRA: UNARY OPERATORS

The SELECT operator (σ): Logical AND (^) Example


• Given the EMPLOYEE relation below, write relational algebra expression that
selects employees who are at least 35 years old and whose salary is above 30000.
ID last_name first_name age salary
1000 Anderson F 25 35000 • We can write the
1001 Chen M 40 30000 previous commutative
1002 Furyk M 45 45000 expressions using the
1003 Miles F 42 39000 AND (^)
Relational algebra: ssalary>30000^age>=35 (employee)
Relational algebra: sage>=35^salary>30000 (employee)
Presentation title 13

RELATIONAL ALGEBRA: UNARY OPERATORS

The PROJECT operator (p) – Equivalent to SQL SELECT


• Returns subset of selected attributes for all the tuples of a relation:
p <attribute list>(𝑅)
• Result of PROJECT operator is a set of distinct tuples
• Degree: Number of attributes in projected attribute list
• Note: Conditions can be written in selection operation but not in
projection operation
Presentation title 14

RELATIONAL ALGEBRA: UNARY OPERATORS

The PROJECT operator (p): Example


• Given the employee relation below, write relational algebra
expression that projects employee last_name and salary.
ID last_name first_name age salary last_name salary
1000 Anderson F 25 20000 Anderson 20000
1001 Chen M 40 30000 Result Chen 30000
1002 Furyk M 45 45000 Furyk 45000
1003 Miles F 42 39000 Miles 39000
Note: The Project Operation
Relational algebra: plast_name,salary(employee) is equivalent to SQL
SELECT statement
Presentation title 15

RELATIONAL ALGEBRA: UNARY OPERATORS

The PROJECT operator (p): Example – SQL Equivalent


• Given the employee relation below, write SQL query that returns
employee last_name and salary from it.
ID last_name first_name age salary last_name salary
1000 Anderson F 25 20000 Anderson 20000
1001 Chen M 40 30000 Result Chen 30000
1002 Furyk M 45 45000 Furyk 45000
1003 Miles F 42 39000 Miles 39000

SQL Statement: SELECT last_name, salary


FROM employee;
Presentation title 16

RELATIONAL ALGEBRA: UNARY OPERATORS

The PROJECT operator (p): Exercise


• Given the employee relation below, write relational algebra
expression that projects dept and head.
last_name first_name dept head
Smith Mary Sales Secci
Adams Peter IT Peterson Result?
Singh Lovedeep Sales Secci
Kim Susan IT Peterson

Relational algebra:
Presentation title 17

RELATIONAL ALGEBRA: UNARY OPERATORS

The PROJECT operator (p): Exercise – SQL Equivalent


• Given the employee relation below, write SQL query that returns
dept and head columns from it.
last_name first_name dept head
Smith Mary Sales Secci
Adams Peter IT Peterson Result?
Singh Lovedeep Sales Secci
Kim Susan IT Peterson

SQL query:
Presentation title 18

RELATIONAL ALGEBRA: UNARY OPERATORS

Combining SELECT (s) and PROJECT (p) Operators


• Returns subset of selected attributes from a relation for the tuples that
meet certain criteria:
p <attribute list>(s <criteria>(𝑅))
• The Project operator filters columns/attributes, while the Select operator
filters rows
• Note: Even basic SQL queries often require using these two operators
together.
Presentation title 19

RELATIONAL ALGEBRA: UNARY OPERATORS


Combining SELECT (s) PROJECT (p) Operators: Example
• Given the employee relation below, write relational algebra expression that
returns employee last_name and salary for employees who are at least 35 years old
and whose salary is above 30000.
ID last_name first_name age salary
1000 Anderson F 25 20000 last_name salary
1001 Chen M 40 30000 Result
Furyk 45000
1002 Furyk M 45 45000 Miles 39000
1003 Miles F 42 39000

Relational algebra: plast_name,salary(sage>=35^salary>30000(employee))


Presentation title 20

RELATIONAL ALGEBRA: UNARY OPERATORS


Combining SELECT (s) PROJECT (p) Operators: SQL Equivalent
• Given the employee relation below, write relational algebra expression that
returns employee last_name and salary for employees who are at least 35 years old
and whose salary is above 30000.
ID last_name first_name age salary
1000 Anderson F 25 20000 last_name salary
1001 Chen M 40 30000 Result
Furyk 45000
1002 Furyk M 45 45000 Miles 39000
1003 Miles F 42 39000

SQL Query: SELECT last_name, first_name FROM employee


WHERE age >=35 AND salary>30000;
Presentation title 21

RELATIONAL ALGEBRA: UNARY OPERATORS


The RENAME operator (r)
• Enables you to rename either a relation, or attributes, or both.
• Rename relation
𝜌𝑆(𝑅)
• Rename attributes
𝜌(𝐵1,𝐵2,…𝐵𝑛)(𝑅)
• Rename relation and its attributes
𝜌𝑆(𝐵1,𝐵2,…,𝐵𝑛)(𝑅)
Presentation title 22

RELATIONAL ALGEBRA: UNARY OPERATORS

The RENAME operator (r)


• Rename relation:

• Notation: rnew_name(R)

• E.g.: Let’s say we want to rename the employee relation to consultant


𝜌consultant(employee)
Presentation title 23

RELATIONAL ALGEBRA: UNARY OPERATORS

The RENAME operator (r)


• Rename attribute(s):
• Notation: rold_name -> new_name(R)

• Let’s say we want to rename ID the employee relation to emp_id


𝜌ID -> emp_id(employee)
Presentation title 24

RELATIONAL ALGEBRA: UNARY OPERATORS

The RENAME operator (r)


• Rename both a relation and attributes:
• Notation: rnew_name (rold_name -> new_name(R))
• Example (combine the previous 2 examples):
𝜌consultant(𝜌ID -> emp_id(employee))
Presentation title 25

RELATIONAL ALGEBRA:
OPERATORS FROM SET THEORY

• UNION (υ)
• INTERSECTION (∩),
• DIFFERENCE (-)
• CARTESIAN PRODUCT (x)
Presentation title 26

RELATIONAL ALGEBRA:
UNION OPERATION
• UNION is symbolized by ∪ symbol.
• It includes all tuples that are in tables A or in B. It R1 R2
also eliminates duplicate tuples.
• For a union operation to be valid, the following
conditions must hold –
R1 R1 ∪ R2 R2
• R and S must be the same number of attributes.
• Attribute domains need to be compatible.
• Duplicate tuples should be automatically removed.
Presentation title 27

RELATIONAL ALGEBRA:
UNION OPERATION
Presentation title 28

RELATIONAL ALGEBRA:
UNION OPERATION
Union operation
that requires
renaming entities
Presentation title 29

RELATIONAL ALGEBRA:
UNION OPERATION
Union operation
that requires
renaming
attributes
Presentation title 30

RELATIONAL ALGEBRA:
INTERSECTION OPERATION

• An intersection is defined by the


symbol ∩
• A∩B
• Defines a relation consisting of a
set of all tuple that are in both A
and B.
• However, A and B must be union-
compatible.
Presentation title 31

RELATIONAL ALGEBRA:
INTERSECTION OPERATION
Intersection:
Example
Presentation title 32

RELATIONAL ALGEBRA:
DIFFERENCE OPERATION
• Denoted by the symbol “–”.
• The result of A – B, is a relation which
includes all tuples that are in A but not in B.
• The attribute name of A has to match with
the attribute name in B.
• The two-operand relations A and B should
be either compatible or Union compatible.
• The result should be a defined relation
consisting of the tuples that are in relation
A, but not in B.
Presentation title 33

RELATIONAL ALGEBRA:
DIFFERENCE OPERATION

Set
Difference:
Example
Presentation title 34

RELATIONAL ALGEBRA:
CARTESIAN PRODUCT OPERATION

• Cartesian Product in DBMS is an operation used to merge


columns from two relations.
• Generally, a cartesian product is never a meaningful operation when
it performs alone.
• However, it becomes meaningful when it is followed by other
operations.
• It is also called Cross Product or Cross Join.
Presentation title 35

RELATIONAL ALGEBRA:
CARTESIAN PRODUCT OPERATION

Cartesian
Product:
Example

You might also like