Lecture 3 - Relational Algebra-Full

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Relational Algebra

Preview
• Relational Algebra
• SELECT(σ)
• Projection(π)
• Rename (ρ)
• Union operation (υ)
• Set Difference (-)
• Intersection
• Cartesian product(X)
• Join Operations
• Inner Join
• Theta Join
• EQUI join
Relational Algebra
 RELATIONAL ALGEBRA is a widely used procedural query
language. It collects instances of relations as input and gives
occurrences of relations as output. It uses various operations
to perform this action. SQL Relational algebra query
operations are performed recursively on a relation. The output
of these operations is a new relation, which might be formed
from one or more input relations.
Relational Operations
• Unary Relational Operations
• SELECT (symbol: σ)
• PROJECT (symbol: π)
• RENAME (symbol: ρ)

• Relational Algebra Operations From Set Theory


• UNION (υ)
• INTERSECTION ( ),
• DIFFERENCE (-)
• CARTESIAN PRODUCT ( x )
Relational Operations
• Binary Relational Operations
• JOIN
• DIVISION
Select (σ)
The SELECT operation is used for selecting a subset of the tuples
according to a given selection condition. Sigma(σ)Symbol denotes it.
It is used as an expression to choose tuples which meet the selection
condition. Select operator selects tuples that satisfy a given
predicate.
σp (r)
σ is the predicate
r stands for relation which is the name of the table
p is prepositional logic
Select (σ)
σ topic = "Database" ( Tutorials)
Selects tuples from Tutorials where topic = ‘Database’.

σ topic = "Database" and author = “Chaplin"( Tutorials)


Selects tuples from Tutorials where the topic is ‘Database’ and ‘author’ is
Chaplin

σ sales > 50000 (Customers)


Selects tuples from Customers where sales is greater than 50000
Projection(π)
• The projection eliminates all attributes of the input relation but
those mentioned in the projection list. The projection method
defines a relation that contains a vertical subset of Relation.
• This helps to extract the values of specified attributes. (pi) symbol is
used to choose attributes from a relation. This operator helps you
to keep specific columns from a relation and discards the other
columns.
• Also, all the duplicate tuples are removed from the relation in the
resulting relation. This is called as Duplicate elimination.
Projection(π) - Example
Customers Output
CustomerID CustomerName Status CustomerName Status
1 Google Active Google Active
2 Amazon Active Amazon Active
3 Apple Inactive Apple Inactive
4 Alibaba Active Alibaba Active

Π CustomerName, Status (Customers)


Projection(π) - Example
Faculty Output
Class Dept Position Class Dept
5 CSE Assistant Professor 5 CSE
5 CSE Assistant Professor 6 EE
6 EE Assistant Professor
6 EE Assistant Professor

Π Class, Dept (Faculty)


Rename (ρ)
• The RENAME operation is used to rename the relation, attribute or both.
• 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 –
• We may want to save the result of a relational algebra expression as a
relation so that we can use it later.
• We may want to join 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.
• ρ X (R) - Where the symbol ‘ρ’ is used to denote the RENAME operator and
R is the result of the sequence of operation or expression which is saved
with the name X.
Rename (ρ) - Examples
• Rename a relation
• Suppose we have a relation named Students and we want to change it
to FinalYrStudents, the rename operation works as follows:
ρFinalYrStudents​(Students)
• Rename an attribute
• Suppose we have a relation named Students and we want to change its
attributes StudentID, StudentName to SID and SName, the rename operation works as
follows:
ρ(SID,SName)​(Students)
The attributes will be ordered as same as the tables and must be renamed in
the same order. If only selective attributes are to be changed, rewrite the
original attribute names of those that are supposed to be unchanged.
Rename (ρ) - Examples
• Rename both
• Next, we'll change both the relation name and attributes of the Students class:

ρFinalYrStudents(SID,SName)​(Students)

Once relations and attributes are renamed, they can be


used in the projection and selection operations to access
the relations.
Union Operation (υ)
• UNION is symbolized by ∪ symbol. It includes all tuples that are in tables A or
in B. It also eliminates duplicate tuples. So, set A UNION set B would be
expressed as:

A∪B
• For a union operation to be valid, the following conditions must hold –

• R and S must be the same number of attributes.


• Attribute domains need to be compatible.
• Duplicate tuples should be automatically removed.
Union Operation (υ) - Examples
Consider the following tables.
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3

Table A ∪ B
column 1 column 2
1 1
1 2
1 3
Set Difference (-)
– Symbol denotes it. The result of A – B, is a relation which includes all tuples
that are in A but not in B.

 The attribute name of A has to match with the attribute name in B.


 The two-operand relations A and B should be either compatible or Union compatible.
 It should be defined relation consisting of the tuples that are in relation A, but not in B.

Table A – B

column 1 column 2

1 2
Intersection
An intersection is defined by the symbol ∩
A∩B

Defines a relation consisting of a set of all tuple that are in both A and B.
However, A and B must be union-compatible.

Table A ∩ B

column 1 column 2

