0% found this document useful (0 votes)
6 views9 pages

DBMS 13

The document provides class notes on relational algebra, focusing on set theory operators such as Union, Intersection, Difference, and Cartesian product, along with their syntax and examples. It also covers the Rename operation and various types of Join operations, including Natural Join, Left Outer Join, Right Outer Join, and Full Outer Join, explaining how they combine tuples from different relations based on specified conditions. Each operator is illustrated with examples to demonstrate their functionality and output.

Uploaded by

tapatapdobo
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)
6 views9 pages

DBMS 13

The document provides class notes on relational algebra, focusing on set theory operators such as Union, Intersection, Difference, and Cartesian product, along with their syntax and examples. It also covers the Rename operation and various types of Join operations, including Natural Join, Left Outer Join, Right Outer Join, and Full Outer Join, explaining how they combine tuples from different relations based on specified conditions. Each operator is illustrated with examples to demonstrate their functionality and output.

Uploaded by

tapatapdobo
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/ 9

Government of West Bengal

Dr. Meghnad Saha Institute of Technology, Haldia


Department of Computer Science & Technology
Class Notes on Relational Algebra

Set Theory Operators-

Following operators are called as set theory operators-

1. Union Operator (∪)


2. Intersection Operator (∩)
3. Difference Operator (-)

Condition for Using Set Theory Operators


To use set theory operators on two relations,
The two relations must be union compatible.
Union compatible property means-
 Both the relations must have same number of attributes.

 The attribute domains (types of values accepted by attributes) of both the relations
must be compatible.
1. Union Operator (∪)-
Union operator is denoted by ∪ symbol and it is used to select all the rows (tuples) from two
tables (relations).
Let’s say we have two relations R1 and R2 both have same columns and we want to select
all the tuples (rows) from these relations then we can apply the union operator on these
relations.
Let R1 and R2 be two relations.
Then-
 R1 ∪ R2 is the set of all tuples belonging to either R1 or R2 or both.
 In R1 ∪ R2, duplicates are automatically removed.

Prepared by Sukanta Singh, Lecturer in CST Page 10 of 18


Government of West Bengal
Dr. Meghnad Saha Institute of Technology, Haldia
Department of Computer Science & Technology
Class Notes on Relational Algebra

 Union operation is both commutative and associative.

Note: The rows (tuples) that are present in both the tables will only appear once in the
union set. In short you can say that there are no duplicates present after the union
operation.
Syntax of Union Operator (∪)
table_name1 ∪ table_name2
Union Operator (∪) Example:
Table 1: COURSE
Course_Id Student_Name Student_Id
C101 Aditya S901
C104 Aditya S901
C106 Steve S911
C109 Paul S921
C115 Lucy S931
Table 2: STUDENT
Student_Id Student_Name Student_Age
S901 Aditya 19
S911 Steve 18
S921 Paul 19
S931 Lucy 17
S941 Carl 16
S951 Rick 18
Query:
∏ Student_Name (COURSE) ∪ ∏ Student_Name (STUDENT)
Output:
Student_Name
Aditya
Carl
Paul
Lucy
Rick
Steve

Relation COURSE ∪ STUDENT

Prepared by Sukanta Singh, Lecturer in CST Page 11 of 18


Government of West Bengal
Dr. Meghnad Saha Institute of Technology, Haldia
Department of Computer Science & Technology
Class Notes on Relational Algebra

Note: As you can see there are no duplicate names present in the output even though we
had few common names in both the tables, also in the COURSE table we had the duplicate
name itself.

2. Intersection Operator (∩)-


Intersection operator is denoted by ∩ symbol and it is used to select common rows (tuples)
from two tables (relations).
Let’s say we have two relations R1 and R2 both have same columns and we want to select
all those tuples (rows) that are present in both the relations
Then-
 R1 ∩ R2 is the set of all tuples belonging to both R1 and R2.

 In R1 ∩ R2, duplicates are automatically removed.

 Intersection operation is both commutative and associative.

Note: Only those rows that are present in both the tables will appear in the result set.
Syntax of Intersection Operator (∩)
table_name1 ∩ table_name2
Intersection Operator (∩) Example
Let’s take the same example that we have taken above.
Table 1: COURSE
Course_Id Student_Name Student_Id
C101 Aditya S901
C104 Aditya S901
C106 Steve S911
C109 Paul S921
C115 Lucy S931

Table 2: STUDENT
Student_Id Student_Name Student_Age
S901 Aditya 19
S911 Steve 18
S921 Paul 19
S931 Lucy 17
S941 Carl 16
S951 Rick 18

Prepared by Sukanta Singh, Lecturer in CST Page 12 of 18


Government of West Bengal
Dr. Meghnad Saha Institute of Technology, Haldia
Department of Computer Science & Technology
Class Notes on Relational Algebra

Query:
∏ Student_Name (COURSE) ∩ ∏ Student_Name (STUDENT)

Output:
Student_Name
Aditya
Steve
Paul
Lucy

Relation COURSE ∩ STUDENT


3. Difference Operator (-):
Set Difference is denoted by – symbol. Let’s say we have two relations R1 and R2 and we
want to select all those tuples (rows) that are present in Relation R1 but not present in
Relation R2,
Then-
 R1 – R2 is the set of all tuples belonging to R1 and not to R2.

 In R1 – R2, duplicates are automatically removed.

 Difference operation is associative but not commutative.

Syntax of Set Difference (-)


table_name1 - table_name2
Set Difference (-) Example
Let’s take the same tables COURSE and STUDENT that we have seen above.
Query:
Lets write a query to select those student names that are present in STUDENT table but not
present in COURSE table.
∏ Student_Name (STUDENT) - ∏ Student_Name (COURSE)
Output:
Student_Name
Carl
Rick

Relation R1 – R2

Prepared by Sukanta Singh, Lecturer in CST Page 13 of 18


Government of West Bengal
Dr. Meghnad Saha Institute of Technology, Haldia
Department of Computer Science & Technology
Class Notes on Relational Algebra

4. Cartesian product (X):


Cartesian product is denoted by X symbol. Let’s say we have two relations R1 and R2 then
the Cartesian product of these two relations (R1 X R2) would combine each tuple of first
relation R1 with the each tuple of second relation R2.
Syntax of Cartesian product (X)
R1 X R2
Cartesian product (X) Example
Table 1: R
Col_A Col_B
AA 100
BB 200
CC 300

Table 2: S
Col_X Col_Y
XX 99
YY 11
ZZ 101
Query:
Let’s find the Cartesian product of table R and S.
RXS
Output:
Col_A Col_B Col_X Col_Y
AA 100 XX 99
AA 100 YY 11
AA 100 ZZ 101
BB 200 XX 99
BB 200 YY 11
BB 200 ZZ 101
CC 300 XX 99
CC 300 YY 11
CC 300 ZZ 101

Prepared by Sukanta Singh, Lecturer in CST Page 14 of 18


Government of West Bengal
Dr. Meghnad Saha Institute of Technology, Haldia
Department of Computer Science & Technology
Class Notes on Relational Algebra

Note: The number of rows in the output will always be the cross product of number of rows
in each table. In our example table 1 has 3 rows and table 2 has 3 rows so the output has
3×3 = 9 rows.
Rename (ρ):
Rename (ρ) operation can be used to rename a relation or an attribute of a relation.

Rename (ρ) Syntax:


ρ(new_relation_name, old_relation_name)
Rename (ρ) Example
Let’s say we have a table customer, we are fetching customer names and we are renaming
the resulted relation to CUST_NAMES.
Table: CUSTOMER
Customer_Id Customer_Name Customer_City
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi

Query:
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:
CUST_NAMES
Steve
Raghu
Chaitanya
Ajeet
Carl

Prepared by Sukanta Singh, Lecturer in CST Page 15 of 18


Government of West Bengal
Dr. Meghnad Saha Institute of Technology, Haldia
Department of Computer Science & Technology
Class Notes on Relational Algebra

Joins operation in relational algebra


Join operation in relational algebra is a combination of a Cartesian product followed by
which satisfy certain condition. A Join operation combines two tuples from two different
relations, if and only if a given condition is satisfied.
There are different types of join operations.
(I) Natural Join (⋈)
A result of 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 ⋈.
Consider the following example to understand natural Joins.
EMPLOYEE
EMP_ID EMP_NAME
1 Ram
2 Varun
3 Lakshmi
SALARY
EMP_ID SALARY
1 50000
2 30000
3 25000
∏ EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
Output:
EMP_NAME SALARY
Ram 50000
Varun 30000
Lakshmi 25000
(II) Outer Join
Outer joins are used to include all the tuples from the relations included in join operation
in the resulting relation.
An outer join is of three types:

Prepared by Sukanta Singh, Lecturer in CST Page 16 of 18


Government of West Bengal
Dr. Meghnad Saha Institute of Technology, Haldia
Department of Computer Science & Technology
Class Notes on Relational Algebra

Left outer join


Right outer join
Full outer join
Consider the example
EMPLOYEE
EMP_NAME STREET CITY
Ram Civil line Mumbai
Varun S.G.Road Kolkata
Lakshmi C.G.Road Delhi
Hari AnandNagar Hyderabad
FACT_WORKERS
EMP_NAME BRANCH SALARY
Ram Infosys 10000
Varun Wipro 20000
Neha HCL 30000
Hari TCS 50000
Left outer join (⟕)
In Left outer join, all the tuples from the Left relation, say R, are included in the resulting
relation. If there are some tuples in relation R which are not matched with tuple in the
Right Relation S, then the attributes of relation R of the resulting relation become NULL.
EMPLOYEE ⟕ FACT_WORKERS
Output:
EMP_NAME STREET CITY BRANCH SALARY
Ram Civil line Mumbai Infosys 10000
Varun S.G.Road Kolkata Wipro 20000
Hari AnandNagar Hyderabad TCS 50000
Lakshmi C.G.Road Delhi NULL NULL

Prepared by Sukanta Singh, Lecturer in CST Page 17 of 18


Government of West Bengal
Dr. Meghnad Saha Institute of Technology, Haldia
Department of Computer Science & Technology
Class Notes on Relational Algebra

Right outer join (⟖)


In Right outer join, all the tuples from the Right relation, say S, are included in the
resulting relation. If there are some tuples in relation S which are not matched with tuple
in the Right Relation R, then the attributes of relation S of the resulting relation become
NULL.
EMPLOYEE ⟖ FACT_WORKERS
Output :
EMP_NAME BRANCH SALARY STREET CITY
Ram Infosys 10000 Civil line Mumbai
Varun Wipro 20000 S.G.Road Kolkata
Hari TCS 50000 AnandNagar Hyderabad
Neha HCL 30000 NULL NULL

Full outer join


Full outer join is the combination of both left outer join and right outer join. It contains all
the tuples from both relations.
For example
EMPLOYEE ⟗ FACT_WORKERS
EMP_NAME STREET CITY BRANCH SALARY
Ram Civil line Mumbai Infosys 10000
Shyam S.G.Road Kolkata Wipro 20000
Hari AnandNagar Hyderabad TCS 50000
Lakshmi C.G.Road Delhi NULL NULL
Neha NULL NULL HCL 30000

Prepared by Sukanta Singh, Lecturer in CST Page 18 of 18

You might also like