Relational Algebra
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
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.
πname(Student)
Name
A
B
πrollno,name(Student)
Roll No. Name
1 A
2 B
3 A
Student Table
σname(σrollno”2”(Student))
σrollno”2”(Student)
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
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)]
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)
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
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
Left Outer
Theta Join
Join
Right
Equi Join
Outer 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
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
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
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
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
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
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
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
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