0% found this document useful (0 votes)
79 views61 pages

Relational Algebra and SQL Overview

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 outlines various operations and examples of queries within these frameworks, emphasizing the practical implementation of SQL in relation to these theoretical concepts.

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)
79 views61 pages

Relational Algebra and SQL Overview

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 outlines various operations and examples of queries within these frameworks, emphasizing the practical implementation of SQL in relation to these theoretical concepts.

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

22-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
22-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
22-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
22-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.
22-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
22-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
22-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
22-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)
22-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
22-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
22-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;
22-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
22-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;
22-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)
22-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;
22-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)
22-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
22-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
22-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
22-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
22-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.
22-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)))
22-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)
22-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)
22-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
22-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)
22-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)
22-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)
22-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”
(σ[Link]-number=[Link]-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)
22-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” (σ[Link]-number=[Link]-


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

Π[Link](σ[Link]<[Link](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
22-02-2025

Rename Operator(ρ) Example-1


account
 Find the largest account balance in the bank

Πbalance(account) -Π[Link](σ[Link]<[Link](account x ρd(account)))


balance
balance
500
500 balance
400
400 Output 900
900
-
700
700
750
750
350
700
350

Join Operator and Its types

Introduction
 Cartesian product of two relations (A × B), gives us all the possible tuples that are
paired together.
 But it might not be feasible in certain cases to take a Cartesian product
where we encounter huge relations with thousands of tuples having a
considerable large number of attributes.
22-02-2025

Join Operation( )
 Join is an Additional / Derived operator which simplify the queries, but does not
add any new power to the basic relational algebra.
 Join is a combination of a Cartesian product followed by a selection process.
 Join = Cartesian Product + Selection
 A Join operation pairs two tuples from different relations, if and only if a given
join condition is satisfied.
 Symbol: ⋈
 A ⋈c B=σc(ΑΧΒ)

Difference
Cross Join/Cross Product/Cartesian
Joins (⋈) Product (X)
• Combination of tuples that satisfy • All possible combination of tuples
the filtering/matching conditions from the relations
• Fewer tuples then cross product, • Huge number of tuples and costly
might be able to compute efficiently to manage

RXS
R ⋈ S (Natural Join)
R S
A R.B S.B C
A B C A B B C 1 a a 3
1 a 3 Output 1 a a 3 Output 1 a b 4
2 b 4 2 b a 3
2 b b 4
2 b b 4
22-02-2025

Types of JOINS

JOINS

Inner Join Outer Join

Natural Left outer Right Full outer


Theta Join Equi Join
Join join outer join join

Types of JOINS
1. Inner Join 2. Outer Join
Contains only those tuples that satisfy the Contains matching tuples that satisfy
matching condition the matching condition, along with some
 Theta(θ) / Conditional Join or all tuples that do not satisfy the
matching Condition
 A ⋈θ B
 uses all kinds of comparison operators(<,>, <=, Contains all rows from either one or
>=,=,≠) both relation
 Equi Join  Left Outer Join
 Special case of theta join  Left relation tuples will always be in result
 uses only equality (=) comparison operator whether the value is matched or not
 Natural join  Right Outer Join
 A⋈B  Right relation tuples will always be in result
whether the value is matched or not
 Based on common attributes in both relation
 does not use any comparison operator  Full Outer Join
 Tuples from both relations are present in
result, whether the value is matched or not
22-02-2025

Inner Join
 An Inner join includes only those tuples that satisfy the matching criteria, while the
rest of tuples are excluded.
 Theta Join, Equi join, and Natural Join are called inner joins.

1. Theta(θ) / Conditional Join


 It combines tuples from different relations provided they satisfy the theta (θ) condition.
 It is a general case of join. And it is used when we want to join two or more relation based on
some conditions.
 The join condition is denoted by the symbol θ.
 It uses all kinds of comparison operators like <, >, <=, >=, =, ≠
 Notation: A ⋈θ B
 Where is a predicate/condition. It can use any comparison operator (<, >, <=, >=, =, ≠)
 A ⋈θ B=σ θ(ΑΧΒ)
22-02-2025

Example: Theta Join


S1 R1

Example: Theta Join


S1 R1

S1 [Link]<[Link] R1
[Link] sname rating age [Link] bid day
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.0 58 103 11/12/96

Equivalent to: σ [Link]<[Link](S1 Χ R1)


22-02-2025

Example: Theta Join


S1 R1

Basic Relational Algebra : σ [Link]<[Link](S1 Χ R1)


SQL:

Example: Theta Join


S1 R1

Join:- S1 [Link]<[Link] R1
SQL:-
22-02-2025

2. Equi Join
 When a theta join uses only equivalence (=) condition, it becomes a Equi join.
 Equi join is a special case of theta (or conditional) join where condition contains equalities
(=).

 Notation: A ⋈A.a1=B.b1 ^ ……………^[Link]=[Link] B

Example 1: Equi Join


S1 R1

S1 [Link]=[Link] R1
[Link] sname rating age [Link] bid day
22 dustin 7 45.0 22 101 10/10/96
58 rusty 10 35.0 58 103 11/12/96

Equivalent to: σ [Link]=[Link](S1 Χ R1)


22-02-2025

Example 1: Equi Join


S1 R1

Basic Relation Algebra: σ [Link]=[Link](S1 Χ R1)


:
SQL

Example 1: Equi Join


S1 R1

Join: S1 [Link]=[Link] R1
SQL :
22-02-2025

Example 2: Equi Join


Student
SID Name Std
Student [Link]=[Link] Subjects
101 Rohan 11
102 Pari 12
SID Name Std Class Subject
Subjects 101 Rohan 11 11 Maths
101 Rohan 11 11 Physics
Class Subject
102 Pari 12 12 English
11 Maths
102 Pari 12 12 Chemistry
11 Physics
12 English
12 Chemistry

3. Natural Join
 Natural join can only be performed if there is at least one common attribute (column) that
exist between two relations. In addition, the attributes must have the same name and
domain.
 Natural join does not use any comparison operator.
 It is same as equi join which occurs implicitly by comparing all the common attributes
(columns) in both relation, but difference is that in Natural Join the common attributes
appears only once. The resulting schema will change.
 Notation: A ⋈ B
 The result of the natural join is the set of all combinations of tuples in two relations A and B
that are equal on their common attribute names.
22-02-2025

3. Natural Join…
 The Natural Join of two relations can be obtained by applying a Projection operation to Equi
join of two relations. In terms of basic operators:

Natural Join = Cartesian product + Selection + Projection


 Natural Join (⋈ ) is by default inner join because the tuples which does not satisfy the
conditions of join does not appear in result set.
 Natural Join is very important.

Example 1: Natural Join

R=(A,B,C,D) S=(B,D,E)
 Resulting Schema of R ⋈ S =(A,B,C,D,E)

 R ⋈ S is defined as : Π R.A,R.B,R.C,R.D,S.E(σR.B=S.B ^ R.D=S.D(R X S))


R S R⋈S
22-02-2025

Example 2: Natural Join


S1 R1

S1 R1
sid sname rating age bid day
22 dustin 7 45.0 101 10/10/96
58 rusty 10 35.0 103 11/12/96

Equivalent to: Π [Link], sname,rating,age,bid,day(σ[Link]=[Link](S1 X R1))

Example 2: Natural Join


S1 R1

Basic Relational Algebra: Π [Link], sname,rating,age,bid,day(σ[Link]=[Link](S1 X R1))


SQL:
22-02-2025

Example 2: Natural Join


S1 R1

Join: S1 R1
SQL:

Example 3: Natural Join

Equivalent to:
Π CID, Course,[Link],Head(σ[Link]=[Link](Courses X HOD))
22-02-2025

Natural Join
 Natural join can only be performed if there is at least one common attribute
(column) that exist between two relations. In addition, the attributes must have the
same name and domain.
 In Natural Join resulting relation the common attributes appears only once.
 Notation: A ⋈ B (Natural join does not use any comparison operator)
 The result of the natural join is the set of all combinations of tuples in two relations
A and B that are equal on their common attribute names.
 The Natural Join of two relations can be obtained by applying a projection operation
to equi join of two relations. And in terms of basic operators:

Natural Join = Cartesian product + Selection + Projection

Example-1: Natural Join


 Find the names of all customers who have a loan at the bank, along with the loan
number and the loan amount.

Using Cartesian Product

Πcustomer-name,[Link]-number,amount(σ[Link]-number=[Link]-number(borrowerX
loan))
Using Natural Join

Πcustomer-name,[Link]-number,amount (borrower ⋈ loan)


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)
22-02-2025

Example-2: Natural Join


 Find the names of all branches with customers who have an account in the bank and
who live in Harrison.

Using Cartesian Product

Πbranch-namet(σcustomer-city=“Harrison”(customer ⋈ account ⋈ depositor))


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)
Note: Natural Join is associative borrower (customer-name, loan-number)

((customer ⋈ account )⋈ depositor= (customer ⋈ (account ⋈ depositor))


So we can write it (customer ⋈ account ⋈ depositor)

Example-3: Natural Join


 Find all customers who have both a loan and an account at the bank.

Using Natural Join

Πcustomer-name(borrower ⋈ depositor))

Using Set intersection

Πcustomer-name(borrower) ∩ Πcustomer-name(depositor)
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)
22-02-2025

