Relational Algebra
Relational Algebra
• Relational Algebra is a procedural query language, which takes relation as input and generate
relation as output. Relational Algebra mainly provides theoretical foundation for relational
databases and SQL.
• Relational Algebra is a language for expressing relational database queries.
• It uses operators to perform queries. And operator can be either unary or binary.
Types of operator in Relational Algebra
Selection (σ) :
• selection operator is a unary operator in relational algebra that performs a selection operation .
• It selects tuples (or rows) that satisfy the given condition from a relation.
• It is denoted by (σ)
Notation:
σ condition (Relation Name)
Example: select tuple from student table whose age is greater than 17
σ age>17 (Student)
Basic/ Fundamental Operators
Table: student
Roll_no Name Age Address
1 A 20 Bhopal
2 B 17 Mumbai
3 C 16 Mumbai
4 D 19 Delhi
5 E 18 Delhi
Example: select tuple from student table whose age is greater than 17
σ age>17 (Student)
Basic/ Fundamental Operators
Example: select tuple from student table whose age is greater than 17
σ Age > 17 (Student)
output
Roll_no Name Age Address
1 A 20 Bhopal
4 D 19 Delhi
5 E 18 Delhi
Basic/ Fundamental Operators
Example: select tuple from student table whose age is greater than 17 and who lives in Delhi.
σ Age > 17 ʌ Address = “Delhi (Student)
Projection (Π) :
• Projection operator(Π) is a unary operator in relational algebra that performs a projection
operation .
• It projects (or displays) the particular columns ( or attributes) from a relation.
• It delete the column(s) that are not in the projection list.
• It is denoted by (Π )
Notation:
Π attribute_list (Relation Name)
Duplicate rows are automatically eliminated from result.
Basic/ Fundamental Operators
Example: display the columns roll_no and name from the relation student.
Π roll-no, name( student)
Table: student Output
Roll_no Name Age Roll_no Name
1 A 20 1 A
2 B 17 2 B
3 C 18 3 C
4 D 19 4 D
5 E 18 5 E
6 F 18 6 F
Basic/ Fundamental Operators
Example: display ( Project) the name of students from the relation student.
Π name ( student)
Table: student Output
Roll_no Name Age Name
1 A 20 A
2 B 17 B
3 C 18 C
4 D 19 D
5 E 18 E
6 F 18 F
Basic/ Fundamental Operators
Example:
R1 R2
A B C D
1 X 2 P
2 Y 3 Q
R1 X AR2 B C D
1 X 2 P
1 X 3 Q
2 Y 2 P
2 Y 3 Q
Basic/ Fundamental Operators
Example:
R1 X R2
A B C D
1 X 2 P
1 X 3 Q
2 Y 2 P
2 Y 3 Q
Example:
R1 X R2
A B C D
1 X 2 P
1 X 3 Q
2 Y 2 P
2 Y 3 Q
Rename Operator:
• The Rename Operator is used to rename the output of a relation.
Symbol: rho (ρ )
Notation: ρ S ( E )
Where (ρ ) is used to denote the rename operator.
(E) Is the result of operation which is saved with the name s.
R R X ρ S (R)
A B R.A R.B S.A S.B
P 1 P 1 P 1
Q 2 P 1 Q 2
Q 2 P 1
R X ρ S (R ) Q 2 Q 2
Rename R to S.
Basic/ Fundamental Operators
Union Operator ( U) :
• The union operator selects all the tuples that are either in Relation R or S or
in both relations R and S.
• For Union Operation to be valid, the following conditions must hold.
• Two relations R and S both have same number of attributes.
• Corresponding attribute (or column) have same domain ( or type)
• The attributes of R and S must occur in the same order.
Student Employee
Roll_no Name
Roll_no Name Emp_no Name 1 A
1 A 2 B 2 B
2 B 8 G 3 C
3 C 9 H 4 D
4 D 8 G
9 H
Basic/ Fundamental Operators
Name
A
B
C
D
G
H
Basic/ Fundamental Operators
Example
Student Employee (Student) – ( Employee)
Roll_no Name Emp_no Name Roll_no Name
1 A 2 B 1 A
2 B 8 G 3 C
3 C 9 H 4 D
4 D
Symbol:
A C B = σC(AXB)
Types of Join
• Inner join
• Theta join
• Equi join
• Natural join
• Outer join
• Left outer join
• Right outer join
• Full outer join
Inner Join
• Inner join
An inner join includes only those tuples that satisfy the matching criteria,
while the rest of tuples are excluded.
Theta join, equi join and natural join are called inner join
Theta( θ ) / Conditional Join
• it combines tuples from different relations provided they satisfy the Theta (
θ ) Condition.
• It is general case of join and it is used when we want to join two or more
relation based on some condition.
• The join conditions is denoted by the symbol ( θ ).
• It uses all kinds of comparison operator like < , >, < = , > = , = , < > .
Notation: A θ B
Example :
R1 R1 X S1
A B C
A B R1.C S1.C D E
1 8 25
1 8 25 26 X 45
2 9 30
1 8 25 22 Y 43
3 10 22
1 8 25 24 Z 51
2 9 30 26 X 45
S1
2 9 30 22 Y 43
C D E
2 9 30 24 Z 51
26 X 45
3 10 22 26 X 45
22 Y 43
3 10 22 22 Y 43
24 Z 51
3 10 22 24 Z 51
Theta( θ ) / Conditional Join
Example :
R1 X S1
A B R1.C S1.C D E
1 8 25 26 X 45
3 10 22 26 X 45
3 10 22 24 Z 51
Notation :
A A.a1 = B.b1 ʌ ………ʌA.an = B.B.bn B
Equi Join
Example:
Courses HOD
Cid Course Dept Dept Head
101 Database CS CS Rohan
102 Mechanics ME ME Sara
103 Electronics EE EE Jiya
Courses HOD
Cid Course Dept Head
101 Database CS Rohan
102 Mechanics ME Sara
103 Electronics EE Jiya
Natural Join
• An inner join includes only those tuples with matching attributes and the
rest are discarded in the resulting relation. Therefore, we need to use outer
join to include all the rest of the tuples from the participating relations in
the resulting relation.
• The outer join operation is an extension of the join operation that avoids
loss of information.
• Outer Join contains matching tuples that satisfy the matching condition ,
along with some or all tuples that do not satisfy the matching condition.
• It is based on both matched and unmatched tuple.
• It contains all rows from either one or both relations are present.
• It uses NULL values . NULL signifies that the value is unknown or does not
exist.
Left Outer Join (R1 R2 )
• Left Outer Join, all the tuples from left relation R1 are included in the
resulting relation . The tuples from R1 which do not satisfy Join condition ,
will have values as NULL for attributes of R2 .
• In Short
• All records from left table.
• Only matching records from right table.
Symbol :
Notation: R1 R2
Left Outer Join (R1 R2 )
Example :
Courses HOD
Cid Course Cid Name
100 Database 100 Rohan
101 Mechanics 102 Sara
102 Electronics 104 Jiya
Courses HOD
Cid Course Name
100 Database Rohan
101 Mechanics NULL
102 Electronics Jia
Right Outer Join (R1 R2 )
• In Right Outer Join, all the tuples from right relation R2 are included in the
resulting relation. The tuples of R2 which do not satisfy join condition will
have values as NULL for attributes of R1.
• In short:
• All records from right table
• Only matching record from left table.
Symbol :
Notation : R1 R2
Right Outer Join (R1 R2 )
Example :
Courses HOD
Cid Course Cid Name
100 Database 100 Rohan
101 Mechanics 102 Sara
102 Electronics 104 Jiya
Courses HOD
Cid Course Name
100 Database Rohan
102 Electronics Sara
104 NULL Jia
Full Outer Join (R1 R2 )
• In Full Outer Join, all the tuples from both left relation R1 and right relation
R2 are included in the resulting relation. The tuples of both relations R1 and
R2 which do not satisfy Join condition, their respective unmatched
attributes are made NULL .
• In short:
• All records from all tables.
Symbol:
Notation : R1 R2
Full Outer Join (R1 R2 )
Example :
Courses HOD
Cid Course Cid Name
100 Database 100 Rohan
101 Mechanics 102 Sara
102 Electronics 104 Jiya
Courses HOD
Cid Course Name
100 Database Rohan
101 Mechanics NULL
102 Electronics Sara
104 NULL Jiya
Division Operator
Division Operation is represented by "division"(÷ or /) operator and is
used in queries that involve keywords "every", "all", etc.
Notation : R(X,Y)/S(Y)
Here,
R is the first relation from which data is retrieved.
S is the second relation that will help to retrieve the data.
X and Y are the attributes/columns present in relation. We can have
multiple attributes in relation, but keep in mind that attributes of S
must be a proper subset of attributes of R.
For each corresponding value of Y, the above notation will return us
the value of X from tuple<X,Y> which exists everywhere.
Example
the query is to return the STUDENT_ID of students who are enrolled in every course
Database Modification
• Deletion
• Insertion
• Updation
Deletion
Insertion
Updation
Update, delete and insert
• https://www.youtube.com/watch?v=rfH9Q4gS9BQ
• https://www.scaler.com/topics/dbms/relational-algebra-in-dbms/
• https://www.linkedin.com/pulse/02-dml-relational-algebra-build-
your-own-dbms-from-first-el-deen/