You are on page 1of 23

Relational Algebra

Chittaranjan Pradhan

Database Management Query Language

Relational Algebra

System 11 SELECT Operator(σ)

PROJECT

Relational Algebra Operator(π)


Composition of Relational
Operators

RENAME Operator(ρ)

Union Compatibility

UNION Operator(∪)

DIFFERENCE
Operator(-)

Cartesian Product
Operator(×)

Intersection
Operator(∩)

JOIN Operator(o
n)

Division Operator(÷)

Assignment
Operator(←)

Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
11.1
Relational Algebra
Query Language
Chittaranjan Pradhan

Query Language

Relational Algebra
Language in which user requests information from the SELECT Operator(σ)
database are: PROJECT
Operator(π)
• Procedural language Composition of Relational
Operators

• Nonprocedural language RENAME Operator(ρ)

Union Compatibility

UNION Operator(∪)

The categories of different languages are: DIFFERENCE


Operator(-)
• SQL Cartesian Product
Operator(×)
• Relational Algebra Intersection
Operator(∩)
• Relational Calculus
JOIN Operator(o
n)
• Tuple Relational Calculus
Division Operator(÷)
• Domain Relational Calculus
Assignment
Operator(←)

11.2
Relational Algebra
Relational Algebra
Chittaranjan Pradhan

Query Language

Relational Algebra
Relational Algebra
SELECT Operator(σ)
Relational algebra is a procedural language for manipulating PROJECT
Operator(π)
relations. Relational algebra operations manipulate relations. Composition of Relational
Operators
That is, these operations use one or two existing relations to
RENAME Operator(ρ)
create a new relation Union Compatibility

UNION Operator(∪)
• Fundamental operators DIFFERENCE
Operator(-)
• Unary: SELECT, PROJECT, RENAME
Cartesian Product
• Binary: UNION, SET DIFFERENCE, CARTESIAN Operator(×)
PRODUCT Intersection
Operator(∩)

JOIN Operator(o
n)
• Secondary operators Division Operator(÷)
• INTERSECTION, NATURAL JOIN, DIVISION, and Assignment
Operator(←)
ASSIGNMENT

11.3
Relational Algebra
Relational Algebra...
Chittaranjan Pradhan

Query Language
The relational schemas used for different operations are: Relational Algebra

SELECT Operator(σ)
• Customer(cust_name, cust_street, cust_city)
PROJECT
• used to store customer details Operator(π)
Composition of Relational
• Branch(branch_name, branch_city, assets) Operators

RENAME Operator(ρ)
• used to store branch details
Union Compatibility
• Account(acc_no, branch_name, balance) UNION Operator(∪)
• stores the account details DIFFERENCE
Operator(-)
• Loan(loan_no, branch_name, amount) Cartesian Product
Operator(×)
• stores the loan details
Intersection
• Depositor(cust_name, acc_no) Operator(∩)

JOIN Operator(o
n)
• stores the details about the customers’ account
Division Operator(÷)
• Borrower(cust_name, loan_no)
Assignment
• used to store the details about the customers’ loan Operator(←)

11.4
Relational Algebra
SELECT Operator(σ)
Chittaranjan Pradhan

SELECT Operator(σ)
Query Language
SELECT operation is used to create a relation from another
Relational Algebra
relation by selecting only those tuples or rows from the original SELECT Operator(σ)
relation that satisfy a specified condition. It is denoted by sigma PROJECT
(σ) symbol. The predicate appears as a subscript to σ Operator(π)
Composition of Relational
Operators

RENAME Operator(ρ)
The argument relation is in parenthesis after the σ. The result is
Union Compatibility
a relation that has the same attributes as the relation specified UNION Operator(∪)
in <relation-name>. The general syntax of select operator is: DIFFERENCE
σ <selection−condition> (<relation name>) Operator(-)

Cartesian Product
Operator(×)
Query: Find the details of the loans taken from ’Bhubaneswar Intersection
Main’ branch. Operator(∩)

JOIN Operator(o
n)
σ branch_name=0 BhubaneswarMain0 (Loan)
Division Operator(÷)
• The operators used in selection predicate may be: =, 6=, <, Assignment
≤, >, ≥. Operator(←)