Example-4: Natural Join


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

Using Cartesian product


Πcustomer-name (σbranch-name=“Perryridge” (σ[Link]-number=[Link]-
number(borrower x loan)))

Using Natural Join


Πcustomer-name(σbranch-name=“Perryridge”( borrower ⋈ loan))
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)

Example-5: Natural Join


 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

Using Cartesian product


Πcustomer-name (σbranch-name=“Perryridge” (σ[Link]-number=[Link]-
number(borrower x loan)))- Πcustomer-name (depositor)

Using Natural Join

Πcustomer-name(σbranch-name=“Perryridge”(borrower⋈loan))
-
Πcustomer-name (depositor) 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)
22-02-2025

Outer Join
 An Inner join includes only those tuples with matching attributes and the rest are
discarded in the resulting relation. Therefore, we need to use outer joins to include
all the rest of the tuples from the participating relations in the resulting relation.
 The outer join operation is an extension of the join operation that avoids loss of
information.
 Outer Join contains matching tuples that satisfy the matching condition, along with
some or all tuples that do not satisfy the matching condition.
 It is based on both matched or unmatched tuple.
 It contains all rows from either one or both relations are present
 It uses NULL values.
 NULL signifies that the value is unknown or does not exist

Example: Inner Join(Natural Join)


22-02-2025

