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

Relational Algebra

Relational Algebra, introduced by E.F. Codd in 1970, is a procedural query language that provides the theoretical foundation for relational databases and SQL. It consists of basic operators like Projection, Selection, and Union, as well as derived operators such as Join and Intersection. These operators allow for various operations on relations to retrieve and manipulate data effectively.

Uploaded by

riyadadhich07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Relational Algebra

Relational Algebra, introduced by E.F. Codd in 1970, is a procedural query language that provides the theoretical foundation for relational databases and SQL. It consists of basic operators like Projection, Selection, and Union, as well as derived operators such as Join and Intersection. These operators allow for various operations on relations to retrieve and manipulate data effectively.

Uploaded by

riyadadhich07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

RELATIONAL ALGEBRA

Relational Algebra given by E.F. Codd, who is the father of DBMS, in 1970.
It is also Known as Procedural Query Language or Formal Query Language.
It consists of a set of operations that take one or two relations as input and produce a new relation as
their result.
Realational Algebra mainly provides theoretical foundation for relational databases and SQL.

Operators used in Relational Algebra:-

Operators

Basic Operators Derived Operators

1. Projection (π) Pie 1. Join ( )


2. Selection (σ) Sigma 2. Intersect (n) (XnY)=X-(X-Y)
3. Cross Product (x) 3. Division (/,÷)
4. Union (U)
5. Rename (p) Rho
6. Set Difference (-)

Basic/Fundamental Operators
The Projection, Selection and Rename operations are called Uniary Operations because they operate on one
relation. The other three operators operate on pairs of relations and are therefore, called binary operators.

1. Projection (π) Pie


 Projection is used to project/retrieve required column data from a relation.
 By default Projection removes duplicate data.
Student Table

Roll No. Name Age


1 A 20
2 B 21
3 A 19

Query: Retrieve the Roll No. from Student Table.


πroll no(Student)
Roll No.
1
2
3

πname(Student)
Name
A
B

πrollno,name(Student)
Roll No. Name
1 A
2 B
3 A

2. Selection (σ) Sigma

Selection is used to select required tuples of the relations.

Student Table

Roll No. Name Age


1 A 20
2 B 21
3 A 19

Query: Retrieve the name of student whose Roll No.=”2”

σname(σrollno”2”(Student))

σrollno”2”(Student)

Roll No. Name Age


2 B 21

3. Cross Product (x)

Cross product between two relations let say A and B, so cross product between AxB will results all the
attributes of A followed by each attribute of B.

R1 A B C
1 2 3
2 1 4

R2
D E F
3 4 5
2 1 2

R1xR2

A B C D E F
1 2 3 3 4 5
1 2 3 2 1 2
2 1 4 3 4 5
2 1 4 2 1 2

4. Union (U)
 Union operator in relational algebra is same as Union operation in set theory only constraint is for
union of two relations both relations must have same set of attributes.
 No. of columns must be same in tables.
 Domain of columns must be same.
Student
Roll No. Name
1 A
2 B
3 C
Employee
Emp No. Name
7 E
1 A

(Student)U(Employee)
Roll no Name
1 A
2 B
3 C
7 E

πname(Student)Uπname(Employee)
Name
A
B
C
E

5. Rename (p) Rho


The Rename operation is used to rename the output relation. It is denoted by Rho(p).
Rename

Rename Relation Name Rename Attribute Name

Eg- if we have two relations “Student” and “Grade” and we want to retrieve the name of the students
with “A” grade. And we want the output relation with the name “Student grade”, then the query will
be written as-
PStudentgrade[σGrade”A”(StudentxGrade)]

To change the name of the attribute


Pa1/a2(Tablename)
a1=New attribute name
a2=Old attribute name
Eg- Student

Std-id S-name

There is a table with 2 attributes. If we want to change the name of the attribute “Std-id” with “Id”,
then the query will be written as-
PId/Std-id(Student)

6. Set Difference (-)


Set difference in relational algebra is same as set difference operation in set theory with the constraint
that both relations should have same set of attributes

Student
Roll No. Name
1 A
2 B
3 C
Employee
Emp No. Name
7 E
1 A

Student-Employee

Roll No Name
2 B
3 C

Derived /Extended Operators


Extended Operators are those operators which can be derived from Basic operators.

1. Intersection(n)

Intersection on two relations can only be computed if those two relations are Union compatible i.e. these
two relations should have same no. of attributes and corresponding attributes in two relations have same
domain.

Student

Roll_no. Name
1 A
2 B
3 C

Employee
Emp_no Name
7 E
1 A
StudentnEmployee

1 A
Student

Name Sub_failed
A DBMS
A JAVA
B DBMS
C JAVA
Subject
Sub
DBMS
JAVA

Query- Find the names of students who failed in all the subjects.
Student÷Subjects

A DBMS
JAVA

πname(Student÷Subjects)

A
3. Joins( )
Join operation is as its name suggest, to join or combine two or more relation’s information.
Join is a combination of a Cartesian/cross product followed by a selection process.

Types of Joins

Inner Join Outer Join

Left Outer
Theta Join
Join

Right
Equi Join
Outer Join

Natural Full Outer


Join Join

Inner Join: An inner join includes only those tuples that satisfy the matching criteria, while the rest
of tuples are excluded.

 THETA(Ѳ)/CONDITIONAL JOIN

 It combines tuples from different relations provided they satisfy the Theta(Ѳ) condition.
 It uses all kinds of comparison operators like <,>,<=,>=,=,≠
R1
S_Id Name Age
22 A 45
31 B 55
58 C 50
R2
S_Id B-day
22 10/10/1996
58 11/12/1996

Query: R1 R1.S_Id<R2.S_IdR2

S_Id Name Age S_Id B-day


22 A 45 58 11/12/1996
31 B 55 58 11/12/1996

 EQUI JOIN(=)

 When a Theta join uses only equivalence(=) condition, it becomes a Equi Join.
 Equi join is a special case of Theta join where condition contains equalities(=)
R1
S_Id Name Age
22 A 45
31 B 55
58 C 50
R2
S_Id B-day
22 10/10/1996
58 11/12/1996

Query: R1 R1.S_Id=R2.S_IdR2

S_Id Name Age S_Id B-day


22 A 45 22 10/10/1996
58 C 50 58 11/12/1996

 NATURAL JOIN( )

 Natural join can only be performed if there is at least one common attribute that exist
between two relations.
 In addition, the attributes must have the same name and domain.
 Natural join does not use any comparison operator.
 It is same as Equi join which occurs implicitly by comparing all the common attributes in
both relations, but difference is that in Natural join the common attributes appears only
once. The resulting schema will change.
 The result of the Natural join is the set of all combinations of tuples in two relations A
and B that are equal on their common attribute names.

R1
S_Id Name Age
22 A 45
31 B 55
58 C 50
R2
S_Id B-day
22 10/10/1996
58 11/12/1996

R1 R2

S_Id Name Age B-day


22 A 45 10/10/1996
31 B 55
58 C 50 11/12/1996

Outer Join: The outer join operation is an extension of the join operation that avoids loss of information.
 LEFT-OUTER JOIN ( )

The left-outer join is an outer join that returns all the values of the left table, and the values of
the right table has matching values in the left table.

Employee

Emp_no E_name Dept_no


E1 A D1
E2 B D2
E3 C D3
E4 D

Department
Dept_no D_name Location
D1 IT Delhi
D2 HR Jaipur
D3 Finance Pune

Syntax: SELECT Table name.column name,Table name.column name…… Table name.column name FROM
Employee LEFT OUTER JOIN Department ON Employee.Dept_no=Department.Dept_no
Query: SELECT Employee.Emp_no,Employee.E_name,Department.D_name,Department.Location FROM
Employee LEFT OUTER JOIN Department ON Employee.Dept_no=Department.Dept_no

Employee Department

Emp_no E_name D_name Location


E1 A IT Delhi
E2 B HR Jaipur
E3 C IT Delhi
E4 D - -

 RIGHT-OUTER JOIN ( )

The Right-Outer Join is an outer join that returns all the values of the right table and the values
of the left table that has matching values in the right table.

Employee

Emp_no E_name Dept_no


E1 A D1
E2 B D2
E3 C D1
E4 D

Department
Dept_no D_name Location
D1 IT Delhi
D2 HR Jaipur
D3 Finance Pune
Syntax: SELECT Table name.column name,Table name.column name…… Table name.column name FROM
Employee RIGHT OUTER JOIN Department ON Employee.Dept_no=Department.Dept_no

Query: SELECT Employee.Emp_no,Employee.E_name,Department.D_name,Department.Location FROM


Employee RIGHT OUTER JOIN Department ON Employee.Dept_no=Department.Dept_no
Employee Department

Emp_no E_name D_name Location


E1 A IT Delhi
E2 B HR Jaipur
E3 C IT Delhi
- - Finance Pune

 FULL-OUTER JOIN ( )

The Full-Outer Join contains all the values of both the tables whether they have matching values in them
or not.

Employee

Emp_no E_name Dept_no


E1 A D1
E2 B D2
E3 C D3
E4 D

Department
Dept_no D_name Location
D1 IT Delhi
D2 HR Jaipur
D3 Finance Pune
Syntax: SELECT Table name.column name,Table name.column name…… Table name.column name FROM
Employee FULL OUTER JOIN Department ON Employee.Dept_no=Department.Dept_no

Query: SELECT Employee.Emp_no,Employee.E_name,Department.D_name,Department.Location FROM


Employee FULL OUTER JOIN Department ON Employee.Dept_no=Department.Dept_no
Employee Department

Emp_no E_name D_name Location


E1 A IT Delhi
E2 B HR Jaipur
E3 C IT Delhi
E4 D - -
- - Finance Pune

You might also like