Unit 3 Query Languages 1 Relational Algebra
Unit 3 Query Languages 1 Relational Algebra
Relational Algebra
Relational Algebra
• The relational algebra provides basic operations which can be performed over
single or multiple relations in order to generate new relations(single or
multiple).
• Relational algebra is a procedural query language which follows a particular
syntax with the help of which, data can be accessed and retrieved very easily
from single as well as multiple table/data sources.
• Certain operators are used to perform queries and retrieve desired results.
• These operators can perform certain operations on single attribute(called
unary operator) or multiple attribute(called binary operator).
• Types of operations in relational algebra
• 1. Basic Operations
• 2. Derived Operations
Relational Algebra
• Basic/Fundamental Operations:
• 1. Select (σ)
• 2. Project (∏)
• 3. Union (∪)
• 4. Set Difference (-)
• 5. Cartesian product (X)
• 6. Rename (ρ)
• Derived Operations:
• 1. Natural Join (⋈)
• 2. Left, Right, Full outer join (⟕, ⟖, ⟗)
Relational Algebra : Operations
• Select (σ): This operation is used to fetch rows from given table or relation
on the basis of given conditions, it is denoted by “Sigma(σ)”.
• Syntax : σ Condition/Predicate(Relation/Table name)
• where clause in SQL, is used for the same purpose.
• For example : Consider the table of relation R(Roll No, Name, Age, Marks). If
we want to select the name and age of student, then it can be done by:
• Query Used : σ Name and Age>21 (Student_Details)
Relational Algebra : Operations
• Project(∏): This operation is also used to fetch all the rows/tuples/data
according to the requested attribute/Column. It means, using project
operation one can simply fetch all the tuples corresponding to a single
attribute or multiple attributes. It does not supports any conditions as select
operation and is denoted using “Pie(π)”.
• Syntax :∏column_name1,column_name2,..,column_nameN(table_name)
• Project operator in relational algebra is similar to the Select statement in SQL.
• For example : Consider the table of relation R(Roll No, Name, Age, Marks). If
we want to project the marks column, then it can be done by :
• Query Used : πMarks(Student_Details)
Relational Algebra : Operations
• Union(∪):In order to fetch data from two relations to generate new relation
with combined capabilities, union operations can be used. The union operation
fetches the data from both tables and projects it accordingly. It is denoted
through “Union Symbol(U)”.
• In Union Operation Both the relations compulsory to have same domain for
attributes.
• Syntax : X1 U X2 , where X1 & X2 are two different relations satisfying the
above two conditions.
• 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.
Relational Algebra : Operations
• Intersection(∩) : Intersection operator is denoted by ∩ symbol and it is used
to select common rows (tuples) from two tables (relations).
• Lets 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 in that case we can apply intersection operation on these two relations
R1 ∩ R2.
• Note: Only those rows that are present in both the tables will appear in the
result set. COURSE ∩ STUDENT
Relational Algebra : Operations
• Set Difference (-) : In order to fetch the data which is not present in any one
of the relation, set difference operation is used. The set difference operation
is denoted by “Minus(-)”.
• Lets 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,
this can be done using Set difference R1 – R2.
• For example : Consider the two tables with relations X1(Name, Age) and
X2(Name, Age). If we wish to apply the set difference operation, then it can
be done by :
Relational Algebra : Operations
• Cartesian product (X): The Cartesian product operation will generate the
possible combinations among the tuples from the relations resulting in
table containing all the data.
• It combines the information of two or more relations in one single relation.
Cartesian product is different from union operation and is denoted by
“Cross(X)”.
Relational Algebra : Operations
• Rename (ρ) : Rename (ρ) operation can be used to rename a relation or an
attribute of a relation.
• Rename (ρ) Syntax: ρ(new_relation_name, old_relation_name)
• Query: ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Relational Algebra
• Basic/Fundamental Operations:
• 1. Select (σ)
• 2. Project (∏)
• 3. Union (∪)
• 4. Set Difference (-)
• 5. Cartesian product (X)
• 6. Rename (ρ)
• Derived Operations:
• 1. Natural Join (⋈)
• 2. Left, Right, Full outer join (⟕, ⟖, ⟗)
Relational Algebra : Joins
• The database joins has the ability of
combining two or more data
tables/tuples into a single
table/tables only if the following
conditions are satisfied.
• There must be a common attribute in
both(tables which are participating)
tables.
• Join condition must be satisfied.
• Database joins can be broadly
classified into two categories which
are further categorized into sub
categories.
Relational Algebra : Joins (The Inner Joins)
• When inner join is applied to tuples or tables, only those tuples of the table are kept
which have common attribute in all the tables. Other tuples which are not common are
dropped from the resulting tuple/table.
• Two possible inner joins are available i.e. Theta Join & Natural J
• Syntax : R1(X1, X2,X3…Xn) ⋈(Condition “θ”) R2(Y1, Y2,Y3…Yn) where, R1 and R2 are
relations having (X1, X2,X3…Xn) and (Y1, Y2,Y3…Yn) as join.
• Theta Join :If a condition is satisfied by the participating tables from different relations, then
the tuples are combined together using Theta Join. Theta join is denoted through
“Theta(Θ)”. attributes respectively.
• For example : Consider the
tables Student_Details and
Student_Result. Now, if we
want to implement theta join
on these relations, the result
will look like:
Relational Algebra : Joins (The Inner Joins)
• Natural Join (⋈):
• Natural join does not supports any condition such as theta join and works
only if, one attribute or more than one attributes are common between the
joining/participating relations.
• Syntax : R1(X1, X2,X3…Xn) ⋈ R2(Y1, Y2,Y3…Yn) where, R1 and R2 are
relations having (X1, X2,X3…Xn) and (Y1, Y2,Y3…Yn) as attributes
respectively.
• For example : Consider the tables Student_Details and Student_Result. Now,
if we want to implement natural join on these relations, the result will be
Relational Algebra : Joins (The Outer Joins)
• Natural Join (⋈):Outer join overcomes the inability of inner joins of dropping
the tuples which are uncommon among participating relations. If we want to
display those tuples which are not common, the concept of outer join is used.
• Also, if all the tuples needs to be displayed from all the participating relations,
outer joins can be used. They are of three types : Left Outer Join, Right Outer Join
& Full Outer Join.
• Left Outer Join
• There exists a concept of position(left or right) of relations in case of both left and
right outer join.
• In the left outer join, operation allows keeping all tuple in the left relation.
• If in case any tuple in left relation does not matches with the tuple in right
relation, NULL value will be displayed against that tuple in the resulting relation.
Relational Algebra : Joins (The Outer Joins)
• Left Outer Join:
Set Difference(-) The result of A – B, is a relation which includes all tuples that are in A but not in B.
Intersection(∩) Intersection defines a relation consisting of a set of all tuple that are in both A and B.
Cartesian Product(X) Cartesian operation is helpful to merge columns from two relations.
Inner Join Inner join, includes only those tuples that satisfy the matching criteria.
Theta Join(θ) The general case of JOIN operation is called a Theta join. It is denoted by symbol θ.
Natural Join(⋈) Natural join can only be performed if there is a common attribute between the relations.
Outer Join In an outer join, along with tuples that satisfy the matching criteria.
Left Outer Join( ) In the left outer join, operation allows keeping all tuple in the left relation.
Right Outer join( ) In the right outer join, operation allows keeping all tuple in the right relation.
Full Outer Join( ) In a full outer join, all tuples from both relations are included in the result irrespective of the
matching condition.