Outer Join
 Outer Join = Natural Join + Extra information (from left table, right table or both table)

 There are three kinds of outer joins:


1. Left Outer join
2. Right Outer join
3. Full Outer Join

1. Left Outer Join (R1 R2)


When applying join on two relations R1 and R2, some tuples of R1 or R2 does not
appear in result set which does not satisfy the join conditions. But….
 In Left outer join, all the tuples from the Left relation R1 are included in the
resulting relation. The tuples of R1 which do not satisfy join condition will have
values as NULL for attributes of R2.
 In short:
 All record from left table
 Only matching records from right table
 Symbol: ⟕
 Notation: R1 ⟕ R2
22-02-2025

Example: Left Outer Join

Join: Courses ⟕ HOD

2. Right Outer Join (R1 R2)


 In Right outer join, all the tuples from the right relation R2 are included in the
resulting relation. The tuples of R2 which do not satisfy join condition will have
values as NULL for attributes of R1.
 In short:
 All record from right table
 Only matching records from left table
 Symbol: ⟖
 Notation: R1 ⟖ R2
22-02-2025

Example: Right Outer Join

Join: Courses ⟖ HOD

3. Full Outer Join (R1 R2)

 In Full outer join, all the tuples from both Left relation R1 and right relation R2 are
included in the resulting relation. The tuples of both relations R1 and R2 which do
not satisfy join condition, their respective unmatched attributes are made NULL.
 In short:
 All record from all table
 Symbol: ⟗
 Notation: R1 ⟗ R2
22-02-2025

Example: Full Outer Join

Join: Courses ⟗ HOD

Example: Outer Join


22-02-2025

Example: Inner Join (Natural Join)

Example: Left Outer Join


22-02-2025

Example: Right Outer Join

Example: Full Outer Join


22-02-2025

Division Operator(÷ or /)…


 Division operator is a Derived Operator, not supported as a primitive operator
 Suited to queries that include the keyword "all", or "every" like "at all", "for all" or "in all", "at
every", "for every" or "in every". Eg:
 Find the person that has account in all the banks of a particular city
 Find sailors who have reserved all boats
 Find employees who works on all projects of company.
 Find students who have registered for every course.
 In all these queries, the description after the keyword "all" or "every" defines a set which
contains some elements and the final result contains those records who satisfy these
requirements.
 Notation: A÷B or A/B (where A and B are two relations)

Division Operator(÷ or /)…


Division operator can be applied if and only if:
 Attributes of B is proper subset of Attributes of A.
 The relation returned by division operator will have attributes =(All attributes of A- All
attributes of B).
 The relation returned by division operator will return those tuples from relation A which are
associated to every B's tuple
Example 1: A B A÷B
X Y Y X

A 1 1 output A
÷
B 2 2

