0% found this document useful (0 votes)
24 views

Relational Algebra in DBMS

Uploaded by

rkmaharshi1108
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Relational Algebra in DBMS

Uploaded by

rkmaharshi1108
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Subject Name: Database Management System

Subject Code:

Relational Algebra in DBMS


Points to be Discussed..
In this video lecture we will learn

Relational Algebra Join Operations


Select (ς) Inner:
Projection (π) Theta (θ), EQUI
Rename (ρ) Natural Join (⋈)
Union Operation (U) Outer Join
Set Difference (-) Left Outer Join (⟕)
Intersection (∩ ) Right Outer Join (⟖)
Cartesian Product (X) Full Outer 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.
Basic SQL Relational Algebra Operations
Relational Algebra divided in various groups
Unary Relational Operations
SELECT (symbol: ς)
PROJECT (symbol: π)
RENAME (symbol: ρ)
Relational Algebra Operations From Set Theory
UNION (υ)
INTERSECTION (∩)
DIFFERENCE (-)
CARTESIAN PRODUCT ( x )
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 (ς): Example
Example 1:
ς topic = "Database" (Tutorials)
Output - Selects tuples from Tutorials where topic = 'Database'.

Example 2:
ς topic = "Database" and author = "guru99"( Tutorials)
Output - Selects tuples from Tutorials where the topic is 'Database' and 'author' is guru99.

Example 3:
ς sales > 50000 (Customers)
Output - 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 to eliminates duplicate


