0% found this document useful (0 votes)
48 views89 pages

Query Is A Question or Requesting Information

The document outlines the fundamental concepts and operators of relational algebra, including selection, projection, union, and join operations. It explains various types of joins such as natural, outer, and equi joins, along with their SQL syntax and examples. Additionally, it discusses the importance of relational algebra in querying and manipulating relational databases, serving as a foundation for SQL operations.

Uploaded by

Putta Swamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views89 pages

Query Is A Question or Requesting Information

The document outlines the fundamental concepts and operators of relational algebra, including selection, projection, union, and join operations. It explains various types of joins such as natural, outer, and equi joins, along with their SQL syntax and examples. Additionally, it discusses the importance of relational algebra in querying and manipulating relational databases, serving as a foundation for SQL operations.

Uploaded by

Putta Swamy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 89

Relational Algebra

Operators in Relational Algebra:

There are a few operators that are used in relational algebra -


1. Select (sigma): Returns rows of the input relation that satisfy the provided predicate. It is
unary Operator means requires only one operand.
2. Projection (ℼ): Show the list of those attribute which we desire to appear and rest other
attributes are eliminated from the table. It seperates the table vertically.
3. Set Difference (-): It returns the difference between two relations . If we have two relations R
and S them R-S will return all the tuples (row) which are in relation R but not in Relation S ,
It is binary operator.
4. Cartesian Product (X): Combines every tuple (row) of one table with every tuple (row) in
other table ,also referred as cross Product . It is a binary operator.
5. Union (U): Outputs the union of tuples from both the relations. Duplicate tuples are
eliminated automatically. It is a binary operator means it require two operands.
Unary Operators in Relational Algebra
All those Operators that operate on a single operand are known as unary operators. There are
various types of unary operators in relational algebra.
Types of Unary Operators
Unary operators are of three types
1. Projection Operator
2. Selection Operator
3. Rename Operator
3.

3. Join Operations:
A Join operation combines related tuples from different tables. Join
operators can apply if and only if a given join condition is satisfied. Join operator denoted by ⋈.
For join operation, there must be some commonality in the two tables. As in the below tables,
EMP_ID is common in both tables.
Join operation is used when we need to access data from more than one table.
In the following tables, we need Emp_Name and EMP_Salary. So it requires to join the
EMPLOYEE and SALARY tables.
Example: Suppose EMPLOYEE and SALARY Tables
Table: EMPLOYEE

Table: SALARY

Apply Join Operation (EMPLOYEE ⋈ SALARY)

Types of JOIN Operator


1. Natural JOIN
2. OUTER JOIN
a. LEFT OUTER JOIN
b. RIGHT OUTER JOIN
c. FULL OUTER JOIN
3. Equi JOIN
In the next lecture, we will cover all the above Types of Joins.

Natural Join is the cross product of two tables where it finds and returns the matching tuples.
Condition: Common columns (one as a primary key and the other as a foreign key) for joining
must have the same name.
Keep in mind: In natural join, columns with the same name of both joining tables will appear
once only.
Example:
Suppose the following two tables, Student and Student_Marks,
Table: Student

Table: Student_Marks

To get all the unique columns from student and student_marks tables, the following SQL
statement can be used
SQL Code:
SELECT * FROM Student NATURAL JOIN Student_Marks;
Output for the above query is given under

Concept: Natural join is a select condition and cross-product.


How Does Natural Join Actually Work?
Example
Requirement: Select STD_Name from both tables (Student and Student_Marks) where both
tables have common Std_ID.
SQL Query:
SELECT Std_Name FROM Student NATURAL JOIN Student_Marks;

1. First, see Cross product of Student and Student_Marks


Common Tupples where Std_ID of Student table = Std_ID of Student_Marks table is given
below.

As we know, natural Join skips the duplicate common columns. So, one Std_ID is skipped, as
given below

2. Now Apply Condition


According to Condition, Select Just Student Name from the above diagram, and then the
result is given below.
Keep in mind: If similar results are required as in Natural Join, but common columns do not
hold the same names, then the following SQL query can be used instead of Natural Join.
SQL: SELECT Std_Name FROM Student, Student_Marks Where Student.Std_ID=
Std_Marks.Std_ID;
Student, Student_Marks is a cross-product of two tables above the SQL query.
 After Where Clause (Student.Std_ID= Std_Marks.Std_ID;) is a condition.
Outer Join And Its Types
A Natural join finds and returns matching data from two tables, but an outer -join finds and
returns some matching data and some dissimilar data as well from tables.
Conditions for outer join are similar to those in natural join, where Common columns for
joining must have the same name.
Types of Outer Join
 Left outer Join
 Right Outer Join
 Full Outer Join
Consider the following two tables (Employee and Department) and explain all three types

1. Left Outer Join


Left outer join finds and returns all matching rows from both tables and also returns the rows
that are present in the left table but not present in the right table.
SQL Syntax:
SELECT * From Table1 LEFT OUTER JOIN TABLE2 ON <Condition> WHERE
<CONDITION>.
Symbolic Representation

Consider the following SQL query and its results in the following table.
SQL
SELECT emp_no, emp_name, dept_name, loc From employee LEFT OUTER JOIN
Department ON employee.dept_no =department.dept_no
Output of the above SQL

2. RIGHT OUTER JOIN


Right outer join finds and returns all matching rows from both tables and also returns the
rows that are present in the Right table but not present in the left table.
SQL Syntax:
SELECT * From Table1 RIGHT OUTER JOIN TABLE2 ON <Condition> WHERE
<CONDITION>.
Symbolic Representation
Consider the following SQL query and its results in the following table.
SQL
SELECT emp_no, emp_name, dept_name, loc From employee RIGHT OUTER JOIN
TABLE2 ON employee.dept_no =department.dept_no
Output of the above SQL

3. FULL OUTER JOIN


Full outer join finds and returns all matching and mismatching rows from both tables.
SQL Syntax:
SELECT * From Table1 FULL OUTER JOIN TABLE2 ON <Condition> WHERE
<CONDITION>.
Symbolic Representation
Consider the following SQL query and its results in the following table.
SQL
SELECT emp_no, emp_name, dept_name, loc From employee FULL OUTER JOIN
TABLE2 ON employee.dept_no =department.dept_no
Output of the above SQL

EQUI JOIN
Equi Join is also known as an inner join. It is the most common join. It finds and returns the
values which are based on equality conditions. Equi uses the comparison operator (=).
Note: Column names for the primary and foreign keys may or may not be the same because
we use the “Where” clause in Equi-Join.
Syntax
Select * from Employee, Department where <Condition>
Example:
It is necessary to find the Employee name (employee table) who is working in a department
(Department table) having the location same as their address. Employee and department tables
are in the following diagram
SQL
SQL for the above problem is given below
SELECT emp_no, emp_name, dept_no From Employee ,Department where
employee.EMP_no =department.dept_Emp_no AND employee.emp.address
=department.dept_location.
Output of the above SQL
Take the cross product of both tables and calculate all those tuples where employees.EMP_no
=department.dept_Emp_no AND employee. emp.address =department.dept_location. So, the
result will be as follows

Restrict Operator (σ)


This operator retrieves rows from a relation that satisfies a specified condition (predicate). It
acts like a WHERE clause in SQL. The selection operator is denoted by the Greek letter sigma
(σ).
Select operation chooses the subset of tuples from the relation that satisfies the given condition
mentioned in the syntax of selection. The selection operation is also known as horizontal
partitioning since it partitions the table or relation horizontally.
Example-1

Student(Roll, Name, Class, Fees, Team)


Q1: Select all the students of Team A :
Ans: σ Team = ‘A’ (Student)
Example-2
Q1: Select all the students of department ECE whose fees are greater than or equal to 10,000
and belong to a Team other than A.
Ans: σ Fees >= 10000(σTeam != ‘A’ (Student))
Projection Operator (π)
Projection Operation: The projection operation in relational algebra is used to extract specific
columns (attributes) from a relation while eliminating duplicates. It results in a new relation
that contains only the selected attributes. The projection operation is denoted by the Greek
letter π (pi).
For example
Class teacher table
Q: Display Dept names of the class teachers
Ans:

Join Operator (⋈)


