Unit2 Relational Algebra

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

UNIT 2

Relational Query Language


Relational Algebra

 Relational algebra is a procedural query language that works on


relational model. The purpose of a query language is to retrieve data
from database or perform various operations such as insert, update,
delete on the data. When I say that relational algebra is a procedural
query language, it means that it tells what data to be retrieved and
how to be retrieved.
 On the other hand relational calculus is a non-procedural query
language, which means it tells what data to be retrieved but doesn’t
tell how to retrieve it.
Types of operations in relational algebra

 We have divided these operations in two categories:


1. Basic Operations
2. Derived Operations
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 (⟕, ⟕, ⟕)
3. Intersection (∩)
4. Division (÷)
Basic Operations
Select Operator (σ)

 Select Operator is denoted by sigma (σ) and it is used to find the


tuples (or rows) in a relation (or table) which satisfy the given
condition.

 Syntax of Select Operator (σ)

σ Condition/Predicate(Relation/Table name)
Select Operator (σ) Example
 Select a customer who’s city name is “agra”.

 Query:
σ Customer_City="Agra" (CUSTOMER)
Output
Project Operator (Π)

 Project operator is denoted by Π symbol and it is used to select


desired columns (or attributes) from a table (or relation).
 Project operator in relational algebra is similar to the Select
statement in SQL.
 Duplicate entities are eliminated.
Syntax of Project Operator (Π)

 Π column_name1, column_name2, ....,


column_nameN(table_name)
Project Operator (Π) Example

 In this example, we have a table CUSTOMER with three columns,


we want to fetch only two columns of the table, which we can do
with the help of Project Operator Π.
Query

 Π Customer_Name, Customer_City (CUSTOMER)


Output
Union Operator ( ∪ )

 Union operator is denoted by ∪ symbol and it is used to select all


the rows (tuples) from two tables (relations).
 Lets discuss union operator a bit more. Lets say we have two
relations R1 and R2 both have same columns and we want to select
all the tuples(rows) from these relations then we can apply the union
operator on these relations.
 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.
Syntax of Union Operator (∪)

 table_name1 ∪ table_name2
Union Operator (∪) Example
Query

 Π Student_Name (COURSE) ∪ Π Student_Name (STUDENT)


Output
Note:

 As you can see there are no duplicate names present in the


output even though we had few common names in both
the tables, also in the COURSE table we had the duplicate
name itself.
 If relations don't have the same set of attributes, then
the union of such relations will result in NULL.
Intersection Operator ( ∩ )

 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.
Syntax of Intersection Operator (∩)

 table_name1 ∩ table_name2
Intersection Operator (∩) Example

 Lets take the same example that we have taken above.


 Query:
Π Student_Name (COURSE) ∩ Π Student_Name (STUDENT)

 Output:
Set Difference (-)

 Set Difference is denoted by – symbol. 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.
 Syntax of Set Difference (-)
table_name1 - table_name2
Set Difference (-) Example

 Lets take the same tables COURSE and STUDENT that we have
seen above.
Lets write a query to select those student names that are present in STUDENT
table but not present in COURSE table.

 Query:
Π Student_Name (STUDENT) - Π Student_Name
(COURSE)
Output
Note

 Just like union, the set difference also comes with the
exception of the same set of attributes in both relations.
Cartesian product (X)

 Cartesian Product is denoted by X symbol.


 Lets say we have two relations R1 and R2 then the cartesian product
of these two relations (R1 X R2) would combine each tuple of first
relation R1 with the each tuple of second relation R2.
 Syntax of Cartesian product (X)
R1 X R2
Cartesian product (X) Example
 Query:
 Lets find the cartesian product of table R and S.
 R X S
Output
Note

 The number of rows in the output will always be the cross product
of number of rows in each table. In our example table 1 has 3 rows
and table 2 has 3 rows so the output has 3×3 = 9 rows.
Rename (ρ)

 Rename (ρ) operation can be used to rename a relation or an


attribute of a relation.
 Rename operation is denoted by "Rho"(ρ).
 Rename (ρ) Syntax:
ρ(new_relation_name, old_relation_name)
Rename (ρ) Example

 Lets say we have a table customer, we are fetching customer names


and we are renaming the resulted relation to CUST_NAMES.
 Query:
 ρ(CUST_NAMES, Π(Customer_Name)(CUSTOMER))
Output
Takeaway

• Select (σ) is used to retrieve tuples(rows) based on certain


conditions.
• Project (∏) is used to retrieve attributes(columns) from the relation.
• Union (∪) is used to retrieve all the tuples from two relations.
• Set Difference (-) is used to retrieve the tuples which are present in
R but not in S(R-S).
• Cartesian product (X) is used to combine each tuple from first
relation with each tuple from second relation.
• Rename (ρ) is used to rename the output relation.
Task

 Consider the following schema


Flight( FlightNo: Number, FlightName: String, Source:
String, Destination: String)
 Display the information of the flights whose FlightNo is greater
than 1105.
 Display the FlightName for which the Source is London
 Display the information of all the flight going (Destination)to
Singapore

You might also like