A 2

D 4
22-02-2025

Example
Example 2:

Example
Example 2:
22-02-2025

Expressing Division Operator(÷ or /)


using Basic Operators
 Division is a derived operator (or additional operator).
 Division can be expressed in terms of Cross Product, Set-Difference and Projection.

Idea:
 For A/B, compute all x values that are not 'disqualified' by some y value in B.
 x value is disqualified if by attaching y value from B, we obtain an xy tuple that is not in A.
Disqualified x values : Π X((Π X(A) X B)-A)
A B A÷B
So X Y Y X
A/B = Π X(A)- all Disqualified tuples 1 output
A 1 ÷ A
A/B = Π X(A)- Π X((Π X(A) X B)-A) B 2 2

A 2

D 4

A B Disqualified [Link] values: Π [Link]((Π [Link](A) X B)-A)


[Link] [Link] [Link]
Π [Link](A) B Π [Link](A) X B A ((Π [Link](A) X B)-A)
S1 P1
÷ P2 [Link] [Link] [Link]
[Link] [Link] [Link] [Link] [Link] [Link]
S1 P2 P4 S1 P2 S1 P2 S1 P1 S2 P4 S2
S1 P3 S2 P4 S2 P2 S1 P2 S3 P4 S3
S1 P4 S3 S3 P2 S1 P3
Π [Link]((Π [Link](A) X B)-A)
S2 P1 S4 S4 P2 S1 P4

S2 P2 S1 P4 S2 P1

S3 P2 S2 P4 S2 P2 Π [Link](A) Π [Link]((Π [Link](A) X B)-A)


S4 P2 S3 P4 S3 P2 [Link] [Link]
S4 P4 S4 P4 S4 P2 S1 S2
- A/B
S4 P4 S2 S3
[Link]
A/B = Π X(A)- all Disqualified tuples S3
S1
A/B = Π X(A)- Π X((Π X(A) X B)-A) S4
S4
22-02-2025

Assignment Operator
 The assignment operation (←) provides a convenient way to express complex queries.
 It writes query as a sequential program consisting of:
 a series of assignments
 followed by an expression whose value is displayed as a result of the query.
 Assignment must always be made to a temporary relation variable.
 Division Operator: A/B = Π X(A)- Π X((Π X(A) X B)-A)
 Write A/B as :
Temp1 ← Π X(A)
Temp2 ← Π X((Temp1 X B)-A)
Result ← Temp1-Temp2
 The result to the right of the is assigned to the relation variable on the left of the ←
 May use variable in subsequent expressions.

Relational Calculus
 Relational calculus is a non-procedural query language(or declarative language).
It uses mathematical predicate calculus(or first-order logic instead of algebra.)
 Relational calculus tells what to do but never explain how to do.
 Relational calculus provides description about the query to get the result where as Relational
algebra gives the method to get the result.
When applied to database, It comes in two flavors:
1. Tuple Relational Calculus(TRC):
 Proposed by codd in the year 1972.
 Works on tuples(or rows) like SQL.
2. Domain Relational Calculus(DRC):
 Proposed by lacroix & pirotte in the year 1972.
 Works on domain of attributes (or columns).
22-02-2025

Relational Calculus
 Calculus has variable constraints, comparison operator, logical connectives and quantifiers.
 TRC: Variables range over tuples.
 Like SQL
 DRC: Variables range over domain element.
 Like Query-By-Example(QBE)
 Expressions in the calculus are called formulas.
 Resulting tuple is an assignment of constants to variables that make the formula evaluate to
true.

1. Tuple Relational Calculus (TRC)


 Tuple relational calculus is a non-procedural query language.
 Tuple relational calculus is used for selecting the tuples in a relation that satisfy the given
condition(or predicate). The result of the relation can have one or more tuples.
 A query in TRC expressed as:
{ t| P(t)}
Where t denotes resulting tuple and P(t) denotes predicate(or condition) used to fetch tuple t.
| filter sign
 Result of Query: It is the set of all tuples t such that predicate P is true for t.
Notation used:
 t is a tuple variable.
 t[A] denotes the value of tuple t on attribute A.
 t E r denotes that tuple t is in relation r.
 P is a formula similar to that of the predicate calculus.
22-02-2025

Predicate Calculus Formula


