0% found this document useful (0 votes)
35 views30 pages

Module - 3 DE - Part 1

The document provides an overview of relational algebra, relational calculus, and SQL, detailing their roles as query languages in relational database systems. It distinguishes between procedural and non-procedural query languages, explaining how relational algebra is procedural while relational calculus is non-procedural. Additionally, it covers various relational algebra operations, including selection, projection, and set operators, along with examples of their application.

Uploaded by

Sivangi Tripathy
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)
35 views30 pages

Module - 3 DE - Part 1

The document provides an overview of relational algebra, relational calculus, and SQL, detailing their roles as query languages in relational database systems. It distinguishes between procedural and non-procedural query languages, explaining how relational algebra is procedural while relational calculus is non-procedural. Additionally, it covers various relational algebra operations, including selection, projection, and set operators, along with examples of their application.

Uploaded by

Sivangi Tripathy
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/ 30

04-02-2025

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

SQL(STRUCTURED QUERY LANGUAGE)


DDL(Data Definition Language)
DML(Data Manipulation Language)
DCL(Data Control Language)

Relational Query Languages


Relational database systems are expected be equipped with a query language
that assist its users to query the database instances .
Query Language is a language in which user requests information from the
database. E.g.: SQL
Query= “Retrieval Program”
Types of Query Language
Procedural
Language
Query Language
Non-Procedural
Language
04-02-2025

Procedural vs. Non-Procedural


1. Procedural Query Language
In Procedural query language, user instructs the system to perform a series of
operations to produce the desired results.
Users tells what data to be retrieved from database and how to retrieve it.
2. Non-Procedural or Declarative Query Language
In Non-procedural query language, user instructs the system to produce the
desired result without telling the step by step process.
User tells what data to be retrieved from database but doesn’t tell how to
retrieve it.

Two “Pure” Query Language

Two “Pure” query languages or two mathematical query language:


1. Relational Algebra
2. Relational Calculus
2.1. Tuple Relational Calculus
2.2. Domain Relational Calculus
04-02-2025

Relational Algebra vs. Relational Calculus


1. Relational Algebra
Relational algebra is a procedural query language.
It is more operational, very useful for representing execution plan.
Procedural: What data is required and How to get those data.
2. Relational Calculus
2.1. Tuple Relational Calculus
2.2. Domain Relational Calculus

Relational calculus is a non-procedural query language.


It is non-operational or declarative.
Non-procedural: What data they want without specifying how to get those data.

Query
Language

Procedural Non-Procedural
Query Language Query Language

Relational Relational
Algebra Calculus

Structure Query
Language
04-02-2025

Relational Algebra, Calculus, RDBMS, & SQL


Relational Model is a theoretical concept.
RDBMS is a practical implementation of relational model.
SQL(Structured Query Language) is used to write query on RDBMS.
Relational Algebra and calculus are the mathematical system or query language
used on relational model.
SQL is a practical implementation of Relational algebra and calculus

Relation Model RDBMS


Relational Algebra, Relational Calculus SQL
Algorithm Code
Conceptual Reality
Theoretical Practical

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.

We can use Relational Algebra to fetch


data from this Table(relation)
Select Name students with age less than 17
ID Name Age
1 Priya 17 Name
2 Ram 19 Hari
3 Hari 15 Puja
4 Puja 13

Relational Algebra Operations


1. Basic/Fundamental Operations: 2. Additional/Derived Operations:
1) Selection (σ) 1) Natural Join (⋈)
2) Projection (Π) 2) Left, Right, Full Outer Join (⟕, ⟖, ⟗)
3) Union (U) 3) Set Intersection (∩)
4) Set Difference (-) 4) Division (÷)
5) Cartesian product (X) 5) Assignment (←)
6) Rename (ρ)
 All are Binary operators because they
 Select, Project and Rename are Unary operate on two relations
operators because they operate on one
relation
 Union, Difference and Cartesian product
are Binary operators because they operate
on two relation
04-02-2025

Selection (σ) Operator


 Selection Operator (σ) is a unary operator in relational algebra that performs a selection
operation.
 It selects tuples (or rows) that satisfy the given condition (or predicate) from a relation.
 It is denoted by sigma (σ).
 Notation- σp(r) or σ(Condition)(Relation Name)
 p is used as a propositional logic formula which may use logical connectives: ^ (AND)
V(OR) ! (NOT) and relational operators like =, =, <, >, <, ≥ to form the condition
 The WHERE clause of a SQL command corresponds to relational select σ().
SQL: SELECT * FROM R WHERE C;
 Example: Select tuples from student table whose age is greater than 17

σ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

Query-1: Select Student whose roll no is 2

Query-4: Select Student whose age is greater