The join operator is a fundamental operation used to combine data from two or more relations
(tables) based on a common attribute. The join operation creates a new relation by matching
rows from different relations that satisfy a specified condition. The result of a join operation is
a relation that combines attributes from the participating relations.
For example
Q: Display the subject that the students do in one table?
Ans:

Division Operator (÷)


The division operator is used to express a certain type of query involving two relations (tables):
it selects rows from one relation that matches a condition with every row in another relation.
The result of the division operation is a relation that contains attributes from the first relation
minus attributes from the second relation.
Cartesian Product (×)
the Cartesian product (also known as a cross join) is an operation that combines every row
from one relation (table) with every row from another relation, resulting in a new relation.
This operation is used to create all possible combinations of rows between two relations.
The Cartesian product operation is denoted using the × symbol. The basic syntax for the
Cartesian product operation is:
Table1 × Table2
For example
Q: Display all the subjects Nimal, Mala, and Kamal doing
Ans:

Student X Subject
Union (∪)
The union operation is used to combine the rows of two relations (tables) into a single
relation, eliminating duplicate rows. The result of the union operation contains all the distinct
rows from both input relations.
Notice that the duplicate row with values (2, Y) appears only once in the result, as the union
operation eliminates duplicates.
The union operation is a set-based operation that allows you to combine rows from different
tables while ensuring that each row is included only once in the result. It’s one of the core
operations in relational algebra and is widely used in SQL queries to combine data from
multiple tables.
Example for Union
For example

Q: StudentSubject U StudentMark
Ans: (π Student_ID (StudentSubject)) U (π Student_ID (StudentMark))

(π Student_ID (StudentSubject)) U (π Student_ID (StudentMark))


Intersection (∩)
In relational algebra, an intersection operation is used to combine two relations
(tables) and retrieve the common rows that exist in both relations. The result of the
intersection operation is a new relation that contains only the rows that are present in both
input relations.
The intersection operation is often denoted using the ∩ symbol.

Select operation:
It displays the records that satisfy a condition. It is denoted by sigma (σ) and is
a horizontal subset of the original relation.
Syntax: Its syntax is as follows −
σcondition(table name)
Example
Consider the student table given below −
Regno Branch Section
1 CSE A
2 ECE B
3 CIVIL B
4 IT A
Query: display all the records of student table, we will use the following command:
σ(student)
In addition to this, when we have to display all the records of CSE branch in student table, we
will use the following command: −
σbranch=cse(student)
Hence, the result will be as follows −
RegN Branch Section
o
1 CSE A
To display all the records in student tables whose regno>2, we will use the below mentioned
command −
σRegNo>2(student)
The output will be as follows −
RegN Branch Section
o
3 CIVIL B
4 IT A
To display the record of ECE branch section B students, use the given command −
σbranch=ECE ^ section=B(student)
To display the records of section B CSE and IT branch, use the following command −
σSection=B ^ Branch=cse ∨ branch=IT(student)
Consider the EMPLOYEE TABLE as another example to know about selection operations.
Retrieve information about those employees whose salary is greater than 20,000
 If one condition is specified then, we can use the following command −
σ salary > 20,000 (emp)
 If more than one condition specified in the query then ( AND: ^, OR: ∨ , Not:#, equal: =,
>, <, >=, <=)
Relational operator will be used to combine the multiple conditions into a single statement.
Example − In order to retrieve the information of those employee whose salary > 20,000 and
working in HOD and Dept no is 20, we can use the following command −
σ salary > 20,000 ^LOC=HOD ^Deptno=20(emp)
Introduction of Relational Algebra in DBMS
Relational Algebra is a formal language used to query and manipulate relational databases,
consisting of a set of operations like selection, projection, union, and join. It provides a
mathematical framework for querying databases, ensuring efficient data retrieval and
manipulation. Relational algebra serves as the mathematical foundation for query SQL.
 It provides a clear, structured approach for formulating database queries.
 Helps in understanding and optimizing query execution plans for better performance.
 SQL queries are based on relational algebra operations, making it essential for learning
SQL.
 Enables complex queries, like joins and nested queries, that are critical for working with
large datasets.
Key Concepts in Relational Algebra:
 Relations: In relational algebra, a relation is a table that consists of rows and columns,
representing data in a structured format. Each relation has a unique name and is made
up of tuples.
 Tuples: A tuple is a single row in a relation, which contains a set of values for each
attribute. It represents a single data entry or record in a relational table.
 Attributes: Attributes are the columns in a relation, each representing a specific
characteristic or property of the data. For example, in a “Students” relation, attributes
could be “Name”, “Age”, and “Grade”.
 Domains: A domain is the set of possible values that an attribute can have. It defines the
type of data that can be stored in each column of a relation, such as integers, strings, or
dates.
Operators in Relational Algebra
Relational algebra consists of various operators that help us fetch and manipulate data from
relational tables in the database to perform certain operations on relational data. The
fundamental operators in relational algebra, such as selection, projection, and join, are essential
for querying and transforming data efficiently within a relational database.

Operators in Relational Algebra


Basic Operators
Basic operators are fundamental operations that include selection (σ), projection (π), union
(U), set difference (−), Cartesian product (×), and rename (ρ). These operators are used to
manipulate and retrieve data from relational databases.
1. Selection(σ): Selection Operation is basically used to filter out rows from a given table based
on certain given condition. It basically allows you to retrieve only those rows that match the
condition as per condition passed during SQL Query.
Example:
Consider the relation R as follows:
A B C
1 2 4
2 2 3
3 2 3
4 3 4
For the above relation,
σ(c>3)(R) will select the tuples which have c more than 3.
Output Table:
A B C
1 2 4
4 3 4
Note: The selection operator only selects the required tuples but does not display them. For
display, the data projection operator is used.
2. Projection(π): While Selection operation works on rows, similarly projection operation of
relational algebra works on columns. It basically allows you to pick specific columns from a
given relational table based on the given condition and ignoring all the other remaining
columns.
Example: Suppose we want columns B and C from Relation R.
π(B,C)(R) will show following columns.
B C
2 4
2 3
3 4
Note: By Default, projection removes duplicate data.
3. Union(U): Union Operator is basically used to combine the results of two queries into a
single result. The only condition is that both queries must return same number of columns with
same data types. Union operation in relational algebra is the same as union operation in set
theory.
Example: Consider the following table of Students having different optional subjects in their
course.
FRENCH
Student_Name Roll_Number
Ram 01
Mohan 02
Vivek 13
Geeta 17
GERMAN
Student_Name Roll_Number
Vivek 13
Geeta 17
Shyam 21
Rohan 25
For the above relations, the query:
π(Student_Name)(FRENCH) U π(Student_Name)(GERMAN)
will give us output table:
Student_Name
Ram
Mohan
Vivek
Geeta
Shyam
Rohan
Note: The only constraint in the union of two relations is that both relations must have the same
set of Attributes.
4. Set Difference(-): Set difference basically provides the rows that are present in one table, but
not in another tables. Set Difference in relational algebra is the same set difference operation as
in set theory.
Example: From the above table of FRENCH and GERMAN, Set Difference is used as follows:
π(Student_Name)(FRENCH) – π(Student_Name)(GERMAN)
Student_Name
Ram
Mohan
Note: The only constraint in the Set Difference between two relations is that both relations must
have the same set of Attributes.
5. Rename(ρ): Rename operator basically allows you to give a temporary name to a specific
relational table or to its columns. It is very useful when we want to avoid ambiguity, especially in
complex Queries. Rename is a unary operation used for renaming attributes of a relation.
Table R:
A B C
1 2 4
2 2 3
3 2 3
4 3 4
ρ(D/B)R will rename the attribute ‘B’ of the relation by ‘D”.
Output Table:
A D C
1 2 4
2 2 3
3 2 3
4 3 4
6. Cross Product(X): Cartesian product Operator combines every row of one table with every
row of another table, producing all the possible combination. It’s mostly used as a precursor to
more complex operation like joins.
Example: Cross-product between two relations. Let’s say A and B, so the cross product between
A X B will result in all the attributes of A followed by each attribute of B. Each record of A will
pair with every record of B.
Relation A:
Name Age Sex
Ram 14 M
Sona 15 F
Kim 20 M
Relation B:
ID Course
1 DS
2 DBMS
Relation A X B:
Name Age Sex ID Course
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Sona 15 F 2 DBMS
Kim 20 M 1 DS
Kim 20 M 2 DBMS
Note: If A has ‘n’ tuples and B has ‘m’ tuples then A X B will have ‘n*m’ tuples.
Derived Operators
Derived operators are built using basic operators and include operations like join, intersection,
and division. These operators help perform more complex queries by combining basic operations
to meet specific data retrieval needs.
1. Join Operators: Join operations in relational algebra combine data from two or more
relations based on a related attribute, allowing for more complex queries and data retrieval.
Types of Joins in Relational Algebra:
(a) Inner Join
An inner join combines rows from two relations based on a matching condition and only returns
rows where there is a match in both relations. If a record in one relation doesn’t have a
corresponding match in the other, it is excluded from the result. This is the most common type of
join.
 Conditional Join: A conditional join is an inner join where the matching condition can
