Submitted by –
Trijit Goswami (04)
Sourav Das(15)
Debanshu Sadhukhan(48)
Abhishek Bakshi(57)
A language based on operators and a domain
of values
Operators map values taken from the domain
into other domain values
Hence, an expression involving operators and
arguments produces a value in the domain
When the domain is a set of all relations
(and the operators are as described later),
we get the relational algebra
We refer to the expression as a query and
the value produced as the query result
Domain: set of relations
Basic operators: select, project, union,
set difference, Cartesian product,
rename
Derived operators: set intersection,
division, join
Procedural: Relational expression
specifies query by describing an
algorithm (the sequence in which
operators are applied) for determining
the result of an expression
Optimized
Relational Relational Query Executable
SQL algebra execution code
algebra
query expression expression plan
Code
parser
generator
Query optimizer
DBMS
Produce table containing subset of rows of
argument table satisfying condition
condition (relation)
Example:
Person Hobby=‘stamps’(Person)
Id Name Address Hobby Id Name Address Hobby
1123 John 123 Main stamps 1123 John 123 Main stamps
1123 John 123 Main coins 9876 Bart 5 Pine St stamps
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps
Operators: <, , , >, =,
Simple selection condition:
<attribute> operator <constant>
<attribute> operator <attribute>
<condition> AND <condition>
<condition> OR <condition>
NOT <condition>
Id>3000 Or Hobby=‘hiking’ (Person)
Id>3000 AND Id <3999 (Person)
NOT(Hobby=‘hiking’) (Person)
Hobby‘hiking’ (Person)
Produces
table containing subset of columns of
argument table
attribute list(relation)
Example:
Person
Name,Hobby(Person)
Id Name Address Hobby Name Hobby
1123 John 123 Main stamps John stamps
1123 John 123 Main coins John coins
5556 Mary 7 Lake Dr hiking Mary hiking
9876 Bart 5 Pine St stamps Bart stamps
• Example:
Person Name,Address(Person)
Id Name Address Hobby Name Address
1123 John 123 Main stamps John 123 Main
1123 John 123 Main coins Mary 7 Lake Dr
5556 Mary 7 Lake Dr hiking Bart 5 Pine St
9876 Bart 5 Pine St stamps
Result is a table (no duplicates)
Id, Name ( Hobby=’stamps’ OR Hobby=’coins’ (Person) )
Id Name Address Hobby Id Name
1123 John 123 Main stamps 1123 John
1123 John 123 Main coins 9876 Bart
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps Result
Person
Result of this operation is a relation that
includes all the tuples that are either in
first relation or in the second relation or in
both,duplicate values are eliminated.
sign - ∪
11
Example:
Consider two tables employee and company…
Employee Company
Emp_id Emp_name Emp_id Comp_id
E1 A E1 C1
E2 B E5 C2
E5 C E7 C3
E6 D E8 C4
E8 E E3 C5
Algebra: ∏Emp_id,Emp_name(Employee) ∪ ∏Emp_id,Comp_id(Company)
Output:
Emp_id Emp_name Comp_id
E1 A C1
E2 B -
E5 C C2
E6 D -
E8 E C4
E7 - C3
E3 - C5
The result of the intersection operation is a
relation that includes all the tuples that are
in both relation.
Symbol: ∩
Example:
Consider two tables employee and
company…
Employee Company
Emp_id Emp_name Emp_id Comp_id
E1 A E1 C1
E2 B E5 C2
E5 C E7 C3
E6 D E8 C4
E8 E E3 C5
Algebra: ∏Emp_id,Emp_name(Employee) ∩ ∏Emp_id,Comp_id(Company)
Output:
Emp_id Emp_name Comp_id
E1 A C1
E5 C C2
E8 E C4
The result of the operation contains all the
tuples that are only in first relation but not
in second relation.
Symbol: ‘ – ’
Example:
Consider two tables employee and
company…
Employee Company
Emp_id Emp_name Emp_id Comp_id
E1 A E1 C1
E2 B E5 C2
E5 C E7 C3
E6 D E8 C4
E8 E E3 C5
Algebra: ∏Emp_id,Emp_name(Employee) - ∏Emp_id,Comp_id(Company)
Output:
Emp_id Emp_name Comp_id
E2 B -
E6 D -
Relation is a set of tuples => set operations
should apply
Result of combining two relations with a set
operator is a relation => all its elements
must be tuples having same structure
Hence, scope of set operations limited to
union compatible relations
Two relations are union compatible if
Both have same number of columns
Names of attributes are the same in both
Attributes with the same name in both
relations have the same domain
Unioncompatible relations can be combined
using union, intersection, and set
difference
Tables:
Person (SSN, Name, Address, Hobby)
Professor (Id, Name, Office, Phone)
are not union compatible. However
Name (Person) and Name (Professor)
are union compatible and
Name (Person) - Name (Professor)
makes sense.
IfR and S are two relations, R S is the
set of all concatenated tuples <x,y>, where
x is a tuple in R and y is a tuple in S
(R and S need not be union compatible)
R S is expensive to compute:
Factor of two in the size of each row
Quadratic in the number of rows
a b c d a b c d
x1 x2 y1 y2 x1 x2 y1 y2
x3 x4 y3 y4 x1 x2 y3 y4
x3 x4 y1 y2
R S x3 x4 y3 y4
R S
Transcript (StudId, CrsCode, Semester, Grade)
Teach ing (ProfId, CrsCode, Semester)
StudId, CrsCode (Transcript)[StudId, CrsCode1]
ProfId, CrsCode(Teach ing) [ProfId, Crscode2]
This is a relation with 4 attributes:
StudId, CrsCode1, ProfId, CrsCode2
Join is used to fetch data from two or more
tables, which is joined to appear as single set
of data. It is used for combining column from
two or more tables by using values common to
both tables.
Types of join:
a. Inner join
b. Natural join
c. Outer join :
Left outer
Right outer
Full outer
Class
• The Inner join selects ID NAME
all rows from both 1 abhi
participating tables as 2 adam
long as there is a match 3 alex
between the columns. 4 anu
• symbol: ⋈ Common
Class_info
ID Address
attribute = common attribute 1 DELHI
2 MUMBAI
3 CHENNAI
Output:
NAME Address
abhi DELHI
adam MUMBAI
alex CHENNAI
Class
ID NAME
Special case of equijoin: 1 abhi
join condition equates all 2 adam
and only those attributes 3 alex
with the same name
4 anu
(condition doesn’t have to
be explicitly stated)
Class_info
duplicate columns
eliminated from the result ID Address
Symbol: ⋈ 1 DELHI
2 MUMBAI
3 CHENNAI
Output:
ID NAME Address
1 abhi DELHI
2 adam MUMBAI
3 alex CHENNAI
Class
ID NAME
1 abhi
Left outer join
includes the common 2 adam
values from both the 3 alex
realtion and 4 anu
uncommon value from 5 ashish
the left hand side Class_info
relation. ID Address
Symbol: ⟕ 1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
30
Output:
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
4 anu null null
5 ashish null null
Class
ID NAME
1 abhi
Right outer join include 2 adam
common values from 3 alex
bothe the relation and 4 anu
uncommon value from 5 ashish
the right hand side Class_info
relation ID Address
1 DELHI
Symbol: ⟖
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT 32
32
Output:
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
4 anu null null
5 ashish null null
Class
ID NAME
The full outer join 1 abhi
returns a result set 2 adam
table with 3 alex
the matched data of 4 anu
two table then 5 ashish
remaining rows of Class_info
both left table and ID Address
then the right table. 1 DELHI
2 MUMBAI
Symbol: ⟗
3 CHENNAI
7 NOIDA
8 PANIPAT
34
Output:
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
4 anu null null
5 ashish null null
null null 7 NOIDA
null null 8 PANIPAT
This operator is used to rename the
relations.
The results of relational algebra are also
relations but without any name. The rename
operation allows us to rename the output
relation. 'rename' operation is denoted with
small Greek letter rho ρ.
Symbol: ρ New relation name (Old relation name)
The division operator is used when we
have to evaluate queries which contain
the keyword ALL
Symbol: Relation1 ÷ Relation2
Course_taken
Student_Name Course
Robert Databases Course_required
Robert Programming
Course
Languages
Databases
David Databases
Programming Languages
David Operating Systems
Hannah Programming
Languages
Hannah Databases
Tom Operating Systems
Course_taken ÷ Course_required
Output:
Robert
Hannah
Relational Algebra Operators are critical mathematical
tools used to retrieve queries by describing a sequence
operations on tables or even databases(schema)
involved mathematically. With relational algebra
operators, a query is always composed of a number of
operators, which each in turn are composed of relations
as variables and return an individual abstraction as the
end product. Relational Algebra operations can easily be
translated into SQL commands to retrieve query
results, making it a powerful tool in the hands of any
Database designer, user, and administrator.