Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views8 pages

Relational Algebra

The document provides an overview of relational algebra, including operations such as selection (σ), projection (∏), union (∪), set difference (-), Cartesian product (X), and renaming (ρ). It also explains join operations, specifically inner joins, and their types: theta join, equi join, and natural join. Examples are provided for each operation using relations for students and employees to illustrate the concepts clearly.

Uploaded by

pritampaul2k17
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)
13 views8 pages

Relational Algebra

The document provides an overview of relational algebra, including operations such as selection (σ), projection (∏), union (∪), set difference (-), Cartesian product (X), and renaming (ρ). It also explains join operations, specifically inner joins, and their types: theta join, equi join, and natural join. Examples are provided for each operation using relations for students and employees to illustrate the concepts clearly.

Uploaded by

pritampaul2k17
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/ 8

Relational Algebra

STUDENT

ROLL NAME AGE

1 Aman 20

2 Atul 18

3 Baljeet 19

4 Harsh 20

5 Prateek 21

6 Prateek 23

EMPLOYEE

EMPLOYEE_NO NAME AGE

E-1 Anant 20

E-2 Ashish 23

E-3 Baljeet 25

E-4 Harsh 20

E-5 Pranav 22
Select (σ)
Select operation is done by Selection Operator which is represented by "sigma"(σ). It is used to retrieve
tuples(rows) from the table where the given condition is satisfied. It is a unary operator means it requires
only one operand.
Notation : σ p(R)
Where σ is used to represent SELECTION
R is used to represent RELATION
p is the logic formula

Suppose we want the row(s) from STUDENT Relation where "AGE" is 20


σ AGE=20 (STUDENT)
This will return the following output:

ROLL NAME AGE

1 Aman 20

4 Harsh 20

Project (∏)
Project operation is done by Projection Operator which is represented by "pi"(∏). It is used to retrieve
certain attributes(columns) from the table. It is also known as vertical partitioning as it separates the
table vertically. It is also a unary operator.
Notation : ∏ a(r)
Where ∏ is used to represent PROJECTION
r is used to represent RELATION
a is the attribute list

Suppose we want the names of all students from STUDENT Relation.


∏ NAME(STUDENT)
This will return the following output:

NAME

Aman

Atul

Baljeet

Harsh

Prateek

As you can see from the above output it eliminates duplicates.


For multiple attributes, we can separate them using a ",".
∏ ROLL,NAME(STUDENT)
Above code will return two columns, ROLL and NAME.
ROLL NAME

1 Aman

2 Atul

3 Baljeet

4 Harsh

5 Prateek

6 Prateek

Union (∪)
Union operation is done by Union Operator which is represented by "union"(∪). It is the same as the
union operator from set theory, i.e., it selects all tuples from both relations but with the exception
that for the union of two relations/tables both relations must have the same set of Attributes. It is
a binary operator as it requires two operands.
Notation: R ∪ S
Where R is the first relation
S is the second relation
If relations don't have the same set of attributes, then the union of such relations will result in NULL.

Suppose we want all the names from STUDENT and EMPLOYEE relation.
∏ NAME(STUDENT) ∪ ∏ NAME(EMPLOYEE)

NAME

Aman

Anant

Ashish

Atul

Baljeet

Harsh

Pranav

Prateek

As we can see from the above output it also eliminates duplicates.


Set Difference (-)
Set Difference as its name indicates is the difference between two relations (R-S). It is denoted by a
"Hyphen"(-) and it returns all the tuples(rows) which are in relation R but not in relation S. It is also
a binary operator.
Notation : R - S
Where R is the first relation
S is the second relation
Just like union, the set difference also comes with the exception of the same set of attributes in both
relations.
We would like to know the names of students who are in STUDENT Relation but not in EMPLOYEE
Relation.
∏ NAME(STUDENT) - ∏ NAME(EMPLOYEE)
This will give us the following output:

NAME

Aman

Atul

Prateek

Cartesian product (X)


Cartesian product is denoted by the "X" symbol. Let's say we have two relations R and S. Cartesian
product will combine every tuple(row) from R with all the tuples from S. I know it sounds
complicated, but once we look at an example, you'll see what I mean.
Notation: R X S
Where R is the first relation
S is the second relation
As we can see from the notation it is also a binary operator.
Let's combine the two relations STUDENT and EMPLOYEE.
STUDENT X EMPLOYEE

ROLL NAME AGE EMPLOYEE_NO NAME AGE

1 Aman 20 E-1 Anant 20

1 Aman 20 E-2 Ashish 23

1 Aman 20 E-3 Baljeet 25

1 Aman 20 E-4 Harsh 20

1 Aman 20 E-5 Pranav 22

2 Atul 18 E-1 Anant 20

2 Atul 18 E-2 Ashish 23

2 Atul 18 E-3 Baljeet 25

2 Atul 18 E-4 Harsh 20

2 Atul 18 E-5 Pranav 22

. . . And so on.
Rename (ρ)
Rename operation is denoted by "Rho"(ρ). As its name suggests it is used to rename the output
relation. Rename operator too is a binary operator.
Notation: ρ(R,S)
Where R is the new relation name
S is the old relation name
Suppose we are fetching the names of students from STUDENT relation. We would like to rename
this relation as STUDENT_NAME.
ρ(STUDENT_NAME,∏ NAME(STUDENT))
STUDENT_NAME

NAME

Aman

Atul

Baljeet

Harsh

Prateek