involve any comparison operator like equals (=), greater than (>), etc.
Example:
Joining Employees and Departments on DepartmentID where Salary > 50000 will return
employees in departments with a salary greater than 50,000.
 Equi Join: An equi join is a type of conditional join where the condition is specifically
equality (=) between columns from both relations.
Example:
Joining Customers and Orders on CustomerID where both relations have this column,
returning only matching records.
 Natural Join: A natural join automatically combines relations based on columns with the
same name and type, removing duplicate columns in the result. It’s a more efficient way of
joining.
Example:
Joining Students and Enrollments where StudentID is common in both, and the result
contains only unique columns.
(b) Outer Join
An outer join returns all rows from one relation, and the matching rows from the other relation.
If there is no match, the result will still include all rows from the outer relation with NULL values
in the columns from the unmatched relation.
 Left Outer Join: A left outer join returns all rows from the left relation and the matching
rows from the right relation. If there is no match, the result will include NULL values for
the right relation’s attributes.
Example:
Joining Employees with Departments using a left outer join ensures all employees are
listed, even those who aren’t assigned to any department, with NULL values for the
department columns.
 Right Outer Join: A right outer join returns all rows from the right relation and the
matching rows from the left relation. If no match exists, the left relation’s columns will
contain NULL values.
Example:
Joining Departments with Employees using a right outer join includes all departments,
even those with no employees assigned, filling unmatched employee columns with NULL.
 Full Outer Join: A full outer join returns all rows when there is a match in either the left
or right relation. If a row from one relation does not have a match in the
other, NULL values are included for the missing side.
Example:
Joining Customers and Orders using a full outer join will return all customers and
orders, even if there’s no corresponding order for a customer or no customer for an order.
Learn more about Joins here.
2. Set Intersection(∩) : Set Intersection basically allows to fetches only those rows of data that
are common between two sets of relational tables. Set Intersection in relational algebra is the
same set intersection operation in set theory.
Example: Consider the following table of Students having different optional subjects in their
course.
Relation FRENCH:
Student_Name Roll_Number
Ram 01
Mohan 02
Vivek 13
Geeta 17
Relation GERMAN:
Student_Name Roll_Number
Vivek 13
Geeta 17
Shyam 21
Rohan 25
From the above table of FRENCH and GERMAN, the Set Intersection is used as follows:
π(Student_Name)(FRENCH ∩ π(Student_Name)(GERMAN)
Output Table:
Student_Name
Vivek
Geeta
Note: The only constraint in the Set Difference between two relations is that both relations must
have the same set of Attributes.
3. Division (÷): Division is used to find tuples in one relation that are related to all tuples in
another relation. It’s typically used for “for all” queries.
Example: We have two tables
Student_Course (Dividend Table):
Student_ID Course_ID
101 C1
101 C2
102 C1
103 C1
103 C2
Course (Divisor Table):
Course_ID
C1
C2
Query is to find students who are enrolled in all courses listed in the Course table. In this case,
students must be enrolled in both C1 and C2.
Student_Course(Student_ID, Course_ID)÷ Course(Course_ID)
Output Table:
Student_ID
101
103

What is Relational Algebra in DBMS?


Relational Algebra came in 1970 and was given by Edgar F. Codd (Father of DBMS). It is also
known as Procedural Query Language(PQL) as in PQL, a programmer/user has to mention two
things, "What to Do" and "How to Do".
Suppose our data is stored in a database, then relational algebra is used to access the data from
the database.
The First thing is we have to access the data, this needs to be specified in the query as "What to
Do", but we have to also specify the method/procedure in the query that is "How to Do" or how
to access the data from the database.
Types of Relational Operations in DBMS
In Relational Algebra, we have two types of Operations.
 Basic Operations
 Derived Operations
Applying these operations over relations/tables will give us new relations as output.
Basic Operations
Six fundamental operations are mentioned below. The majority of data retrieval operations are
carried out by these. Let's know them one by one.
But, before moving into detail, let's have two tables or we can say relations STUDENT(ROLL,
NAME, AGE) and EMPLOYEE(EMPLOYEE_NO, NAME, AGE) which will be used in the
below examples.
STUDENT
ROLL NAME AGE
1 Aman 20
2 Atul 18
3 Baljeet 19
ROLL NAME AGE

4 Harsh 20
5 Prateek 21
6 Prateek 23
EMPLOYEE
EMPLOYEE_NO NAME AGE
E-1 Anant 20
E-2 Ashish 23
E-3 Baljeet 25
E-4 Harsh 20
E-5 Pranav 22
Select (σ)
Select operation is done by Selection Operator which is represented by "sigma"(σ). It is used to
retrieve tuples(rows) from the table where the given condition is satisfied. It is a unary
operator means it requires only one operand.
Notation : σ p(R)
Where σ is used to represent SELECTION
R is used to represent RELATION
p is the logic formula
Let's understand this with an example:
Suppose we want the row(s) from STUDENT Relation where "AGE" is 20
σ AGE=20 (STUDENT)
This will return the following output:
ROLL NAME AGE
1 Aman 20
4 Harsh 20
Project (∏)
Project operation is done by Projection Operator which is represented by "pi"(∏). It is used to
retrieve certain attributes(columns) from the table. It is also known as vertical partitioning as it
separates the table vertically. It is also a unary operator.
Notation : ∏ a(r)
Where ∏ is used to represent PROJECTION
r is used to represent RELATION
a is the attribute list
Let's understand this with an example:
Suppose we want the names of all students from STUDENT Relation.
∏ NAME(STUDENT)
This will return the following output:
NAME
Aman
Atul
Baljeet
Harsh
Prateek
As you can see from the above output it eliminates duplicates.
For multiple attributes, we can separate them using a ",".
∏ ROLL,NAME(STUDENT)
Above code will return two columns, ROLL and NAME.
ROLL NAME
1 Aman
2 Atul
3 Baljeet
4 Harsh
5 Prateek
6 Prateek
Union (∪)
Union operation is done by Union Operator which is represented by "union"(∪). It is the same
as the union operator from set theory, i.e., it selects all tuples from both relations but with the
exception that for the union of two relations/tables both relations must have the same set of
Attributes. It is a binary operator as it requires two operands.
Notation: R ∪ S
Where R is the first relation
S is the second relation
If relations don't have the same set of attributes, then the union of such relations will result
in NULL.
Let's have an example to clarify the concept:
Suppose we want all the names from STUDENT and EMPLOYEE relation.
∏ NAME(STUDENT) ∪ ∏ NAME(EMPLOYEE)
NAME
Aman
Anant
Ashish
Atul
Baljeet
Harsh
Pranav
Prateek
As we can see from the above output it also eliminates duplicates.
Set Difference (-)
Set Difference as its name indicates is the difference between two relations (R-S). It is denoted by
a "Hyphen"(-) and it returns all the tuples(rows) which are in relation R but not in relation S. It
is also a binary operator.
Notation : R – S
Where R is the first relation
S is the second relation
Just like union, the set difference also comes with the exception of the same set of attributes in
both relations.
Let's take an example where we would like to know the names of students who are in STUDENT
Relation but not in EMPLOYEE Relation.
∏ NAME(STUDENT) - ∏ NAME(EMPLOYEE)
This will give us the following output:
NAME
Aman
Atul
Prateek
Cartesian product (X)
Cartesian product is denoted by the "X" symbol. Let's say we have two relations R and S.
Cartesian product will combine every tuple(row) from R with all the tuples from S. I know it
sounds complicated, but once we look at an example, you'll see what I mean.
Notation: R X S
Where R is the first relation
S is the second relation
As we can see from the notation it is also a binary operator.
Let's combine the two relations STUDENT and EMPLOYEE.
STUDENT X EMPLOYEE
ROLL NAME AGE EMPLOYEE_NO NAME AGE
1 Aman 20 E-1 Anant 20
1 Aman 20 E-2 Ashish 23
1 Aman 20 E-3 Baljeet 25
1 Aman 20 E-4 Harsh 20
1 Aman 20 E-5 Pranav 22
2 Atul 18 E-1 Anant 20
2 Atul 18 E-2 Ashish 23
2 Atul 18 E-3 Baljeet 25
2 Atul 18 E-4 Harsh 20
2 Atul 18 E-5 Pranav 22
. . . And so on.
Rename (ρ)
Rename operation is denoted by "Rho"(ρ). As its name suggests it is used to rename the output
relation. Rename operator too is a binary operator.
Notation: ρ(R,S)
Where R is the new relation name
S is the old relation name
Let's have an example to clarify this
Suppose we are fetching the names of students from STUDENT relation. We would like to rename
this relation as STUDENT_NAME.
ρ(STUDENT_NAME,∏ NAME(STUDENT))
STUDENT_NAME
NAME
Aman
Atul
Baljeet
Harsh
Prateek
As you can see, this output relation is named "STUDENT_NAME".
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 the first relation with each
tuple from the second relation.
 Rename (ρ) is used to rename the output relation.