than 17 who lives in Delhi

Query-2: Select Student whose name is D

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“

 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

 Select tuples from a relation "Books" where subject is "database“


σsubject=“database”(Books)
 Select tuples from a relation "Books" where subject is "database" and price is "450“
σsubject=“database”^ price=450(Books)
 Select tuples from a relation "Books" where subject is "database" and price is "450" or have
a publication year after 2010
σsubject=“database” ^ price=450 V year>2010 (Books)

Projection (Π) Operator


 Projection (Π) Operator is a unary operator in relational algebra that performs a projection
operation.
 It projects (or displays) the particular columns (or attributes) from a relation and it delete
column(s) that are not in the projection list.
 It is denoted by Π
 Notation- Π A1,A2,….An(r) or Π Attribute_list(relation name)
Where A1, A2,…,An are attribute names of relation r.
 Duplicate rows are automatically eliminated from result

 The SQL SELECT command corresponds to relational project π().


SQL: SELECT A1,A2, An FROM R;

 Example: Display the columns roll_no and name from the relation Student.
Π roll no, name(Student)
04-02-2025

Student Roll_no Name Age


Query-2: Display the roll_no Query-3: Display the age students
1 A 20 and name of students
2 B 17
3 C 16
4 D 19
5 E 18
6 F 18

Query-1: Display (or project) the


name of students

Query-4: Display the roll_no and name of students whose age in


greater than 17

Student Roll_no Name Age


Query-2: Display the roll_no Query-3: Display the age students
1 A 20 and name of students
2 B 17 Πage(Student)
3 C 16 Πroll_no,name(Student) Age
4 D 19 20
Roll_no Name
5 E 18 17
1 A
6 F 18 16
2 B
19
Query-1: Display (or project) the 3 C
18
name of students 4 D
Πname(Student) 5 E Note: By default, projection removes
duplicate values
6 F
Name
A Query-4: Display the roll_no and name of students whose age in
B greater than 17
Roll_no Name
C Πroll_no,name(σAge>17(Student)) 1 A
D 4 D
E 5 E
F 6 F
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

Union (U) Operator

1 2
2
2 3 1 4
3
3 4

R S RUS

Union (U) Operator


 Suppose R and S are two relations. The Union operation selects all the tuples that are either
in relations R or S or in both relations R & S.
 It eliminates the duplicate tuples.
 For a union operation to be valid, the following conditions must hold –
1. Two relations R and S both have same number of attributes.
2. Corresponding attribute (or column) have the same domain (or type).The attributes
of R and S must occur in the same order.
3. Duplicate tuples should be automatically removed
 Symbol: U
 Notation: RUS
RA: RUS
SQL: SELECT * FROM R UNION SELECT * FROM S;
04-02-2025

Student Employee (Student) U (Employee)


Roll_no Name Emp_no Name Roll_no Name
1 A 2 B 1 A
2 B 8 G 2 B
3 C 9 H 3 C
4 D 4 D Note: Union is
8 G commutative: A U B =
9 H BUA

Student Employee (Student) U (Employee)


Roll_no Name Emp_no Name Roll_no Name
1 A 2 B 1 A
2 B 8 G 2 B
3 C 9 H 3 C
4 D 4 D Note: Union is
8 G commutative: A U B =
Πname(Student) U Πname(Employee) 9 H BUA

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

Set Intersection (∩) Operator

1 2
2
2 3 1 4
3
3 4

R S R∩S

Set Intersection (∩) Operator


 Suppose R and S are two relations. The Set Intersection operation selects all the tuples that
are in both relations R & S.
 For a Set Intersection to be valid, the following conditions must hold
1. Two relations R and S both have same number of attributes.
2. Corresponding attribute (or column) have the same domain (or type).
• The attributes of R and S must occur in the same order.
 Symbol: ∩
 Syntax: R ∩ S
RA: R ∩ S
SQL: SELECT * FROM R
INTERSECT
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 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

Set Difference (-) Operator

1 2
2
2 3 1 4
3
3 4

R S R-S

Set Difference (-) Operator


 Suppose R and S are two relations. The Set Difference operation selects all the tuples that
are present in first relation R but not in second relation S.
 For a Set Difference to be valid, the following conditions must hold
1. Two relations R and S both have same number of attributes.
2. Corresponding attribute (or column) have the same domain (or type).
• The attributes of R and S must occur in the same order.

 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

Cartesian Product/ Cross Product


 Cartesian Product is fundamental operator in relational algebra
 Cartesian Product combines information of two different relations into one.
 It is also called Cross Product.
 Generally, a Cartesian Product is never a meaningful operation when it is