• Different predicates can be combined into a larger


V W
predicate by using the connectors like: AND( ), OR( ),
NOT(¬)
11.5
Relational Algebra
SELECT Operator(σ)...
Chittaranjan Pradhan

Query Language

Relational Algebra
Loan SELECT Operator(σ)
loan_no branch_name amount
PROJECT
L201 Bhubaneswar Main 50,000,000.00 Operator(π)
L202 Bhubaneswar Main 5,000,000.00 Composition of Relational
Operators
L203 Mumbai Main 100,000,000.00 RENAME Operator(ρ)
L204 Juhu 60,000,000.00 Union Compatibility

UNION Operator(∪)
σ branch_name=0 BhubaneswarMain0 (Loan) DIFFERENCE
loan_no branch_name amount Operator(-)

L201 Bhubaneswar Main 50,000,000.00 Cartesian Product


Operator(×)
L202 Bhubaneswar Main 5,000,000.00 Intersection
Operator(∩)

σ branch_name=0 BhubaneswarMain0 ANDamount>10,000,000 (Loan) JOIN Operator(o


n)

loan_no branch_name amount Division Operator(÷)

Assignment
L201 Bhubaneswar Main 50,000,000.00 Operator(←)

11.6
Relational Algebra
PROJECT Operator(π)
Chittaranjan Pradhan

Query Language

Relational Algebra

SELECT Operator(σ)
PROJECT Operator(π)
PROJECT
Operator(π)
PROJECT operation can be thought of as eliminating Composition of Relational

unwanted columns. It eliminates the duplicate rows. It is Operators

RENAME Operator(ρ)
denoted by pie(π) symbol. The attributes needed to be
Union Compatibility
appeared in the resultant relation appear as subscript to π. UNION Operator(∪)

DIFFERENCE
The argument relation follows in parenthesis. The general Operator(-)

Cartesian Product
syntax of project operator is: Operator(×)

π <attribute−list> (<relation name>) Intersection


Operator(∩)

JOIN Operator(o
n)
Query: Find the loan numbers and respective loan amounts.
Division Operator(÷)
π loan_no,amount (Loan) Assignment
Operator(←)

11.7
Relational Algebra
PROJECT Operator(π)...
Chittaranjan Pradhan

Query Language

Relational Algebra

SELECT Operator(σ)
Loan PROJECT
loan_no branch_name amount Operator(π)
L201 Bhubaneswar Main 50,000,000.00 Composition of Relational
Operators
L202 Bhubaneswar Main 5,000,000.00 RENAME Operator(ρ)
L203 Mumbai Main 100,000,000.00 Union Compatibility
L204 Juhu 60,000,000.00 UNION Operator(∪)

DIFFERENCE
π loan_no,amount (Loan) Operator(-)

loan_no amount Cartesian Product


Operator(×)
L201 50,000,000.00
Intersection
L202 5,000,000.00 Operator(∩)

L203 100,000,000.00 JOIN Operator(o


n)
L204 60,000,000.00 Division Operator(÷)

Assignment
Operator(←)

11.8
Relational Algebra
Composition of Relational Operators
Chittaranjan Pradhan

Composition of Relational Operators


Query Language

Relational algebra operators can be composed together into a Relational Algebra

relational algebra expression to answer the complex queries SELECT Operator(σ)

PROJECT
Operator(π)
Q:Find the name of the customers who live in Bhubaneswar Composition of Relational
Operators

RENAME Operator(ρ)
Customer
Union Compatibility
cust_name cust_street cust_city
UNION Operator(∪)
Rishi India Gate New Delhi
DIFFERENCE
Sarthak M. G. Road Bangalore Operator(-)

Manas Shastri Nagar Bhubaneswar Cartesian Product


Operator(×)
Ramesh M. G. Road Bhubaneswar
Intersection
Mahesh Juhu Mumbai Operator(∩)

JOIN Operator(o
n)

π cust_name (σ cust_city =0 Bhubaneswar 0 (Customer)) Division Operator(÷)

Assignment
Operator(←)
cust_name
Manas
Ramesh

11.9
Relational Algebra
RENAME Operator(ρ)
Chittaranjan Pradhan

RENAME Operator(ρ) Query Language