1 1
Cartesian Product(X)
Cartesian Product in DBMS is an operation used to merge columns from two relations.
Generally, a cartesian product is never a meaningful operation when it performs alone.
However, it becomes meaningful when it is followed by other operations. It is also called
Cross Product or Cross Join.
Example – Cartesian product
σ column 2 = ‘1’ (A X B)
Output – The above example shows all rows from relation A and B whose column 2 has
value 1

σ column 2 = ‘1’ (A X B)
column 1 column 2
1 1
1 1
END OF PORTION FOR CT - I
Join Operations
A Join operation combines related tuples from different
relations, if and only if a given join condition is satisfied. It is
denoted by ⋈.
EMPLOYEE SALARY Operation:
(EMPLOYEE ⋈ SALARY)
EMP_CODE EMP_NAME EMP_CODE SALARY EMP_CODE EMP_NAME SALARY

101 Stephan 101 50000 101 Stephan 50000

102 Jack 102 30000 102 Jack 30000

103 Harry 103 25000 103 Harry 25000


Types of Joins
JOIN

INNER JOIN OUTER JOIN

LEFT RIGHT FULL


THETA NATURAL EQUI JOIN OUTER OUTER OUTER
JOIN JOIN JOIN JOIN JOIN
Natural Join
•A natural join is the set of tuples of all combinations in R and S that
are equal on their common attribute names.
•It is denoted by ⋈. NO OPERATOR IS REQUIRED.
∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
EMPLOYEE SALARY Output
EMP_CODE EMP_NAME EMP_CODE SALARY EMP_NAME SALARY

101 Stephan 101 50000 Stephan 50000

102 Jack 102 30000 Jack 30000

103 Harry 103 25000 Harry 25000


Equi Join
•Equi join is same as Theta Join, but the only condition is it only uses
equivalence condition while performing join between two tables.
ONLY EQUIVALENT OPERATOR CAN BE USED.

∏CLASS_ID,NAME,PRODUCT_ID, CITY (CUSTOMER ⋈ (CLASS_ID=PRODUCT_ID)PRODUCT)

CUSTOMER PRODUCT Output


CLASS_ID NAME PRODUCT_ID CITY CLASS_ID NAME PRODUCT_ID CITY

1 John 1 DHAKA
1 John 1 DHAKA
2 Harry 2 DELHI 2 Harry 2 DELHI
3 Jackson 3 NEW YORK 3 Harry 3 NEW
YORK
Outer Join
Outer Join in Relational algebra returns all the attributes of both the table
depending on the condition. If some attribute value is not present for any one
of the tables it returns NULL in the respective row of the table attribute.
EMPLOYEE WORKERS Output
EMP_NAME STREET CITY EMP_NAME BRANCH SALARY EMP_NAME STREET CITY BRANCH SALAR
Y

Ram Civil line Mumbai Ram Infosys 10000 Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata


Shyam Wipro 20000

Ravi M.G. Street Delhi Shyam Park street Kolkata Wipro 20000
Kuber HCL 30000

Hari Nehru nagar Hyderabad


Hari TCS 50000 Hari Nehru nagar Hyderabad TCS 50000
Left Outer Join - EMPLOYEE ⟕ WORKERS
It returns all the rows of the left table even if there is no matching row for it in the right table
performing Left Outer Join.
It is denoted by ⟕.

EMPLOYEE WORKERS Output


EMP_NAME STREET CITY EMP_NAME BRANCH SALARY EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000


Ram Civil line Mumbai Ram Infosys 10000
Shyam Park street Kolkata Wipro 20000
Shyam Park street Kolkata
Shyam Wipro 20000

Ravi M.G. Street Delhi Hari Nehru Hyderabad TCS 50000


street
Kuber HCL 30000

Hari Nehru nagar Hyderabad Ravi M.G. Delhi NULL NULL


Hari TCS 50000
Street
Right Outer Join - EMPLOYEE ⟖ WORKERS
It returns all the rows of the second table even if there is no matching row for it in the first
table performing Right Outer Join.
It is denoted by ⟖.

EMPLOYEE WORKERS Output


EMP_NAME STREET CITY EMP_NAME BRANCH SALARY EMP_NAME BRANCH SALARY STREET CITY

Ram Infosys 10000 Civil line Mumbai


Ram Civil line Mumbai Ram Infosys 10000

Shyam Park street Kolkata Shyam Wipro 20000 Park street Kolkata
Shyam Wipro 20000

Ravi M.G. Street Delhi


Kuber HCL 30000
Hari TCS 50000 Nehru street Hyderabad

Hari Nehru nagar Hyderabad


Hari TCS 50000
Kuber HCL 30000 NULL NULL
Full Outer Join - EMPLOYEE ⟗ WORKERS
Full outer join is like a left or right join except that it contains all rows from both tables.

It is denoted by ⟗.

EMPLOYEE WORKERS Output


EMP_NAME STREET CITY EMP_NAME BRANCH SALARY EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000


Ram Civil line Mumbai Ram Infosys 10000
Shyam Park street Kolkata Wipro 20000
Shyam Park street Kolkata
Shyam Wipro 20000
Hari Nehru Hyderabad TCS 50000
Ravi M.G. Street Delhi street
Kuber HCL 30000
Ravi M.G. Street Delhi NULL NULL

Hari Nehru nagar Hyderabad


Hari TCS 50000 Kuber NULL NULL HCL 30000

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy