Module - 3 DE - Part 1
Module - 3 DE - Part 1
Module-3 Overview
Relational Algebra
Unary Relational Operations
Relational Algebra Operations From Set Theory
Binary Relational Operations
Additional Relational Operations
Examples of Queries in Relational Algebra
Relational Calculus
Tuple Relational Calculus
Domain Relational Calculus
Query
Language
Procedural Non-Procedural
Query Language Query Language
Relational Relational
Algebra Calculus
Structure Query
Language
04-02-2025
Relational Algebra
Relational Algebra is a procedural query language which takes a relation as an
input and generates a relation as an output.
Relational Algebra is a language for expressing relational database queries.
It uses operators to perform queries. An operator can be either unary or binary.
Types of operators in relational algebra:
1. Basic/Fundamental Operators
2. Additional/Derived Operators
Relational algebra operations work on one or more relations to define another
relation without changing the original relations.
Relational algebra is performed recursively.
04-02-2025
Example
In relational algebra, input is a relation (table from which data has to be accessed) and
output is also a relation (a temporary table holding the data asked for by the user).
Relational algebra is performed recursively on a relation and intermediate results are also
considered relations.
σAge>17(Student)
Student
Roll_no Name Age Address
Query-3: Select Student whose age is greater
1 A 20 Bhopal than 17
2 B 17 Mumbai
3 C 16 Mumbai
4 D 19 Delhi
5 E 18 Delhi
Note: In Selection operation, schema of resulting relation is identical to schema of input relation
04-02-2025
Student
Roll_no Name Age Address
Query-3: Select Student whose age is greater
1 A 20 Bhopal than 17
2 B 17 Mumbai
3 C 16 Mumbai
σAge>17(Student)
4 D 19 Delhi
Roll_no Name Age Address
5 E 18 Delhi
1 A 20 Bhopal
Query-1: Select Student whose roll no is 2 4 D 19 Delhi
σRoll_no=2(Student) 5 E 18 Delhi
Roll_no Name Age Address Query-4: Select Student whose age is greater
2 B 17 Mumbai than 17 who lives in Delhi
σAge>17 ^ Address=“Delhi”(Student)
Query-2: Select Student whose name is D
σName=“D”(Student) Roll_no Name Age Address
Roll_no Name Age Address 4 D 19 Delhi
4 D 19 Delhi 5 E 18 Delhi
Note: In Selection operation, schema of resulting relation is identical to schema of input relation
Examples
Select tuples from a relation "Books" where subject is "database" and price is "450“
Select tuples from a relation "Books" where subject is "database" and price is "450" or have
a publication year after 2010
04-02-2025
Examples
Example: Display the columns roll_no and name from the relation Student.
Π roll no, name(Student)
04-02-2025
Set Operators
Set Operators
Set
Set Difference
Union (∪) Intersection
(-)
(∩)
Set Operators
Set operators: Union, intersection and difference, binary operators as they takes
two input relations
To use set operators on two relations,
The two relations must be Compatible
Two relations are Compatible if –
1. Both the relations must have same number of attributes (or columns).
2. Corresponding attribute (or column) have the same domain (or type).
Duplicate tuples are automatically eliminated
04-02-2025
1 2
2
2 3 1 4
3
3 4
R S RUS
Name
A Que-1: Find the names of the authors who have either written a
B book or an article or both.
C
D Πauthor(Books) U Πauthor(Articles)
G
H
04-02-2025
1 2
2
2 3 1 4
3
3 4
R S R∩S
Student Employee
(Student) ∩ (Employee)
Roll_no Name Emp_no Name
1 A 2 B Roll_no Name
2 B 8 G 2 B
3 C 9 H
4 D Note: Union is
commutative: A ∩ B =
B∩A
Student Employee
(Student) ∩ (Employee)
Roll_no Name Emp_no Name
1 A 2 B Roll_no Name
2 B 8 G 2 B
3 C 9 H
4 D Note: Union is
commutative: A ∩ B =
Πname(Student) ∩ Πname(Employee) B∩A
Name
B Que-1: Find the names of the authors who have written a book
and an article both.
Πauthor(Books) ∩ Πauthor(Articles)
04-02-2025
1 2
2
2 3 1 4
3
3 4
R S R-S
Symbol: -
Syntax: R-S
RA: R-S
SQL: SELECT * FROM R
MINUS
SELECT * FROM S;
04-02-2025
Student Employee
(Student) - (Employee)
Roll_no Name Emp_no Name
1 A 2 B Roll_no Name
2 B 8 G 1 A
3 C 9 H 3 C
4 D 4 D
Note:
1. Set Difference is non-commutative: A-B ≠ B-A
2. R-(R-S) = R ∩ S
Intersection can be is derived from set difference that’s why intersection is derived operator.
Student Employee
(Student) - (Employee)
Roll_no Name Emp_no Name
1 A 2 B Roll_no Name
2 B 8 G 1 A
3 C 9 H 3 C
4 D 4 D
Note:
1. Set Difference is non-commutative: A-B ≠ B-A
2. R-(R-S) = R ∩ S
Intersection can be is derived from set difference that’s why intersection is derived operator.
Que-1: Find the names of the authors who have written books but not article.
Πauthor(Books) - Πauthor(Articles)
04-02-2025
R1 R2 R1 x R2
A1 A2 B1 B2 B3 A1 A2 B1 B2 B3
X 1 X 10 A X 1 X 10 A
Y 2 Y 10 A X 1 Y 10 A
Y 20 B X 1 Y 20 B
Z 10 B X 1 Z 10 B
Y 2 X 10 A
Y 2 Y 10 A
Y 2 Y 20 B
Y 2 Z 10 B
04-02-2025
Characteristics
If relation R1 and R2 have a & b attributes respectively, then resulting relation will have a + b
attributes from both the input relations.
If relation R1 and R2 have nl & n2 tuples respectively, then resulting relation will have n1 x
n2 tuples, combining each possible pair of tuples from both the relations.
R1 R2 R1 X R2
Attributes a B a+b
Tuples n1 n2 n1 X n2
If both input relation have some attribute having same name, change the name of the
attribute with the name of the relation "relation_name.attribute_name"
If both input relation have some attribute having same name, change the name of the
attribute with the name of the relation "relation_name.attribute_name"
R1 R2 R1 x R2
A1 A2 A1 B2 B3 R1.A1 A2 R2.A1 B2 B3
X 1 X 10 A X 1 X 10 A
Y 2 Y 10 A X 1 Y 10 A
Y 20 B X 1 Y 20 B
Z 10 B X 1 Z 10 B
Y 2 X 10 A
Y 2 Y 10 A
Y 2 Y 20 B
Y 2 Z 10 B
04-02-2025
Composition Operations
σA1=B1(R1 X R2)
R1 x R2
A1 A2 B1 B2 B3 A1 A2 B1 B2 B3
X 1 X 10 A X 1 X 10 A
Y 2 Y 10 A X 1 Y 10 A
Y 2 Y 20 B X 1 Y 20 B
X 1 Z 10 B
Y 2 X 10 A
Y 2 Y 10 A
Y 2 Y 20 B
Y 2 Z 10 B
Composition Operations
ΠA1(σB2=20(R1 X R2))
R1 x R2
A1 A1 A2 B1 B2 B3
X X 1 X 10 A
Y X 1 Y 10 A
X 1 Y 20 B
X 1 Z 10 B
Y 2 X 10 A
Y 2 Y 10 A
Y 2 Y 20 B
Y 2 Z 10 B
04-02-2025
Rename Operators
The results of relational algebra are also relations but without any name.
The RENAME operator is used to rename the output of a relation.
Sometimes it is simple and suitable to break a complicated sequence of operations and
rename it as a relation with different names. Reasons to rename a relation can be many, like:
1. We may want to save the result of a relational algebra expression as a relation so that
we can use it later.
2. We may want to join (or cartesian product) a relation with itself, in that case, it
becomes too confusing to specify which one of the tables we are talking about, in
that case, we rename one of the tables and perform join operations on them.
Symbol: rho ρ
Notation 1: ρX(E)
Where the symbol ' ρ' is used to denote the RENAME operator and E is the result of
expression or sequence of operation which is saved with the name X
SQL: Use the AS keyword in the SELECT clause to rename attributes (columns)
(Eg: RollNo AS SNo renames RollNo to SNo)
SELECT column_name AS new_column_name
FROM tablename
WHERE condition
04-02-2025
Example:
Suppose we want to do Cartesian product between same table then one of the table should be
renamed with another name.
R X R (Ambiguity means confusion will be there) RxR
Rename Operation
Notation 1: ρX(A1,A2,….An)(E)
It returns the result of expression E under the name X, and with the attributes renamed
to A1, A2,.....A_{n}.
Notation 2: ρ(A1,A2,….An)(E)
It returns the result of expression E with the attributes renamed to A1, A2, ...., An.
04-02-2025
Example
Example-1: Query to find the female students from Student relation and rename the
relation Student as Female Student and the attributes of Student RollNo, SName as Sno,
Name.
Student FemaleStudent
RollNo Sname Gender SNo Name
1 Neha F 1 Neha
Rename to
2 Swati F 2 Swati
3 Rohan M 4 Puja
4 Puja F
5 Rahul M
Example
Example-1: Query to find the female students from Student relation and rename the
relation Student as Female Student and the attributes of Student RollNo, SName as Sno,
Name.
Student FemaleStudent
RollNo Sname Gender SNo Name
1 Neha F 1 Neha
Rename to
2 Swati F 2 Swati
3 Rohan M 4 Puja
4 Puja F
5 Rahul M
ρFemaleStudent(Sno,Name)(ΠRollNo,Sname(σGender=‘F’(Student)))
04-02-2025
Examples
Example-2: Query to rename the attributes Name, Age of table Person to N, A.
Example-3: Query to rename the table name Project to Work and its attributes to P, Q,
R.
Example-4: Query to rename the first attribute of the table Student with attributes A, B,
C to P.
Examples
Example-2: Query to rename the attributes Name, Age of table Person to N, A.
ρ (N,A)(Person)
Example-3: Query to rename the table name Project to Work and its attributes to P, Q,
R.
ρ Work(P,Q,R)(Project)
Example-4: Query to rename the first attribute of the table Student with attributes A, B,
C. From A to P.
ρ (P,B,C)(Student)
Example-5: Query to rename the table name Loan to L.
ρ L(Loan)
04-02-2025
σbranch-name=“Perryridge”(loan) L-11
L-14
Round Hill
Downtown
900
1500
loan L-15 Perryridge 1500
loan-number branch-name amount L-16 Perryridge 1300
L-15 Perryridge 1500 Output L-17 Downtown 1000
L-16 Perryridge 1300 L-23 Redwood 2000
L-93 Mianus 500
Πcustomer-name(σcustomer-city=“Harrison”(Customer)) Curry
Glenn
North
Sand Hill
Rye
Woodside
Green Walnut Stamford
Hayes Main Harrison
customer Johnson Alma Palo alto
customer- Jones Main Harrison
name Lindsay Park Pittsfield
Output
Hayes Smith North Rye
Jones Turner Putnam Stamford
Williams Nassau Princeton
Πcustomer-name(borrower) ∩ Πcustomer-name(depositor)
customer-name
branch (branch-name, branch-city, assets)
Hayes customer (customer-name, customer-street, customer-city)
Output
Jones account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
Smith depositor (customer-name, account-number)
borrower (customer-name, loan-number)
04-02-2025
Πcustomer-name(depositor) - Πcustomer-name(borrower)
customer-name branch (branch-name, branch-city, assets)
Lindsay customer (customer-name, customer-street, customer-city)
Output account (account-number, branch-name, balance)
Johnson loan (loan-number, branch-name, amount)
Turner depositor (customer-name, account-number)
borrower (customer-name, loan-number)
Πcustomer-name (σbranch-name=“Perryridge”
(σborrower.loan-number=loan.loan-number
(borrower x loan)))
customer-name
Output
Adams
Hayes
customer-name
Output
Adams
Strategy:
1. Find those balances that are not largest (as a temporary relation).
• Rename account relation as d so that we can compare each account balance with all others
Πaccount.balance(σaccount.balance<d.balance(account x ρd(account)))
2. Use set difference to find those account balances that were not found in the earlier step.
• Take set difference between relation Πbalance(account) and temporary relation just computed,
to obtain the result