Relational Algebra
The results of relational algebra expressions do not have a
SELECT Operator(σ)
name that can be used to refer them. It is useful to be able to PROJECT
give them names; the rename operator is used for this Operator(π)
Composition of Relational
purpose. It is denoted by rho(ρ) symbol. Operators

RENAME Operator(ρ)

Union Compatibility
The general syntax of rename operator is:
UNION Operator(∪)
ρ X (E)
DIFFERENCE
Operator(-)

Assume E is a relational-algebra expression with arity n. The Cartesian Product


Operator(×)
second form of rename operation is: ρ X (b1 ,b2 ,...bn ) (E) Intersection
Operator(∩)

JOIN Operator(o
n)
π cust_name (σ cust_city =0 Bhubaneswar 0 (Customer)) can be written as:
Division Operator(÷)
1. ρ Customer _Bhubaneswar (σ cust_city =0 Bhubaneswar 0 (Customer)) Assignment
Operator(←)

2. π cust_name (Customer_Bhubaneswar)

11.10
Relational Algebra
RENAME Operator(ρ)...
Chittaranjan Pradhan

Query Language

Relational Algebra
The different forms of the rename operation for renaming the SELECT Operator(σ)
relation are: PROJECT
Operator(π)
Composition of Relational

a. ρ S (R) Operators

RENAME Operator(ρ)

Union Compatibility
b. ρ S(b1 ,b2 ,...bn ) (R) UNION Operator(∪)

DIFFERENCE
c. ρ (b1 ,b2 ,...bn ) (R) Operator(-)

Cartesian Product
Operator(×)

For example, the attributes of Customer (cust_name, Intersection


Operator(∩)
cust_street, cust_city) can be renamed as:
JOIN Operator(o
n)

Division Operator(÷)
ρ (name,street,city ) (Customer) Assignment
Operator(←)

11.11
Relational Algebra
Union Compatibility
Chittaranjan Pradhan

Query Language

Relational Algebra

SELECT Operator(σ)
Union Compatibility PROJECT
Operator(π)
To perform the set operations such as UNION, DIFFERENCE Composition of Relational
Operators
and INTERSECTION, the relations need to be union RENAME Operator(ρ)
compatible for the result to be a valid relation Union Compatibility

UNION Operator(∪)

Two relations R1 (a1 ,a2 ,... an ) and R2 (b1 ,b2 ,... bm ) are union DIFFERENCE
Operator(-)
compatible iff: Cartesian Product
Operator(×)

Intersection
• n = m, i.e. both relations have same arity Operator(∩)

• dom(ai ) = dom(bi ) for 1 ≤ i ≤ n JOIN Operator(o


n)

Division Operator(÷)

Assignment
Operator(←)

11.12
Relational Algebra
UNION Operator(∪)
Chittaranjan Pradhan
UNION Operator(∪)
The union operation is used to combine data from two Query Language

relations. It is denoted by union(∪) symbol. The union of two Relational Algebra

SELECT Operator(σ)
relations R1 (a1 ,a2 ,... an ) and R2 (b1 ,b2 ,... bn ) is a relation R3
PROJECT
(c1 ,c2 ,... cn ) such that: Operator(π)

dom(ci ) = dom(ai ) ∪ dom(bi ), 1 ≤ i ≤ n Composition of Relational


Operators

R1 ∪ R2 is a relation that includes all tuples that are either RENAME Operator(ρ)

present in R1 or R2 or in both without duplicate tuples Union Compatibility

UNION Operator(∪)

DIFFERENCE
Depositor Borrower Operator(-)
cust_name acc_no cust_name loan_no Cartesian Product
Manas A101 Ramesh L201 Operator(×)

Ramesh A102 Ramesh L202 Intersection


Operator(∩)
Rishi A103 Mahesh L203
Mahesh A104 Rishi L204 JOIN Operator(o
n)

Mahesh A105 Division Operator(÷)

Assignment
π cust_name (Depositor) ∪ π cust_name (Borrower) Operator(←)

cust_name
Manas
Ramesh
Rishi
Mahesh
11.13
Relational Algebra
DIFFERENCE Operator(-)
Chittaranjan Pradhan

