0% found this document useful (0 votes)
28 views42 pages

Relational Algebra

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)
28 views42 pages

Relational Algebra

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
You are on page 1/ 42

RELATIONAL ALGEBRA

R
Relational Algebra

• Basic set of operations for the relational model


• Similar to algebra that operates on numbers
• Operands and results are relations instead of numbers
Unary Relational Operations

 SELECT (symbol: σ)
 PROJECT (symbol: π)
 RENAME (symbol: ρ)
SELECT(RESTRICT)
• SELECT is denoted by the lowercase Greek letter sigma (σ). Sigma is followed by
the condition to be evaluated (called a predicate) as a subscript, and then the relation is listed in
parentheses.

• SELECT referred to as a unary operator because it only uses one table as input. It yields values
for all rows found in the table that satisfy a given condition.
• Comparison operators:<, , , >, =, 
• Logical operators:
• ^  “and”, V  “or”, ¬ ”not”
EXAMPLE: Display /List all account whose amount is greater
than 8000

Given relation/table Answer



σ Amt>8000 (Account)

• Output
Find the details of the members who were
born on October 21, 1997.
Given relation/table
Details of the members who were born on
21/10/1997.
Given relation/table Answer
Display those Employees who are either Assistant Professors or in the Economics
department

Given relation Answer


EMP
Display those Employees who are either Assistant Professors or in the Economics
department

Given relation Answer


EMP
•σ Rank = 'Assistant' V Dept = 'Econ' (EMP)
• Output
Projection

• PROJECT is denoted by the Greek letter pi (π). Pi is followed by the list of attributes to be returned
as subscripts, and then the relation listed in parentheses.
Produce a list of Person showing only the name and hobby.

• Given relation • Answer


Display the names and departments of the employees

• Given relation
• EMP
Display the names and departments of the employees

• Given relation • Answer


• EMP
• Name, Dept (EMP)
Show the names of all employees working in the CS department:

• Give relation
Show the names of all employees working in the CS department:

• Give relation • Answer


• Name ( σDept=‘CS’(EMP))
Display Id & Name, whose Hobbies are either stamps or coins

• Given relation
Display Id & Name, whose Hobbies are either stamps or coins

• Given relation • Answer


•  Id, Name ( Hobby=’stamps’ V Hobby=’coins’ (Person) )
Rename
• Rename operation
• Represented by lower letter rho
• Syntax
• (A1,A2,A3,..An) (Relation)
Rename Name and department attributes to Name1 and Name2

• Given relation • Answer


• p(Name1,Name2)(Emp)
Rename the first attribute of the relation
Student with attributes A, B, C to P.
Rename the first attribute of the relation
Student with attributes A, B, C to P.

• p(P, B, C)(Student)
Rename the relation name Project to Pro and
its attributes to P, Q, R.
• p Pro(P, Q, R) (Project)
Relational Algebra Operations From Set Theory

 UNION (υ)

 INTERSECTION ( )

 DIFFERENCE (-)

 CARTESIAN PRODUCT ( x )
Union
• UNION combines all rows from two tables, excluding duplicate rows.

• To be used in the UNION, it must be a union- compatible

• UNION-COMPATIBLE - they have the same degree and the domains of the corresponding
attributes are the same.
Find: R S

• Output
Find names of all customers that have either a
bank account or a loan at the bank
Find names of all customers that have either a
bank account or a loan at the bank
• πcust_name(depositor) ∪ πcust_name(borrower)
List/Display the names of all employees and students
List/Display the names of all employees and students

•π Name (Employee) ∪ π Name (Student)


OUTPUT:
Intersection
• INTERSECT yields only the tupples that appear in both relations
Find Student1  Student2
• Consider the following relations
Student1 Student 2

Sid Sname Saddress Sid Sname Saddress

013 A Melissa Las Pinas 011 A Marian Makati

012 A Ivanna Taguig


012 A Ivanna Taguig
Find Student1  Student2
• Consider the following relations Output

Student1 Student 2
Sid Sname Saddress
Sid Sname Saddress Sid Sname Saddress

013 A Melissa Las Pinas 011 A Marian Makati

012 A Ivanna Taguig


012 A Ivanna Taguig
012 A Ivanna Taguig
Find THE NAME employees and at the same time students from Relation Employee.

• Given relation
Find THE NAME employees and at the same time students from Relation Employee.

• Given relation
•π Name (Employee) ∩π Name (Student)
Difference

• DIFFERENCE yields all tupples in one table that are not found in the other table.
Find the names of those employees that are not students

• π Name (Employee) — π
Name (Student)
Cartesian- product (X)

• Also called cross product or


cross join
Produce all combinations of tuples from two relations

• Given relations •RXS


• OUTPUT
Given relation
Output
Given relation

Output
Display all the students of department ECE whose fees is greater than
equal to 10000 and belongs to Team other than A.

• Student • Output

You might also like