1. Set of attributes and constants
2. Set of comparison operators: e.g., <, ≤, =, ≠, >, ≥
3. Set of connectives: and(^), or (v), not(¬)
4. Implication (⟹): x ⟹ y, if x is true, then y is true. x ⟹ y ≡¬ x ∨ y
5. Quantifiers: Existential Quantifiers(∃) and Universal Quantifier(∀)
 ∃ t ∈ r(Q(t)) ≡ “there exists” a tuple in t in relation r such that predicate Q(t) is true.
 ∀t ∈ r(Q(t))≡ Q is true “for all” tuples t in relation r.
Free and Bound variables:
 The use of quantifiers ∃ X and ∀X in a formula is said to bind X in the formula .
 A variable that is not bound is free.
There is an important restriction
 The variable t that appears to the left of ‘|’ must be the only free variable in the formula P(t).
 In other words, all other tuple variables must be bound using a quantifiers.

Example Queries : TRC


 Find the loan-number, branch-name and amount for all loans of over $1200.
{t | t ∈ loan ∧ t [amount] > 1200} (Selection)

It selects all the tuples t from relation loan such that the resulting loan tuples will have amount greater than $1200

 Find the loan number for each loan of an amount greater than $1200
{t | ∃ s ∈ loan (t[loan-number] = s[loan-number] ∧ s[amount]> 1200)} (Projection)

It selects the set of tuples t such that there exists a tuple s in relation loan for which the values of t & s for the
loan – number attribute are equal and the value of s for the amount attribute is greater than $1200.
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)
22-02-2025

Example Queries : TRC….


 Find the names of all customers having a loan at the Perryridge branch
{ t | ∃s ∈ borrower(t [customer-name] = s[customer-name]
∧ ∃u ∈ loan ( u(branch-name] = “Perryridge ”
∧ u[loan-number] = s[loan-number]))}
 Find the names of all customer having a loan, an account or both at the bank
{t | ∃s ∈ borrower (t[customer-name] = s[customer-name])
∨ ∃u ∈ depositor(t[customer-name] = u[customer-name])
 Find the names of all customer who have a loan and an account at the bank
{ t | ∃s ∈ borrower (t[customer-name] = s[customer-name])
∧ ∃u ∈ depositor (t[customer-name] = u[customer-name]

2. Domain Relational Calculus (DRC)


 Domain Relational Calculus is a non-procedural query language.
 In Domain Relational Calculus the records are filtered based on the domains.
 DRC uses list of attributes to be selected from relation based on the condition(or predicate).
 DRC is same as TRC but differs by selecting the attributes rather than selecting whole tuples.
 In DRC, each query is an expression of the form:
{< a1, a2,………., an > | P(a1, a2,………,an)}
a1, a2,………., an represent domain variables
P represents a predicate similar to that of the predicate calculus
Result of Query: It is the set of all tuples a1, a2,………., an such that predicate P is true for a1,
a2,………., an tuples.
22-02-2025

Predicate Calculus Formula


 Notations used:
 <a1, a2,………., an > ∈ r is relation on n attribute and a1, a2,………., an are domain
variables or domain constants.
 P is a formula similar to that of the predicate calculus.
 FORMULA:
1. Set of domain variables and constants:
2. Set of comparison operators: e.g., <, ≤, =, ≠, >, ≥
3. Set of connectives: and(^), or (v), not(¬)
4. Implication (⟹): x ⟹ y, if x is true, then y is true. x⟹y≡¬x∨ y
5. Quantifiers: Existential Quantifiers (∃) and Universal Quantifiers (∀)
∃x(P(x)) and ∀ x (P(x)) x is Free domain variable.

Example Queries: DRC


 Find the loan-number, branch-name and amount for loans of over $1200:

{ <l, b, a> | <l, b, a> ∈ loan ∧ a > 1200} (Selection)

 Find the loan number for each loan of an amount greater than $1200:

{ <l> | ∃ b, a (<l, b, a> ∈ loan ∧ a > 1200)} (Selection then Projection)


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

Example Queries: DRC….


 Find the names of all customers who have a loan of over $1200:
{<c> | ∃ l, b, a (<c, l> borrower (Join)
∧ (<l, b, a> ∈ loan ∧ a > 1200))}
 Find the names of all customers having a loan at the Perryridge branch and find the loan
amount:
{<c, a> | ∃ l ( <c, l> ∈ borrower
∧ ∃ 𝐛 (<l, b, a> ∈ loan ∧ b = “Perryridge”))} (Join)
branch(branch-name, branch-city, assets)
customer(customer-name, customer-street, customer-city)
account(account-number, branch-name, amount)
loan(loan-number, branch-name, amount)
depositor(customer-name, account-number)
borrower(customer-name, loan-number)

You might also like