values. (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.
Example of Projection:

Consider the following table (Customers)


CustomerID CustomerName Status
1 Google Active
2 Amazon Active
3 Apple Inactive
4 Alibaba Active
Example of Projection:
Here, the projection of CustomerName and status will give

π CustomerName,Status (Customers)

Output Table: CustomerName Status


Google Active
Amazon Active
Apple Inactive
Alibaba Active
Rename (ρ):

Rename is a unary operation used for renaming attributes of a


relation.
ρ (a/b)R will rename the attribute 'b' of relation by 'a'.
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: The result <- 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 (υ): Example
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
A ∪ B gives:
column 1 column 2
1 1
1 2
1 3
Intersection (∩)

An intersection is defined by the symbol ∩: A ∩ B


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

Visual Definition of Intersection


Intersection (∩): Example
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3

Example: A ∩ B
Table A ∩ B
column 1 column 2
1 1
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.
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.
Set Difference (-): Example
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3

Example: A - B
Table A - B
column 1 column 2
1 2
Cartesian Product(✕)
We already are aware of the fact that relations are nothing but a set of tuples,
and here we will have 2 sets of tuples.
On applying CARTESIAN PRODUCT on two relations that is on two sets of tuples, it will take
every tuple one by one from the left set(relation) and will pair it up with all the tuples in the right
set(relation).
So, the CROSS PRODUCT of two relation A(R1, R2, R3, …, Rp) with degree p, and B(S1, S2, S3, …,
Sn) with degree n, is a relation C(R1, R2, R3, …, Rp, S1, S2, S3, …, Sn) with degree p + n attributes.
CROSS PRODUCT is a binary set operation means, at a time we can apply the operation on two
relations. But the two relations on which we are performing the operations do not have the
same type of tuples, which means Union compatibility (or Type compatibility) of the two
relations is not necessary.
Cartesian Product(✕)
Notation: S ✕ R
where A and S are the relations,
the symbol ‘✕’ is used to denote the CROSS PRODUCT operator.
Example:
Consider two relations STUDENT(SNO, FNAME, LNAME) and DETAIL(ROLLNO, AGE)
below:
SNO FNAME LNAME ROLLNO AGE
1 Albert Singh 5 18
2 Nora Fatehi 9 21
Cartesian Product(✕): Example
On applying CROSS PRODUCT on STUDENT and DETAIL:
STUDENT ✕ DETAILS
SNO FNAME LNAME ROLLNO AGE
1 Albert Singh 5 18
1 Albert Singh 9 21
2 Nora Fatehi 5 18
2 Nora Fatehi 9 21
We can observe that the number of tuples in STUDENT relation is 2, and the
number of tuples in DETAIL is 2. So the number of tuples in the resulting relation
on performing CROSS PRODUCT is 2*2 = 4.
Join Operations
Join operation is essentially a Cartesian product followed by a selection criterion.
Join operation denoted by ⋈.
JOIN operation also allows joining variously related tuples from different relations.
Types of JOIN:
Various forms of join operation are:
Inner Joins:
Theta join
EQUI join
Natural join
Outer join:
Left Outer Join
Right Outer Join
Full Outer Join
Inner Join:

In an inner join, only those tuples that satisfy the matching criteria are
included, while the rest are excluded. Let's study various types of Inner Joins:
Theta Join:
The general case of JOIN operation is called a Theta join. It is denoted by
symbol θ
Example: A ⋈θ B
Theta join can use any conditions in the selection criteria.
Theta Join: Example

Example: A ⋈ A.column 2 > B.column 2 (B) Table A Table B


column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3

A ⋈ A.column 2 > B.column 2 (B)

column 1 column 2
1 2
EQUI Join:

When a theta join uses only equivalence condition, it becomes a equi


join.
For example: A ⋈ A.column 2 = B.column 2 (B) A ⋈ A.column 2 = B.column 2 (B)
column 1 column 2

1 1

EQUI join is the most difficult operations to implement efficiently using SQL in an
RDBMS and one reason why RDBMS have essential performance problems.
NATURAL JOIN (⋈)
Natural join can only be performed if there is a common attribute (column)
between the relations. The name and type of the attribute must be same.
Example :Consider the following two tables
C
Num Square
C⋈D
2 4
Num Square Cube
3 9 C⋈D
2 4 8

D 3 9 27

Num Cube
2 8
3 27
OUTER JOIN
In an outer join, along with tuples that satisfy the matching criteria,
we also include some or all tuples that do not match the criteria.
Left Outer Join (⟕ )
In the left outer join, operation allows keeping all tuple in the left relation.
However, if there is no matching tuple is found in right relation, then the attributes
of right relation in the join result are filled with null values.
LEFT OUTER JOIN: Example
Consider the following 2 Tables
A
Num Square
A⟕B
2 4
Num Square Cube
3 9
2 4 8
4 16
3 9 18

B 4 16 -

Num Cube
2 8
3 18
RIGHT OUTER JOIN (⟖ )

In the right outer join, operation allows keeping all tuple in the right
relation. However, if there is no matching tuple is found in the left
relation, then the attributes of the left relation in the join result are
filled with null values.
RIGHT OUTER JOIN: Example
Consider the following 2 Tables
A
Num Square
A ⟖B
2 4
Num Cube Sqau
3 9
2 8 4
4 16
3 18 9

B 5 75 -

Num Cube
2 8
3 18
5 75
FULL OUTER JOIN (⟗ )
In a full outer join, all tuples from both relations are included in the
result, irrespective of the matching condition.
A A⟗B
Num Square Num Cube Square
2 4 2 4 8
3 9 3 9 18
4 16 4 16 -

B 5 - 75

Num Cube
2 8
3 18
5 75
DIFFERENCE BETWEEN NATURAL JOIN & INNER JOIN
NATURAL JOIN INNER JOIN
Natural Join joins two tables based on same attribute Inner Join joins two table on the basis of the column
name and datatypes. which is explicitly specified in the ON clause.
In Natural Join, The resulting table will contain all the In Inner Join, The resulting table will contain all the
attributes of both the tables but keep only one copy of attribute of both the tables including duplicate columns
each common column also
In Natural Join, If there is no condition specifies then it In Inner Join, only those records will return which exists in
returns the rows based on the common column both the tables
SYNTAX:
SYNTAX:
SELECT *
SELECT *
FROM table1 INNER JOIN table2 ON table1.Column_Name
FROM table1 NATURAL JOIN table2;
= table2.Column_Name;
Summary
Operation(Symbols) Purpose
Select(ς) The SELECT operation is used for selecting a subset of
the tuples according to a given selection condition
Projection(π) The projection eliminates all attributes of the input
relation but those mentioned in the projection list.

Union Operation(∪) UNION is symbolized by symbol. It includes all tuples that


are in tables A or in B.
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.
Intersection(∩) Intersection defines a relation consisting of a set of all
tuple that are in both A and B.
Summary
Operation(Symbols) Purpose
Cartesian Product(X) Cartesian operation is helpful to merge columns from
two relations.
Inner Join Inner join, includes only those tuples that satisfy the
matching criteria.
Theta Join(θ) The general case of JOIN operation is called a Theta
join. It is denoted by symbol θ.
EQUI Join When a theta join uses only equivalence condition, it
becomes a equi join.
Natural Join(⋈) Natural join can only be performed if there is a
common attribute (column) between the relations.
Summary
Operation(Symbols) Purpose
Outer Join In an outer join, along with tuples that satisfy the
matching criteria.
Left Outer Join( ) In the left outer join, operation allows keeping all
tuple in the left relation.
Right Outer join() In the right outer join, operation allows keeping all
tuple in the right relation.
Full Outer Join() In a full outer join, all tuples from both relations are
included in the result irrespective of the matching
condition.
Outer Join In an outer join, along with tuples that satisfy the
matching criteria.
Thank you

You might also like