DIFFERENCE Operator(-)
Query Language
The difference operation is used to identify the rows that are in
Relational Algebra
one relation and not in another. It is denoted as (-) symbol. The SELECT Operator(σ)
difference of two relations R1 (a1 ,a2 ,... an ) and R2 (b1 ,b2 ,... bn ) is PROJECT
a relation R3 (c1 ,c2 ,... cn ) such that: Operator(π)
Composition of Relational
dom(ci ) = dom(ai ) - dom(bi ), 1 ≤ i ≤ n Operators

RENAME Operator(ρ)
R1 - R2 is a relation that includes all tuples that are in R1 , but
Union Compatibility
not in R2 UNION Operator(∪)

DIFFERENCE
Operator(-)
Depositor Borrower Cartesian Product
cust_name acc_no cust_name loan_no Operator(×)
Manas A101 Ramesh L201 Intersection
Ramesh A102 Ramesh L202 Operator(∩)

Rishi A103 Mahesh L203 JOIN Operator(o


n)
Mahesh A104 Rishi L204 Division Operator(÷)
Mahesh A105 Assignment
Operator(←)

π cust_name (Depositor) - π cust_name (Borrower)


cust_name
Manas
11.14
Relational Algebra
Cartesian Product Operator(×)
Chittaranjan Pradhan

Query Language

Relational Algebra
Cartesian Product Operator(×) SELECT Operator(σ)

The Cartesian product of two relations R1 (a1 ,a2 ,... an ) with PROJECT
Operator(π)
cardinality i and R2 (b1 ,b2 ,... bm ) with cardinality j is a relation Composition of Relational
Operators
R3 with RENAME Operator(ρ)

• degree k = n + m, Union Compatibility

UNION Operator(∪)
• cardinality i*j and
DIFFERENCE
Operator(-)
• attributes (a1 ,a2 ,... an , b1 ,b2 ,... bm ))
Cartesian Product
R1 × R2 is a relation that includes all the possible combinations Operator(×)

Intersection
of tuples from R1 and R2 . The Cartesian product is used to Operator(∩)
combine information from any two relations JOIN Operator(o
n)

It is not a useful operation by itself; but is used in conjuction Division Operator(÷)

with other operations Assignment


Operator(←)

11.15
Relational Algebra
Cartesian Product Operator(×)...
Chittaranjan Pradhan

Borrower Loan
cust_name loan_no loan_no branch_name amount Query Language

Ramesh L201 L201 Bhubaneswar Main 50,000,000.00 Relational Algebra

Ramesh L202 L202 Bhubaneswar Main 5,000,000.00 SELECT Operator(σ)

Mahesh L203 L203 Mumbai Main 100,000,000.00 PROJECT


Operator(π)
Rishi L204 L204 Juhu 60,000,000.00 Composition of Relational
Operators

Borrower × Loan RENAME Operator(ρ)

cust_name Borrower.loan_no Loan.loan_no branch_name amount Union Compatibility


Ramesh L201 L201 Bhubaneswar Main 50,000,000.00 UNION Operator(∪)
Ramesh L201 L202 Bhubaneswar Main 5,000,000.00
DIFFERENCE
Ramesh L201 L203 Mumbai Main 100,000,000.00 Operator(-)
Ramesh L201 L204 Juhu 60,000,000.00
Cartesian Product
Ramesh L202 L201 Bhubaneswar Main 50,000,000.00 Operator(×)
Ramesh L202 L202 Bhubaneswar Main 5,000,000.00
Intersection
Ramesh L202 L203 Mumbai Main 100,000,000.00 Operator(∩)
Ramesh L202 L204 Juhu 60,000,000.00
JOIN Operator(o
n)
Mahesh L203 L201 Bhubaneswar Main 50,000,000.00
Mahesh L203 L202 Bhubaneswar Main 5,000,000.00 Division Operator(÷)
Mahesh L203 L203 Mumbai Main 100,000,000.00 Assignment
Mahesh L203 L204 Juhu 60,000,000.00 Operator(←)
Rishi L204 L201 Bhubaneswar Main 50,000,000.00
Rishi L204 L202 Bhubaneswar Main 5,000,000.00
Rishi L204 L203 Mumbai Main 100,000,000.00
Rishi L204 L204 Juhu 60,000,000.00

11.16
Relational Algebra
Cartesian Product Operator(×)...
Chittaranjan Pradhan

Query Language

Relational Algebra

SELECT Operator(σ)

PROJECT
Query: Find out the customer and their loan details taken from Operator(π)
Composition of Relational
Bhubaneswar Main branch. Operators

RENAME Operator(ρ)
Ans: σ branch_name=0 BhubaneswarMain0 ANDBorrower .loan_no=Loan.loan_no
Union Compatibility
(Borrower ×Loan)
UNION Operator(∪)

DIFFERENCE
Operator(-)
cust_name Borrower.loan_no Loan.loan_no branch_name amount
Cartesian Product
Ramesh L201 L201 Bhubaneswar Main 50,000,000.00 Operator(×)
Ramesh L202 L202 Bhubaneswar Main 5,000,000.00
Intersection
Operator(∩)

JOIN Operator(o
n)

Division Operator(÷)

Assignment
Operator(←)

11.17
Relational Algebra
Intersection Operator(∩)
Chittaranjan Pradhan
Intersection Operator(∩)
The intersection operation is used to identify the rows that are Query Language

common to two relations. It is denoted by (∩) symbol. The Relational Algebra

SELECT Operator(σ)
intersection of two relations R1 (a1 ,a2 ,... an ) and R2 (b1 ,b2 ,... bn )
PROJECT
is a relation R3 (c1 ,c2 ,... cn ) such that: Operator(π)

dom(ci ) = dom(ai ) ∩ dom(bi ), 1 ≤ i ≤ n Composition of Relational


Operators

R1 ∩ R2 is a relation that includes all tuples that are present in RENAME Operator(ρ)

both R1 and R2 Union Compatibility

The intersection operation can be rewritten by a pair of set UNION Operator(∪)

DIFFERENCE
difference operations as R ∩ S = R - (R - S) Operator(-)

Cartesian Product
Operator(×)
Depositor Borrower
Intersection
cust_name acc_no cust_name loan_no Operator(∩)
Manas A101 Ramesh L201 JOIN Operator(o
n)
Ramesh A102 Ramesh L202
Division Operator(÷)
Rishi A103 Mahesh L203
Assignment
Mahesh A104 Rishi L204 Operator(←)
Mahesh A105

π cust_name (Depositor) ∩ π cust_name (Borrower)


cust_name
Ramesh
Mahesh
11.18
Rishi
Relational Algebra
JOIN Operator(o
n)
Chittaranjan Pradhan

JOIN Operator(o
n)
Query Language
The join is a binary operation that is used to combine certain Relational Algebra
selections and a Cartesian product into one operation. It is SELECT Operator(σ)

denoted by join (on) symbol. PROJECT


Operator(π)
Composition of Relational
Operators
The join operation forms a Cartesian product of its two RENAME Operator(ρ)
arguments, performs a selection forcing equality on those Union Compatibility
attributes that appear in both relations, and finally removes the UNION Operator(∪)

duplicate attributes DIFFERENCE


Operator(-)

Cartesian Product
Query: Find the names of customers who have a loan at the Operator(×)

bank, along with the loan number and the loan amount. Intersection
Operator(∩)
Ans:This query can be solved by using the PROJECT,
JOIN Operator(o
n)
SELECT and CARTESIAN PRODUCT operators as: Division Operator(÷)
π cust_name,Loan.loan_no,amount (σ Borrower .loan_no=Loan.loan_no Assignment
(Borrower ×Loan)) Operator(←)

This same expression can be simplified by using the JOIN as:


π cust_name,loan_no,amount (Borrower o
n Loan))
11.19
Relational Algebra
JOIN Operator(o
n)...
Chittaranjan Pradhan

Query Language

Relational Algebra

SELECT Operator(σ)
Borrower Loan PROJECT
cust_name loan_no loan_no branch_name amount Operator(π)
Ramesh L201 L201 Bhubaneswar Main 50,000,000.00 Composition of Relational
Operators
Ramesh L202 L202 Bhubaneswar Main 5,000,000.00
RENAME Operator(ρ)
Mahesh L203 L203 Mumbai Main 100,000,000.00
Union Compatibility
Rishi L204 L204 Juhu 60,000,000.00
UNION Operator(∪)

DIFFERENCE
π cust_name,loan_no,amount (Borrower o
n Loan)) Operator(-)

cust_name loan_no amount Cartesian Product


Operator(×)
Ramesh L201 50,000,000.00
Intersection
Ramesh L202 5,000,000.00 Operator(∩)
Mahesh L203 100,000,000.00 JOIN Operator(o
n)
Rishi L204 60,000,000.00 Division Operator(÷)

Assignment
Operator(←)

11.20
Relational Algebra
Division Operator(÷)
Chittaranjan Pradhan

Division Operator(÷)
Query Language
The division operation creates a new relation by selecting the Relational Algebra
rows in one relation that match every row in another relation. SELECT Operator(σ)
The division operation requires that we look at an entire PROJECT
Operator(π)
relation at once. It is denoted by division (÷) symbol Composition of Relational
Operators

RENAME Operator(ρ)
Let A, B, C are three relations and we desire B ÷ C to give A
Union Compatibility
as the result. This operation is possible iff: UNION Operator(∪)

• The columns of C must be a subset of the columns of B. DIFFERENCE


Operator(-)
The columns of A are all and only those columns of B that Cartesian Product
are not columns of C Operator(×)

Intersection
• A row is placed in A if and only if it is associated with B Operator(∩)

and with every row of C JOIN Operator(o


n)

Division Operator(÷)
The division operation is the reverse of the Cartesian product Assignment
operation as: B = (B × C) ÷ C Operator(←)

Division operator is suited to queries that include the phrase


every or all as part of the condition
11.21
Relational Algebra
Division Operator(÷)...
Chittaranjan Pradhan

Depositor Account
cust_name acc_no acc_no branch_name balance Query Language

Manas A101 A101 Bhubaneswar Main 100,000.00 Relational Algebra

Ramesh A102 A102 Shastri Nagar 50,000.00 SELECT Operator(σ)

Rishi A103 A103 India Gate 5,000,000.00 PROJECT


Operator(π)
Mahesh A104 A104 Juhu 600,000.00 Composition of Relational
Mahesh A105 A105 Mumbai Main 10,000,000.00 Operators

RENAME Operator(ρ)

branch_name branch_city assets Union Compatibility

Bhubaneswar Main Bhubaneswar Gold UNION Operator(∪)


Shastri Nagar Bhubaneswar Mines
Branch India Gate New Delhi Gold
DIFFERENCE
Operator(-)
Juhu Mumbai Sea Shore Cartesian Product
Mumbai Main Mumbai Movie Operator(×)

Intersection
Operator(∩)
Query: Find all the customers who have an account at all the
JOIN Operator(o
n)
branches located in Mumbai Division Operator(÷)

Assignment
π cust_name,branch_name (Depositor o n Account) ÷ π branch_name (σ Operator(←)

branch_city =0 Mumbai 0 (Branch))


cust_name
Mahesh
11.22
Relational Algebra
Assignment Operator(←)
Chittaranjan Pradhan

Assignment Operator(←)
Query Language
It works like assignment in a programming language. In
Relational Algebra
relational algebra, the assignment operator gives a name to a SELECT Operator(σ)
relation. It is denoted by (←) symbol PROJECT
Assignment must always be made to a temporary relation Operator(π)
Composition of Relational

variable. The result of the right of the ← symbol is assigned to Operators

RENAME Operator(ρ)
the relation variable on the left of the ← symbol
Union Compatibility
With the assignment operator, a query can be written as a UNION Operator(∪)
sequential program consisting of: DIFFERENCE
Operator(-)
• a series of assignment,
Cartesian Product
Operator(×)
• followed by an expression whose value is displayed as a
Intersection
result of the query Operator(∩)

JOIN Operator(o
n)

π n Account) ÷ π branch_name (σ
cust_name,branch_name (Depositor o
Division Operator(÷)

0
branch_city = Mumbai 0 (Branch)) Assignment
Operator(←)
can be simplified as:
Temp1 ← π cust_name,branch_name (Depositor o n Account)
Temp2 ← π branch_name (σ branch_city =0 Mumbai 0 (Branch))
Result = Temp1 ÷ Temp2 11.23

You might also like