Query Is A Question or Requesting Information
Query Is A Question or Requesting Information
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
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
As we know, natural Join skips the duplicate common columns. So, one Std_ID is skipped, as
given below
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
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
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))
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.
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
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
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.
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.
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.
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.
Ram 60000
Sita 45000
Shiva 55000
Vishnu 75000
Deepthi 65000
Varun 55000
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)
Relation2 (Managers)
Dept_ID Dept_Name
201 HR
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)
Relation2 (Managers)
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
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)
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.
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.
Managers Table
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.
101 D1 Ram HR
103 D1 Deepthi HR
Employee_ID Dept_ID Name Dept_N
102 D2 Sita IT
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.
101 D1 Ram HR
103 D1 Deepthi HR
Employee_ID Dept_ID Name Dept_N
102 D2 Sita IT
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).
101 D1 Ram HR
102 D2 Sita IT
103 D1 Deepthi HR
101 D1 Ram HR
103 D1 Deepthi HR
102 D2 Sita IT
Full Outer Join: Returns all rows when there is a match in either left or right relation
(nulls for unmatched rows from both relations).
101 D1 Ram HR
102 D2 Sita IT
Employee_ID Dept_ID Name Dept_N
103 D1 Deepthi HR
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.
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
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.
2. Logic: Both are declarative languages, meaning they specify what data to retrieve
rather than how to retrieve it.
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.
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 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.
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.
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
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.
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.
Input:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
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.
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.
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:
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:
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.
Input:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
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.
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.
Example:
EMPLOYEE
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:
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
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 degree of action is equal to the sum of the degrees of all
relations
Degree of E = Degree of E + Degree of D
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
DEPT Relation
Dept_No Dname
1 Marketing
2 Purchase
3 Finance
4 Packing
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.
104 Ravi -
105 Abhi -
Resultant Relation
ENAME Mang_Id
Lavi Raj
Chandan Ravi
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 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 "÷".
Subject Relation
Subject_Name Cousres
DBMS Btech CS
DBMS Btech IT
Course Relation
Courses Name
BCA
Btech CS
Btech IT
BSC GWD
MCA
BCA
Subject_Name
DBMS
a. PI
b. Dollar
c. Omega
d. Sigma
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.
Basic/Fundamental Operations:
1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Set Difference (-)
5. Cartesian product (X)
6. Rename (ρ)
Derived Operations:
Lets discuss these operations one by one with the help of examples.
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.
σ Condition/Predicate(Relation/Table name)
Select Operator (σ) Example
Table: CUSTOMER
---------------
σ Customer_City="Agra" (CUSTOMER)
Output:
Table: CUSTOMER
Customer_Name Customer_City
------------- -------------
Steve Agra
Raghu Agra
Chaitanya Noida
Ajeet Delhi
Carl Delhi
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.
table_name1 ∪ table_name2
Union Operator (∪) Example
Table 1: COURSE
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.
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
Note: Only those rows that are present in both the tables will appear
in the result set.
table_name1 ∩ table_name2
Intersection Operator (∩) Example
Lets take the same example that we have taken above.
Table 1: COURSE
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.
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
------------
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.
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:
Rename (ρ)
Rename (ρ) operation can be used to rename a relation or an attribute
of a relation.
Rename (ρ) Syntax:
ρ(new_relation_name, old_relation_name)
Table: CUSTOMER
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:
CUST_NAMES
----------
Steve
Raghu
Chaitanya
Ajeet
Carl
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
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.
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,
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 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.
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.
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
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.
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
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:
For the above query to work, both RegularClass and ExtraClass should have the attribute time.