As you can see, this output relation is named "STUDENT_NAME".


Join Operations
Join Operation in DBMS are binary operations that allow us to combine two or more relations.
They are further classified into two types: Inner Join, and Outer Join.
EMPLOYEE

E_NO E_NAME CITY EXPERIENCE

E-1 Ram Delhi 04

E-2 Varun Chandigarh 09

E-3 Ravi Noida 03

E-4 Amit Bangalore 07

DEPARTMENT

D_NO D_NAME E_NO MIN_EXPERIENCE

D-1 HR E-1 03

D-2 IT E-2 05

D-3 Marketing E-3 02

Also, let's have the Cartesian Product of the above two relations. It will be much easier to understand
Join Operations when we have the Cartesian Product.
E_NO E_NAME CITY EXPERIENCE D_NO D_NAME E_NO MIN_EXPERIENCE

E-1 Ram Delhi 04 D-1 HR E-1 03

E-1 Ram Delhi 04 D-2 IT E-2 05

E-1 Ram Delhi 04 D-3 Marketing E-3 02

E-2 Varun Chandigarh 09 D-1 HR E-1 03

E-2 Varun Chandigarh 09 D-2 IT E-2 05

E-2 Varun Chandigarh 09 D-3 Marketing E-3 02

E-3 Ravi Noida 03 D-1 HR E-1 03

E-3 Ravi Noida 03 D-2 IT E-2 05

E-3 Ravi Noida 03 D-3 Marketing E-3 02

E-4 Amit Bangalore 07 D-1 HR E-1 03

E-4 Amit Bangalore 07 D-2 IT E-2 05

E-4 Amit Bangalore 07 D-3 Marketing E-3 02

Inner Join
When we perform Inner Join, only those tuples returned that satisfy the certain condition. It is also
classified into three types: Theta Join, Equi Join and Natural Join.
Theta Join (θ)
Theta Join combines two relations using a condition. This condition is represented by the
symbol "theta"(θ). Here conditions can be inequality conditions such as >,<,>=,<=, etc.
Notation : R ⋈θ S
Where R is the first relation
S is the second relation
Suppose we want a relation where EXPERIENCE from EMPLOYEE >= MIN_EXPERIENCE from
DEPARTMENT.
EMPLOYEE⋈θ EMPLOYEE.EXPERIENCE>=DEPARTMENT.MIN_EXPERIENCE
DEPARTMENT

E_NO E_NAME CITY EXPERIENCE D_NO D_NAME E_NO MIN_EXPERIENCE

E-1 Ram Delhi 04 D-1 HR E-1 03

E-1 Ram Delhi 04 D-3 Marketing E-3 02

E-2 Varun Chandigarh 09 D-1 HR E-1 03

E-2 Varun Chandigarh 09 D-2 IT E-2 05

E-2 Varun Chandigarh 09 D-3 Marketing E-3 02


E_NO E_NAME CITY EXPERIENCE D_NO D_NAME E_NO MIN_EXPERIENCE

E-3 Ravi Noida 03 D-1 HR E-1 03

E-3 Ravi Noida 03 D-3 Marketing E-3 02

E-4 Amit Bangalore 07 D-1 HR E-1 03

E-4 Amit Bangalore 07 D-2 IT E-2 05

E-4 Amit Bangalore 07 D-3 Marketing E-3 02

Check the Cartesian Product, if in any tuple/row EXPERIENCE >= MIN_EXPERIENCE then insert
this tuple/row in output relation.
Equi Join
Equi Join is a special case of theta join where the condition can only
contain **equality(=)** comparisons.
A non-equijoin is the inverse of an equi join, which occurs when you join on a condition other than
"=".
Let's have an example where we would like to join EMPLOYEE and DEPARTMENT relation where
E_NO from EMPLOYEE = E_NO from DEPARTMENT.
EMPLOYEE ⋈EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT

E_NO E_NAME CITY EXPERIENCE D_NO D_NAME E_NO MIN_EXPERIENCE

E-1 Ram Delhi 04 D-1 HR E-1 03

E-2 Varun Chandigarh 09 D-2 IT E-2 05

E-3 Ravi Noida 03 D-3 Marketing E-3 02

Check Cartesian Product, if the tuple contains same E_NO, insert that tuple in the output relation
Natural Join (⋈)
A comparison operator is not used in a natural join. It does not concatenate like a Cartesian
product. A Natural Join can be performed only if two relations share at least one common attribute.
Furthermore, the attributes must share the same name and domain.
Natural join operates on matching attributes where the values of the attributes in both relations are the
same and remove the duplicate ones.
Preferably Natural Join is performed on the foreign key.
Notation : R ⋈ S
Where R is the first relation
S is the second relation
Let's say we want to join EMPLOYEE and DEPARTMENT relation with E_NO as a common
attribute.
Notice, here E_NO has the same name in both the relations and also consists of the same domain, i.e.,
in both relations E_NO is a string.
EMPLOYEE ⋈ DEPARTMENT
E_NO E_NAME CITY EXPERIENCE D_NO D_NAME MIN_EXPERIENCE

E-1 Ram Delhi 04 D-1 HR 03

E-2 Varun Chandigarh 09 D-2 IT 05

E-3 Ravi Noida 03 D-3 Marketing 02

But unlike the above operation, where we have two columns of E_NO, here we are having only one
column of E_NO. This is because Natural Join automatically keeps a single copy of a common
attribute.

You might also like