performed alone. However, it becomes meaningful when it is followed by other
operations.
 Generally it is followed by select operations.
 Symbol: x
 Notation: R1 x R2
 SQL: SELECT * FROM R1, R2

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

Rename Operators in SQL

 SQL: Use the AS keyword in the FROM clause


(Eg: Students AS Students1 renames Students to Students 1)
SELECT column_name
FROM tablename AS new_table_name
WHERE condition

 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

R R.A R.B R.A R.B


A B X 1 X 1
X 1 X 1 Y 2
Y 2 Y 2 X 1
Y 2 Y 2
R X ρS(R)
 R X ρS(R) (Rename R to S)
R.A R.B S.A S.B
X 1 X 1
X 1 Y 2
Y 2 X 1
Y 2 Y 2

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.

Example-5: Query to rename the table name Loan to L.

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

Relational Algebra Example


Questions on Banking
System

Banking System Example

branch (branch-name, branch-city, assets)


customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
04-02-2025

Selection (σ) Example-1


loan

Find all loans made at “Perryridge” branch loan-number branch-name amount

σ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

Find all loans of over $1200


branch (branch-name, branch-city, assets)
σamount>1200(loan) customer (customer-name, customer-street, customer-city)
loan account (account-number, branch-name, balance)
loan-number branch-name amount loan (loan-number, branch-name, amount)
L-14 Downtown 1500 depositor (customer-name, account-number)
borrower (customer-name, loan-number)
L-15 Perryridge 1500
L-16 Perryridge 1300
Output
L-23 Redwood 2000
04-02-2025

Selection (σ) Example-2


 Find all types who have taken loans of more than $1200 made by the
“Perryridge” branch loan
loan-number branch-name amount
σbranch-name=“Perryridge”^ amount>1200(loan) L-11 Round Hill 900
L-14 Downtown 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

Projection (Π) Example-1


loan
Find all loans numbers and the amount of the loans loan-number branch-name amount
Πloan-number,amount(loan) L-11 Round Hill 900
L-14 Downtown 1500
L-15 Perryridge 1500
loan
L-16 Perryridge 1300
loan-number amount
L-17 Downtown 1000
L-11 900
L-23 Redwood 2000
L-14 1500 Output
L-93 Mianus 500
L-15 1500
L-16 1300
branch (branch-name, branch-city, assets)
L-17 1000
customer (customer-name, customer-street, customer-city)
L-23 2000 account (account-number, branch-name, balance)
L-93 500 loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
04-02-2025

Projection (Π) Example-2


loan
Find all loan number for each loan of an amount loan-number branch-name amount
greater than $1200 L-11 Round Hill 900

Πloan-number(σamount>1200(loan)) L-14 Downtown 1500


L-15 Perryridge 1500
L-16 Perryridge 1300
loan
L-17 Downtown 1000
loan-number L-23 Redwood 2000
L-14 Output
L-93 Mianus 500
L-15
L-16 branch (branch-name, branch-city, assets)
L-23 customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

Projection (Π) Example-3 customer

customer- customer- customer-


name street city
Find those customers name who lives in “Harrison” Adams Spring Pittsfield
Brooks Senator Brooklyn

Π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

branch (branch-name, branch-city, assets)


customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
04-02-2025

Union (U) Example-1


Find the names of all customers who have a loan, an account, or both,
from the bank
Πcustomer-name(borrower) U Πcustomer-name(depositor)
customer-name
Adams
Curry
Hayes
Output
Jackson
branch (branch-name, branch-city, assets)
Jones customer (customer-name, customer-street, customer-city)
Smith account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
Williams depositor (customer-name, account-number)
Lindsay borrower (customer-name, loan-number)
Johnson
Turner

Intersection (∩) Example-1


Find the names of all customers who have a loan and an account at bank

Π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

Set Difference (-) Example-1


Find the names of all customers who have an account but no loan from
the bank

Π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)

Cartesian Product(x) Example-1


 Find the names of all customers who have a loan at the
Perryridge branch

Πcustomer-name (σbranch-name=“Perryridge”
(σborrower.loan-number=loan.loan-number
(borrower x loan)))
customer-name
Output
Adams
Hayes

branch (branch-name, branch-city, assets)


customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
04-02-2025

Cartesian Product(x) Example-2


 Find the names of all customers who have a loan at the Perryridge branch but do not have an
account at any branch of the bank

Πcustomer-name (σbranch-name=“Perryridge” (σborrower.loan-number=loan.loan-


number(borrower x loan)))- Πcustomer-name (depositor)

customer-name
Output
Adams

branch (branch-name, branch-city, assets)


customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

Rename Operator(ρ) Example-1


 Find the largest account balance in the bank

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

You might also like