Derived Operations
Also known as extended operations, these operations can be derived from basic operations and
hence named Derived Operations. These include three operations: Join Operations, Intersection
operations, and Division operations.
Let's study them one by one.
Join Operations
Join Operation in DBMS are binary operations that allow us to combine two or more relations.
They are further classified into two types: Inner Join, and Outer Join.
First, let's have two relations EMPLOYEE consisting
of E_NO, E_NAME, CITY and EXPERIENCE. EMPLOYEE table contains employee's
information such as id, name, city, and experience of employee(In Years). The other relation
is DEPARTMENT consisting of D_NO, D_NAME, E_NO and MIN_EXPERIENCE.
DEPARTMENT table defines the mapping of an employee to their department. It contains
Department Number, Department Name, Employee Id of the employee working in that
department, and the minimum experience required(In Years) to be in that department.
EMPLOYEE
E_NO E_NAME CITY EXPERIENCE
E-1 Ram Delhi 04
E-2 Varun Chandigarh 09
E-3 Ravi Noida 03
E-4 Amit Bangalore 07
DEPARTMENT
D_NO D_NAME E_NO MIN_EXPERIENCE
D-1 HR E-1 03
D-2 IT E-2 05
D-3 Marketing E-3 02
Also, let's have the Cartesian Product of the above two relations. It will be much easier to
understand Join Operations when we have the Cartesian Product.
E_N E_NAM CITY EXPERIENC D_N D_NAM E_N MIN_EXPERIEN
O E E O E O CE
E-1 Ram Delhi 04 D-1 HR E-1 03
E-1 Ram Delhi 04 D-2 IT E-2 05
E-1 Ram Delhi 04 D-3 Marketin E-3 02
g
E-2 Varun Chandigar 09 D-1 HR E-1 03
h
E-2 Varun Chandigar 09 D-2 IT E-2 05
h
E-2 Varun Chandigar 09 D-3 Marketin E-3 02
h g
E-3 Ravi Noida 03 D-1 HR E-1 03
E-3 Ravi Noida 03 D-2 IT E-2 05
E-3 Ravi Noida 03 D-3 Marketin E-3 02
g
E-4 Amit Bangalore 07 D-1 HR E-1 03
E-4 Amit Bangalore 07 D-2 IT E-2 05
E-4 Amit Bangalore 07 D-3 Marketin E-3 02
g
Inner Join
When we perform Inner Join, only those tuples returned that satisfy the certain condition. It is
also classified into three types: Theta Join, Equi Join and Natural Join.
Theta Join (θ)
Theta Join combines two relations using a condition. This condition is represented by the
symbol "theta"(θ). Here conditions can be inequality conditions such as >,<,>=,<=, etc.
Notation : R ⋈θ S
Where R is the first relation
S is the second relation
Let's have a simple example to understand this.
Suppose we want a relation where EXPERIENCE from EMPLOYEE >= MIN_EXPERIENCE
from DEPARTMENT.
EMPLOYEE⋈θ EMPLOYEE.EXPERIENCE>=DEPARTMENT.MIN_EXPERIENCE
DEPARTMENT
E_N E_NAM CITY EXPERIENC D_N D_NAM E_N MIN_EXPERIEN
O E E O E O CE
E-1 Ram Delhi 04 D-1 HR E-1 03
E-1 Ram Delhi 04 D-3 Marketin E-3 02
g
E-2 Varun Chandigar 09 D-1 HR E-1 03
h
E-2 Varun Chandigar 09 D-2 IT E-2 05
h
E-2 Varun Chandigar 09 D-3 Marketin E-3 02
h g
E-3 Ravi Noida 03 D-1 HR E-1 03
E-3 Ravi Noida 03 D-3 Marketin E-3 02
g
E-4 Amit Bangalore 07 D-1 HR E-1 03
E-4 Amit Bangalore 07 D-2 IT E-2 05
E-4 Amit Bangalore 07 D-3 Marketin E-3 02
g
Check the Cartesian Product, if in any tuple/row EXPERIENCE >= MIN_EXPERIENCE then
insert this tuple/row in output relation.
Equi Join
Equi Join is a special case of theta join where the condition can only
contain **equality(=)** comparisons.
A non-equijoin is the inverse of an equi join, which occurs when you join on a condition other
than "=".
Let's have an example where we would like to join EMPLOYEE and DEPARTMENT relation
where E_NO from EMPLOYEE = E_NO from DEPARTMENT.
EMPLOYEE ⋈EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT
E_N E_NAM CITY EXPERIENC D_N D_NAM E_N MIN_EXPERIEN
O E E O E O CE
E-1 Ram Delhi 04 D-1 HR E-1 03
E-2 Varun Chandigar 09 D-2 IT E-2 05
h
E-3 Ravi Noida 03 D-3 Marketin E-3 02
g
Check Cartesian Product, if the tuple contains same E_NO, insert that tuple in the output
relation
Natural Join (⋈)
A comparison operator is not used in a natural join. It does not concatenate like a Cartesian
product. A Natural Join can be performed only if two relations share at least one common
attribute. Furthermore, the attributes must share the same name and domain.
Natural join operates on matching attributes where the values of the attributes in both relations
are the same and remove the duplicate ones.
Preferably Natural Join is performed on the foreign key.
Notation : R ⋈ S
Where R is the first relation

S is the second relation


Let's say we want to join EMPLOYEE and DEPARTMENT relation with E_NO as a common
attribute.
Notice, here E_NO has the same name in both the relations and also consists of the same
domain, i.e., in both relations E_NO is a string.
EMPLOYEE ⋈ DEPARTMENT
E_N E_NAM CITY EXPERIENC D_N D_NAM MIN_EXPERIENC
O E E O E E
E-1 Ram Delhi 04 D-1 HR 03
E-2 Varun Chandigar 09 D-2 IT 05
h
E-3 Ravi Noida 03 D-3 Market

Relational database systems are expected to be equipped with a query language that can assist
its users to query the database instances. There are two kinds of query languages − relational
algebra and relational calculus.
Relational Algebra
Relational algebra is a procedural query language, which takes instances of relations as input
and yields instances of relations as output. It uses operators to perform queries. An operator can
be either unary or binary. They accept relations as their input and yield relations as their output.
Relational algebra is performed recursively on a relation and intermediate results are also
considered relations.
The fundamental operations of relational algebra are as follows −
 Select
 Project
 Union
 Set different
 Cartesian product
 Rename
We will discuss all these operations in the following sections.
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic formula
which may use connectors like and, or, and not. These terms may use relational operators like −
=, ≠, ≥, < , >, ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those books
published after 2010.
Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.
Union Operation (∪)
It performs binary union between two given relations and is defined as −
r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the following conditions must hold −
 r, and s must have the same number of attributes.
 Attribute domains must be compatible.
 Duplicate tuples are automatically eliminated.
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book or an article or both.
Set Difference (−)
The result of set difference operation is tuples, which are present in one relation but are not in
the second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
Cartesian Product (Χ)
Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'tutorialspoint'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by tutorialspoint.
Rename Operation (ρ)
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 ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Additional operations are −
 Set intersection
 Assignment
 Natural join

Table of contents

 What is Relational Algebra in DBMS


 Types of Relational Algebra in DBMS
 Relational Algebra Queries in DBMS
 Comparision of Relational Algebra with SQL
 Applications of Relational Algebra in DBMS
 Relational Calculus and its Relationship with Relational Algebra
 Conclusion
 Frequently Asked Questions

Relational Algebra in DBMS forms the foundation for querying databases using
algebraic operations. It plays a crucial role in query formulation and optimization,
allowing users to manipulate data stored in relational databases efficiently. By applying
a set of well-defined operations, relational algebra ensures that we can query, update,
and combine data from multiple relations, making it a key component of database
management systems (DBMS).

In this article, we will explore the relational algebra importance, types, and queries in
DBMS.

What is Relational Algebra in


DBMS?
Relational algebra in DBMS refers to a procedural query language used to query the
database. It consists of a set of relational algebra operations in DBMS, such as
selection, projection, union, and more, that can be applied to relations to retrieve or
manipulate data.

The result of these operations is typically a relation, which can be further processed with
other operations. This mathematical approach helps in understanding the structure of
queries and optimizing them for better performance in DBMS environments.

Importance of Relational Algebra in DBMS


Relational algebra is a foundational concept in Database Management Systems
(DBMS) as it provides a set of operations for querying and manipulating data in a
relational database. These operations, such as selection, projection, union, and join,
form the basis for SQL and enable the retrieval of data in a structured and efficient
manner. By using relational algebra, database queries become more precise and can
be optimized for performance.

The importance of relational algebra lies in its ability to define clear, mathematical
operations on relations, ensuring data consistency and integrity. It helps in expressing
complex queries in a simplified way and allows for the optimization of query execution.
Understanding relational algebra enhances the ability to design better database
schemas and formulate efficient queries.

Types of Relational Algebra in


DBMS
The relational algebra operations in DBMS can be divided into several categories based
on their nature such as:

1. Unary Operations
Unary operations involve a single relation as input and produce a new relation as
output.
Selection (σ)
The selection operation is used to retrieve rows from a relation that satisfies a given
condition.

Syntax
σ(condition)(Relation)

Projection (π)
The projection operation is used to retrieve specific columns from a relation, effectively
removing other columns.

Syntax
π(column1, column2, ...)(Relation)

Where column1, column2, ... are the columns to be retrieved from the relation.

Example: Consider a relation Employee with the following columns

Employee_ID Name Salary Depart

101 Ram 60000 HR

102 Sita 45000 IT

103 Shiva 55000 Marketing

104 Vishnu 75000 IT


Employee_ID Name Salary Depart

105 Deepthi 65000 HR

106 Varun 55000 Marketing

Query: Retrieve employees with a salary greater than 55,000.


σ(Salary > 55000)(Employee)

Employee_ID Name Salary Depart

104 Vishnu 75000 IT

105 Deepthi 65000 HR

106 Varun 55000 Marketing

Query: Retrieve the names and salaries of all employees.


π(Name, Salary)(Employee)
Name Salary

Ram 60000

Sita 45000

Shiva 55000

Vishnu 75000

Deepthi 65000

Varun 55000

Query: Rename the relation as Workers and the attributes as Emp_ID,


Emp_Name, Emp_Salary, and Emp_Dept.
ρ(Workers)(Emp_ID, Emp_Name, Emp_Salary, Emp_Dept)(Employee)

Employee_ID Name Salary Depart

101 Ram 60000 HR

102 Sita 45000 IT


Employee_ID Name Salary Depart

103 Shiva 55000 Marketing

104 Vishnu 75000 IT

105 Deepthi 65000 HR

106 Varun 55000 Marketing

Query: Retrieve the names of employees with a salary greater than 55,000.
π(Name)(σ(Salary > 55000)(Employee))

Name

Ram

Vishnu

Deepthi
2. Binary Operations
Binary operations work with two relations and produce a new relation as a result.

Union (∪)
The union operation combines the results of two relations, eliminating duplicates. It
requires both relations to have the same number of attributes with matching domains.

Syntax

Relation1 ∪ Relation2

Example

If Employees has the columns Employee_ID, Name, and Salary, and another relation
Managers also has the same columns, the union operation will combine all rows from
both relations, ensuring no duplicate rows.

Relation1 (Employees)

Employee_ID Name Sala

101 Ram 50000

102 Sita 55000

Relation2 (Managers)
Dept_ID Dept_Name

201 HR

202 Finance

Cartesian Product (Employees × Departments)

Employee_ID Name Dept_ID Dept_N

101 Ram 201 HR

101 Ram 202 Finance

102 Sita 201 HR

102 Sita 202 Finance

Intersection (∩)
The intersection operation returns the rows that are common to both relations. It is
equivalent to performing a Union followed by a Selection on common elements.

Syntax
Relation1 ∩ Relation2
Example

If both Employees and Managers have the same structure, the operation Employees ∩
Managers will return only those employees who are also managers.

Relation1 (Employees)

Employee_ID Name Sal

101 Ram 50000

102 Sita 55000

103 Deepthi 60000

Relation2 (Managers)

Employee_ID Name Sal

103 Deepthi 60000

104 Varun 65000

Intersection (Employees ∩ Managers)


Employee_ID Name Sal

103 Deepthi 60000

3. Additional Operations
The additional operations of relational algebra in DBMS:

Rename (ρ)
The rename operation is used to rename a relation or its attributes.

Syntax
ρ(new_name, Relation)

Or to rename attributes:
ρ(new_attribute1, new_attribute2, ...)(Relation)

Example: The rename operation is used to rename either the entire relation or just its
attributes.

Employee Table

Employee_ID Dept_ID Nam

101 D1 Ram

102 D2 Sita
Employee_ID Dept_ID Nam

103 D1 Deepthi

104 D3 Varun

Departments Table

Dept_ID Dept_Name

D1 HR

D2 IT

D3 Finance

Operation: ρ(Employees_New)(Employees)

Employee_ID Dept_ID Nam

101 D1 Ram
Employee_ID Dept_ID Nam

102 D2 Sita

103 D1 Deepthi

104 D3 Varun

4. Join Operations
Join operations are used to combine data from two relations based on some condition.

Theta Join (θ)


A theta join combines rows from two relations based on a condition (theta) that can be
any comparison operator (e.g., =, <, >, <=, >=, ≠).

Syntax

Relation1 ⨝θ Relation2

Example

A theta join combines rows from two relations based on a condition. We'll perform a
theta join where the employee's salary is greater than a manager's salary.

Employees Table (with Salary)


Employee_ID Dept_ID Name

101 D1 Ram 3000

102 D2 Sita 4000

103 D1 Deepthi 5000

104 D3 Varun 3500

Managers Table

Manager_ID Dept_ID Name

201 D1 Ravi 4500

202 D2 Neha 3500

203 D3 Arun 4000

Operation: Employees ⨝(Employees.Salary > Managers.Salary) Managers


Employee_ID Dept_ID Name Employee_Salary Manager_ID Mana

103 D1 Deepthi 5000 201 4500

102 D2 Sita 4000 202 3500

Equi-Join
An equi-join is a special case of theta join where the condition is based on equality (=)
between attributes from the two relations.

Syntax

Relation1 ⨝= Relation2

Example

An equi-join matches rows based on equality (=) between attributes. We will join
Employees with Departments on Dept_ID.

Operation: Employees ⨝= Departments

Employee_ID Dept_ID Name Dept_N

101 D1 Ram HR

103 D1 Deepthi HR
Employee_ID Dept_ID Name Dept_N

102 D2 Sita IT

104 D3 Varun Finance

Natural Join
A natural join automatically joins two relations by matching columns with the same
names and combines the rows where these columns have equal values.

Syntax

Relation1 ⨝ Relation2

Example

A natural join automatically combines rows by matching columns with the same names
and values, eliminating duplicates.

Operation: Employees ⨝ Departments

Employee_ID Dept_ID Name Dept_N

101 D1 Ram HR

103 D1 Deepthi HR
Employee_ID Dept_ID Name Dept_N

102 D2 Sita IT

104 D3 Varun Finance

Outer Joins
Outer joins return rows that do not have a match in the other relation. The three types of
outer joins are:

Left Outer Join: Returns all rows from the left relation and the matching rows from the
right relation (nulls for unmatched rows in the right relation).

Operation: Employees ⟕ Departments (Left Outer Join)

Employee_ID Dept_ID Name Dept_N

101 D1 Ram HR

102 D2 Sita IT

103 D1 Deepthi HR

104 D3 Varun Finance


Right Outer Join: Returns all rows from the right relation and the matching rows from
the left relation (nulls for unmatched rows in the left relation).

Operation: Employees ⟖ Departments (Right Outer Join)

Employee_ID Dept_ID Name Dept_N

101 D1 Ram HR

103 D1 Deepthi HR

102 D2 Sita IT

104 D3 Varun Finance

Full Outer Join: Returns all rows when there is a match in either left or right relation
(nulls for unmatched rows from both relations).

Operation: Employees ⟗ Departments (Full Outer Join)

Employee_ID Dept_ID Name Dept_N

101 D1 Ram HR

102 D2 Sita IT
Employee_ID Dept_ID Name Dept_N

103 D1 Deepthi HR

104 D3 Varun Finance

Division (÷)
The division operation is used to find tuples in one relation that are related to all tuples
in another relation. It is typically used for "all" queries.

Syntax
Relation1 ÷ Relation2

Example

If Employees has Employee_ID and Dept_ID, and Departments has Dept_ID, the
division operation can find all employees who work in every department listed in
Departments.

Employees Table (with Dept_ID)

Employee_ID Dept_ID

101 D1

101 D2
Employee_ID Dept_ID

102 D2

103 D1

103 D2

104 D3

Departments Table

Dept_ID

D1

D2

Result: Employees ÷ Departments

Employee_ID

103
Relational Algebra Queries in DBMS
Relational algebra is essential for the theoretical foundation of SQL (Structured Query
Language) and plays a significant role in query optimization within DBMS.

A relational algebra query typically specifies the operations to be performed on tables


(relations) to retrieve or modify data. These operations are set-based, meaning they
work on sets of data rather than individual elements. The primary operations in
relational algebra include:

 Selection (σ): Filters rows in a relation based on a specified condition.


 Projection (π): Retrieves specific columns from a relation.
 Union (∪): Combines the rows of two relations, eliminating duplicates.
 Intersection (∩): Retrieves the common rows between two relations.
 Difference (−): Retrieves rows that are in one relation but not in another.
 Join (⨝): Combines rows from two relations based on a specified condition.‍
 Rename (ρ): Changes the name of a relation or its attributes.

Comparision of Relational Algebra


with SQL
Relational Algebra and SQL (Structured Query Language) are both fundamental
concepts in database management systems (DBMS) used to query relational
databases. Although they share certain similarities and key differences in terms of
syntax, usability, and real-world applications.

Similarities Between Relational Algebra


and SQL
Here are the similarities between relational algebra and sql:
1. Query formulation: Both relational algebra and SQL are used for querying relational
databases.

2. Logic: Both are declarative languages, meaning they specify what data to retrieve
rather than how to retrieve it.

3. Operations: Many relational algebra operations have SQL equivalents:

 Selection (σ) in relational algebra is similar to the WHERE clause in SQL.


 Projection (π) corresponds to SELECT in SQL.
 Join (⨝) in relational algebra matches the JOIN operation in SQL.

Relational Algebra
Relational algebra is a procedural query language. It gives a step by
step process to obtain the result of the query. It uses operators to
perform queries.

It is also a conceptual language. By this we mean that the queries


made in it are not run on the computer so it is not used as a business
language. However, this knowledge allows us to understand the
optimization and query execution of RDBMS.

PauseNext
Mute
Current Time 0:39
/
Duration 7:01
Loaded: 23.72%
Fullscreen
Basic Set Oriented Operations: It is also known as traditional set
oriented operations. This operation is derived from the mathematical
set theory. Following operations include in basic set oriented
operations. All operations are binary operations which means that
operations applies to pair of relations.

o UNION

o INTERSECTION

o DIFFERENCE

o CARTERSION PRODUCT
Following conditions are satisfied is both the relations are
union

o All the relations must have the same attribute

o Each column in the first relation must have the same data type as the
corresponding column in the second relation. The names of the
corresponding attributes need not be the same.
For example: Consider two relations P and Q such that degrees of P
and Q are m and n. Degree must be equal then m=n.

Special Relational Operations: These operations focus on the


structure of the tuples. These operations not only add power to algebra
but also simplify general questions that are too long to express using
set oriented operations. Special relational operations include the
following operations:

o JOIN

o SELCTION

o Projection

o Division
Let's explain each one by one in detail.

1. Select Operation:
o The select operation also known as restriction operation results in a
new relation that contains those rows of relation that satisfy a specified
condition.

o The select operation selects tuples that satisfy a given predicate.

o It is denoted by sigma (σ).


1. Notation: σ p(r)
Where:

σ is used for selection prediction


r is used for relation
p is used as a propositional logic formula which may use connectors
like: AND OR and NOT. These relational can use as relational operators
like =, ≠, ≥, <, >, ≤.

For example: LOAN Relation

BRANCH_NAME LOAN_NO AMOUNT

Downtown L-17 1000

Redwood L-23 2000


Perryride L-15 1500

Downtown L-14 1500

Mianus L-13 500

Roundhill L-11 900

Perryride L-16 1300

Input:

1. σ BRANCH_NAME="perryride" (LOAN)
Output:

BRANCH_NAME LOAN_NO AMOUNT

Perryride L-15 1500

Perryride L-16 1300

Features of Select operation:

o It is a unary operation because works only a single relation.

o The resulting table has the same degree as that of the original table.
This means that the number of columns in both the relations is same.

o The number of rows of the resulting relations is always less than or


equal to the original relation.

o It operates on each row of the relation independently.

2. Project Operation:
o This operation shows the list of those attributes that we wish to appear
in the result. Rest of the attributes are eliminated from the table.

o It is denoted by ∏ and the attributes to be retrieved appear as


subscripts separated by commas and relations name is given in
parenthesis following the PI.
1. Notation: ∏ A1, A2, An (r)
Where

A1, A2, A3 is used as an attribute name of relation r.

Example: CUSTOMER RELATION

NAME STREET CITY

Jones Main Harrison

Smith North Rye

Hays Main Harrison

Curry North Rye

Johnson Alma Brooklyn

Brooks Senator Brooklyn

Input:

1. ∏ NAME, CITY (CUSTOMER)


Output:

NAME CITY
Jones Harrison

Smith Rye

Hays Harrison

Curry Rye

Johnson Brooklyn

Brooks Brooklyn

Features of Project operation:

o It is a unary operation i.e. it can operate only a single relation.

o The degree of the resulting relation is equal to the number of


attributes specified in the attribute list

o If the attribute list contains a primary key attribute then the number of
tuples in the resulting relation is equal to the number of tuples in the
original relation.

o Non-commutative: It does not hold the commutative property.

o Duplicate Elimination: This operation removes the duplicate rows


from the table which results in a valid relation knows as duplicate
elimination.

3. Union Operation:
o The Union operation of two relations results in a new relation
containing rows from both relations with duplicates removed.

o Suppose there are two tuples R and S. The union operation contains all
the tuples that are either in R or S or both in R & S.

o It eliminates the duplicate tuples. It is denoted by ∪.


1. Notation: R ∪ S
A union operation must hold the following condition:

o R and S must have the attribute of the same number.

o Duplicate tuples are eliminated automatically.


Features of Union operation:

o Input relations must be union compatible.

Commutativity: This means that the result of (R ∪ S) is same as that


of (S ∪ R).
o

o Associativity: This mean that R ∪ (S ∪ O) = (R ∪ S) ∪ O where R, S


and O are relations.

Example:
DEPOSITOR RELATION

CUSTOMER_NAME ACCOUNT_NO

Johnson A-101

Smith A-121

Mayes A-321

Turner A-176

Johnson A-273

Jones A-472

Lindsay A-284
BORROW RELATION

CUSTOMER_NAME LOAN_NO

Jones L-17

Smith L-23

Hayes L-15

Jackson L-14

Curry L-93

Smith L-11

Williams L-17

Input:

1. ∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)


Output:

CUSTOMER_NAME

Johnson

Smith
Hayes

Turner

Jones

Lindsay

Jackson

Curry

Williams

Mayes

4. Intersection:
o Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in both R & S.

o It is denoted by intersection ∩.
1. Notation: R ∩ S
Features of Intersection operation:

o Input relations must be union compatible.

o Commutativity: This means that result of (R ∩ S) is same as that of (S


∩ R).

o Associativity: This mean that R ∩ (S ∩ O) = (R ∩ S) ∩ O where R, S


and O are relations.
Example: Using the above DEPOSITOR table and BORROW table
Input:

1. ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)


Output:

CUSTOMER_NAME

Smith

Jones

5. Difference:
o The difference of two relations results in a new relation that contains
tuples that occur in the first relation but not in the second relation.

o Suppose there are two tuples R and S. The set difference operation
contains all tuples that are in R but not in S.

o It is denoted by intersection minus (-).


1. Notation: R - S
Example: Using the above DEPOSITOR table and BORROW table

Input:

1. ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)


Output:

CUSTOMER_NAME

Jackson

Hayes
Willians

Curry

Features of Set Difference operation:

o Input relations must be union compatible.

o They are not commutative. This means that the result of R - Sis not the
same as the result S - P.

o They are not associative. This means that result of Q - (R - S) is not the
same as the result of (Q - S) - R where Q, S and R are relations.

o Intersection can be expressed as difference (- ) operations:


(R ∩ S) = R - (R - S)

But writing the equation with a single intersection operation is more


convenient than involving a pair of difference operations. Here R and S
unions are favorable.

6. Cartesian product
o The Cartesian product is used to combine each row in one table with
each row in the other table. It is also known as a cross product.

o It is denoted by X.

o It is a binary relation which means that it always operates on two


relations.
1. Notation: E X D

Example:
EMPLOYEE

EMP_ID EMP_NAME EMP_DEPT

1 Smith A
2 Harry C

3 John B

DEPARTMENT

DEPT_NO DEPT_NAME

A Marketing

B Sales

C Legal

Input:

1. EMPLOYEE X DEPARTMENT
Output:

EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME

1 Smith A A Marketing

1 Smith A B Sales

1 Smith A C Legal

2 Harry C A Marketing
2 Harry C B Sales

2 Harry C C Legal

3 John B A Marketing

3 John B B Sales

3 John B C Legal

Properties of Cartesian product operation:

o The relations to which Cartesian product operation is applied need not


necessary to be union compatible.

o The total number of rows in the result operation is equal to the product
of the number of rows in the first and second relations, i.e.
Total number of tuples of data = Total number of tuples of E +
Total number of tuples of D

o The resulting relation may have duplicate properties if some properties


of the two relations are defined on common domains.

o The resulting degree of action is equal to the sum of the degrees of all
relations
Degree of E = Degree of E + Degree of D

o Commutativity: This means that result of E X D is same as that of D X

o Associativity: This mean that E X (D X F) = (E X D) X F where E, D


and F are relations.

7. Rename Operation:
The rename operation is used to rename the output relation. It is
denoted by rho (ρ). If no rename operation is applied then the names
of the attributes in a resulting relation are the same as those in the
original relation and in the same order.
Example: We can use the rename operator to rename STUDENT
relation to STUDENT1.

1. ρ(STUDENT1, STUDENT)
Note: Apart from these common operations Relational algebra can be
used in Join operations.

8. Join Operation:
A join operation combines two or more relations to form a new relation
such that new relation contains only those tuples from different
relations that satisfy the specified criteria. It forms a new relation
which contains all the attributes from both the joined relations whose
tuples are those defined by the restrictions applies i.e. the join
condition applied on two participating relations. The join is performed
on two relations, who have one or more attributes in common. These
attributes must be domain compatible i.e. they have same data type. It
is a binary operation. The Join operation is denoted by a Join symbol.
The general from of representing a join operation on two relations P
and Q is

1. P⋈ <join_condion> Q
When we use equality operator in the join condition then such a join is
called EQUI Join. It is mostly commonly used Join.

EMP Table

EMP_ID ENAME SALARY Dept_ID

101 Raj 450000 1

102 Lavi 300000 2

103 Chandan 480000 3

104 Ravi 340000 4


105 Abhi 370000 5

DEPT Relation

Dept_No Dname

1 Marketing

2 Purchase

3 Finance

4 Packing

5 Marketing

In the above both table, suppose we want to know the employee


information with department name in which each employee is working.
Now the employee information is in the Emp relation and Department
name information is in dept relation. So to retrieve the columns from
both the tables at same time, we need to join the EMP and DEPT
relations. The relations can be joined over the column Dept_ID that
exist in EMP relation and the Dept_no that exist in the DEPT relation
and are domain compatible. Thus the result of EQUI Join where the
condition is that Dept_Id attributes values the EMP relation should be
equal to the Dept_No attribute values in the DEPT relation, the result
is shown below.

EMP_ID ENAME SALARY Dept_ID Dept_No Dname

101 Raj 450000 1 1 Marketing

102 Lavi 300000 2 2 Purchase


103 Chandan 480000 3 3 Finance

104 Ravi 340000 4 4 Packing

105 Abhi 370000 5 5 Marketing

Another is known as natural join in which there is no need to explicitly


name the columns. A join is performed by joining all columns from the
first relation of any column to another relation with the same name.
The result of natural join is as follows:

EMP_ID ENAME SALARY Dept_ID Dname

101 Raj 450000 1 Marketing

102 Lavi 300000 2 Purchase

103 Chandan 480000 3 Finance

104 Ravi 340000 4 Packing

105 Abhi 370000 5 Marketing

The natural join may also been referred to as INNER JOIN. Another type
of JOIN in which a relation is joined to itself by comparing values with a
column of the relation is called a self-join.

Example of SELF Join:

EMP_ID ENAME Mang_Id


101 Raj -

102 Lavi 101

103 Chandan 104

104 Ravi -

105 Abhi -

Resultant Relation

ENAME Mang_Id

Lavi Raj

Chandan Ravi

In the EMP relation, the attribute EMP_ID shows employee'code, ENAME


and Mang_Id under which employee is working. In this, some employee
are not having Mang_Id i.e. their value is null because they act as a
manager itself.

Features of Join Operation:

o Like the cartersion product, join operations are commutative. Using


this property, we can choose which relation can be the inner and which
one the outer while joining two relations. If P and Q are two relations
then,
P⋈ Q = Q ⋈ P

o Rows whose join attribute is null do not appear in the resulting relation.

o We can also join more than two relations by increasing the complexity.
o Joins are generally used when a relationship exists between relations
such as where the join condition is based on the primary key and
foreign key columns.

o Join is a very powerful operator and together with projection form a


base for normalization i.e. theoretical support for designing database
relations.

o If the attributes on which the join is performed have the same name in
both the relations then renaming is necessary for the EQUIJOIN
operation and unnecessary for the NATURAL JOIN operation because
the former i.e. EQUI JOIN both exist as a result of the common attribute
relation but the latter In i.e., natural additive consequent relations
have only one common property.
Division Operation:

The division operation results in a new relation such that every tuple
appearing in the resulting relation must exist in the dividend relation.
In the combination of each tuple in the denominator relation. It is a
binary operation that operates on two relations. The division is
represented by a symbol "÷".

Example: Retrieve the Subject Name that is taught in all


courses.

Subject Relation

Subject_Name Cousres

Computer Graphics BCA

Computer Graphics BSC GWD

DBMS Btech CS

DBMS Btech IT

Theory of Computaion MCA


DBMS BCA

Course Relation

Courses Name

BCA

Btech CS

Btech IT

BSC GWD

MCA

BCA

Subject Relation "÷" Course Relation

The resultant relation is: Result Relation

Subject_Name

DBMS

Properties of Division Relation:

o It is a binary operation as it operators on two relations


o The division operation is suited to queries that include the phrase "for
all".

o It is very rarely used in database applications.

Multiple Choice Questions on Relational Algebra


Operations
1. Which of the following symbol is used to denote the project
operation in relational algebra?

a. PI

b. Dollar

c. Omega

d. Sigma

What is Relational Algebra?


Relational Algebra is considered a procedural query
language. It has a group of operators that work on relations
or tables. It takes relations as an input and also gives relation
as an output. It is the basic foundation for Structured Query
Languages. It consists of different operators.

Operators in Relational Algebra

 Projection (π)
 Selection (σ)
 Cross Product (X)
 Union (U)
 Set difference (-)
 Rename (ρ)
 Natural Join (⋈)
 Conditional Join
 Intersection (⋂)
What is Relational Algebra in DBMS?
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. We will discuss relational calculus in a separate
tutorial.

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:

Left, Right, Full outer join (⟕, ⟖, ⟗)


1. Natural Join (⋈)
2.
3. Intersection (∩)
4. Division (÷)

Lets discuss these operations one by one with the help of examples.

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.

If you understand little bit of SQL then you can think of it as a where
clause in SQL, which is used for the same purpose.

Syntax of Select Operator (σ)

σ Condition/Predicate(Relation/Table name)
Select Operator (σ) Example
Table: CUSTOMER
---------------

Customer_Id Customer_Name Customer_City


----------- ------------- -------------
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi
Query:

σ Customer_City="Agra" (CUSTOMER)
Output:

Customer_Id Customer_Name Customer_City


----------- ------------- -------------
C10100 Steve Agra
C10111 Raghu Agra
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.

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 ∏.

Table: CUSTOMER

Customer_Id Customer_Name Customer_City


----------- ------------- -------------
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi
Query:

∏ Customer_Name, Customer_City (CUSTOMER)


Output:

Customer_Name Customer_City
------------- -------------
Steve Agra
Raghu Agra
Chaitanya Noida
Ajeet Delhi
Carl Delhi

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
Table 1: COURSE

Course_Id Student_Name Student_Id


--------- ------------ ----------
C101 Aditya S901
C104 Aditya S901
C106 Steve S911
C109 Paul S921
C115 Lucy S931
Table 2: STUDENT

Student_Id Student_Name Student_Age


------------ ---------- -----------
S901 Aditya 19
S911 Steve 18
S921 Paul 19
S931 Lucy 17
S941 Carl 16
S951 Rick 18
Query:

∏ Student_Name (COURSE) ∪ ∏ Student_Name (STUDENT)


Output:

Student_Name
------------
Aditya
Carl
Paul
Lucy
Rick
Steve
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.

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

these two relations R1 ∩ R2.


the relations, then in that case we can apply intersection operation on

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.
Table 1: COURSE

Course_Id Student_Name Student_Id


--------- ------------ ----------
C101 Aditya S901
C104 Aditya S901
C106 Steve S911
C109 Paul S921
C115 Lucy S931
Table 2: STUDENT

Student_Id Student_Name Student_Age


------------ ---------- -----------
S901 Aditya 19
S911 Steve 18
S921 Paul 19
S931 Lucy 17
S941 Carl 16
S951 Rick 18
Query:

∏ Student_Name (COURSE) ∩ ∏ Student_Name (STUDENT)


Output:

Student_Name
------------
Aditya
Steve
Paul
Lucy
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.

Query:
Lets write a query to select those student names that are present in
STUDENT table but not present in COURSE table.

∏ Student_Name (STUDENT) - ∏ Student_Name (COURSE)


Output:

Student_Name
------------
Carl
Rick
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. I know it sounds confusing but once we
take an example of this, you will be able to understand this.

Syntax of Cartesian product (X)

R1 X R2
Cartesian product (X) Example
Table 1: R

Col_A Col_B
----- ------
AA 100
BB 200
CC 300
Table 2: S

Col_X Col_Y
----- -----
XX 99
YY 11
ZZ 101
Query:
Lets find the cartesian product of table R and S.

R X S
Output:

Col_A Col_B Col_X Col_Y


----- ------ ------ ------
AA 100 XX 99
AA 100 YY 11
AA 100 ZZ 101
BB 200 XX 99
BB 200 YY 11
BB 200 ZZ 101
CC 300 XX 99
CC 300 YY 11
CC 300 ZZ 101
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 (ρ) 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.

Table: CUSTOMER

Customer_Id Customer_Name Customer_City


----------- ------------- -------------
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi
Query:

ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:

CUST_NAMES
----------
Steve
Raghu
Chaitanya
Ajeet
Carl

What is Relational Algebra?

Every database management system must define a query language to allow users to access the
data stored in the database. Relational Algebra is a procedural query language used to query the
database tables to access data in different ways.

In relational algebra, input is a relation(table from which data has to be accessed) and output is
also a relation(a temporary table holding the data asked for by the user).

Relational Algebra works on the whole table at once, so we do not have to use loops etc to iterate
over all the rows(tuples) of data one by one. All we have to do is specify the table name from
which we need the data, and in a single line of command, relational algebra will traverse the
entire given table to fetch data for you.

The primary operations that we can perform using relational algebra are:

1. Select

2. Project
3. Union

4. Set Different

5. Cartesian product

6. Rename

Select Operation (σ)

This is used to fetch rows(tuples) from table(relation) which satisfies a given condition.

Syntax: σp(r)

Where, σ represents the Select Predicate, r is the name of relation(table name in which you want
to look for data), and p is the prepositional logic, where we specify the conditions that must be
satisfied by the data. In prepositional logic, one can use unary and binary operators
like =, <, > etc, to specify the conditions.

Let's take an example of the Student table we specified above in the Introduction of relational
algebra, and fetch data for students with age more than 17.

σage > 17 (Student)

This will fetch the tuples(rows) from table Student, for which age will be greater than 17.

You can also use, and, or etc operators, to specify two conditions, for example,

σage > 17 and gender = 'Male' (Student)

This will return tuples(rows) from table Student with information of male students, of age more
than 17.(Consider the Student table has an attribute Gender too.)

Project Operation (∏)

Project operation is used to project only a certain set of attributes of a relation. In simple words,
If you want to see only the names all of the students in the Student table, then you can use
Project Operation.

It will only project or show the columns or attributes asked for, and will also remove duplicate
data from the columns.

Syntax: ∏A1, A2...(r)


where A1, A2 etc are attribute names(column names).

For example,

∏Name, Age(Student)

Above statement will show us only the Name and Age columns for all the rows of data
in Student table.

Union Operation (∪)

This operation is used to fetch data from two relations(tables) or temporary relation(result of
another operation).

For this operation to work, the relations(tables) specified should have same number of
attributes(columns) and same attribute domain. Also the duplicate tuples are autamatically
eliminated from the result.

Syntax: A ∪ B

where A and B are relations.

For example, if we have two tables RegularClass and ExtraClass, both have a
column student to save name of student, then,

∏Student(RegularClass) ∪ ∏Student(ExtraClass)

Above operation will give us name of Students who are attending both regular classes and extra
classes, eliminating repetition.

Set Difference (-)

This operation is used to find data present in one relation and not present in the second relation.
This operation is also applicable on two relations, just like Union operation.

Syntax: A - B

where A and B are relations.

For example, if we want to find name of students who attend the regular class but not the extra
class, then, we can use the below operation:

∏Student(RegularClass) - ∏Student(ExtraClass)
Cartesian Product (X)

This is used to combine data from two different relations(tables) into one and fetch data from the
combined relation.

Syntax: A X B

For example, if we want to find the information for Regular Class and Extra Class which are
conducted during morning, then, we can use the following operation:

σtime = 'morning' (RegularClass X ExtraClass)

For the above query to work, both RegularClass and ExtraClass should have the attribute time.

You might also like