Relational Algebra in DBMS
Relational Algebra in DBMS
Relational algebra in DBMS is a procedural query language. Queries in relational algebra are
performed using operators. Relational Algebra is the fundamental block for modern language
SQL and modern Database Management Systems such as Oracle Database, Mircosoft SQL
Server, IBM Db2, etc.
Let's know what relational algebra in DBMS is and also we will learn about relational algebra
operations in DBMS.
Before reading this article, you should have some understanding of the following DBMS topics:
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.
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
Select (σ)
σ AGE=20 (STUDENT)
σ age=20 (student)
Project (∏)
∏ NAME(STUDENT)
This will return the following output:
NAME
Aman
Atul
Baljeet
Harsh
Prateek
∏ ROLL,NAME(STUDENT)
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
Notation: R ∪ S
Attributes. It is a binary operator as it requires two operands.
If relations don't have the same set of attributes, then the union of such relations will result
in NULL.
∏ NAME(STUDENT) ∪ ∏ NAME(EMPLOYEE)
NAME
Aman
Anant
NAME
Ashish
Atul
Baljeet
Harsh
Pranav
Prateek
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)
NAME
Aman
Atul
Prateek
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
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
ρ(STUDENT_NAME,∏ NAME(STUDENT))
STUDENT_NAME
NAME
Aman
Atul
Baljeet
Harsh
Prateek
Takeaway
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.
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.
EMPLOYEE
DEPARTMENT
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 combines two relations using a condition. This condition is represented by the
Notation : R ⋈θ S
symbol "theta"(θ). Here conditions can be inequality conditions such as >,<,>=,<=, etc.
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.
Check Cartesian Product, if the tuple contains same E_NO, insert that tuple in the output relation
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.
Notation : R ⋈ S
Preferably Natural Join is performed on the foreign key.
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_NO E_NAME CITY EXPERIENCE D_NO D_NAME MIN_EXPERIEN
E-1 Ram Delhi 04 D-1 HR 03
E-2 Varun Chandigarh 09 D-2 IT 05
E-3 Ravi Noida 03 D-3 Marketing 02
But unlike the above operation, where we have two columns of E_NO, here we are having only
one column of E_NO. This is because Natural Join automatically keeps a single copy of a
common attribute.
Outer Join
Unlike Inner Join which includes the tuple that satisfies the given condition, Outer Join also
includes some/all the tuples which don't satisfy the given condition. It is also of three types: Left
Outer Join, Right Outer Join, and Full Outer Join.
As we can see from the diagram, Left Outer Join returns the matching tuples(tuples present in
both relations) and the tuples which are only present in Left Relation, here R.
However, if the matching tuples are NULL, then attributes/columns of Right Relation, here S are
made NULL in the output relation.
Here we are combining EMPLOYEE and DEPARTMENT relation with the constraint that
EMPLOYEE's E_NO must be equal to DEPARTMENT's E_NO.
E_NO E_NAME CITY EXPERIENCE D_NO D_NAME MIN_EXPERIEN
E-1 Ram Delhi 04 D-1 HR 03
E-2 Varun Chandigarh 09 D-2 IT 05
E-3 Ravi Noida 03 D-3 Marketing 02
E-4 Amit Bangalore 07 - - -
As you can see here, all the tuples from left, i.e., EMPLOYEE relation are present. But E-4 is not
satisfying the given condition, i.e., E_NO from EMPLOYEE must be equal to E_NO from
DEPARTMENT, still it is included in the output relation. This is because Outer Join also
includes some/all the tuples which don't satisfy the condition. That's why Outer Join marked E-
4's corresponding tuple/row from DEPARTMENT as NULL.
Right Outer Join returns the matching tuples and the tuples which are only present in Right
Relation here S.
The same happens with the Right Outer Join, if the matching tuples are NULL, then the
attributes of Left Relation, here R are made NULL in the output relation.
We will combine EMPLOYEE and DEPARTMENT relations with the same constraint as above.
As all the tuples from DEPARTMENT relation have a corresponding E_NO in EMPLOYEE
relation, therefore no tuple from EMPLOYEE relation contains a NULL.
Full Outer Join returns all the tuples from both relations. However, if there are no matching
tuples then, their respective attributes are made NULL in output relation.
Again, combine the EMPLOYEE and DEPARTMENT relation with the same constraint.
∏ NAME(STUDENT) ∩ ∏ NAME(EMPLOYEE)
NAME
Baljeet
Harsh
Division (÷)
It's a bit difficult to understand this in a theoretical way, but you will understand this with an
example.
Let's have two relations, ENROLLED and COURSE. ENROLLED consist of two attributes
STUDENT_ID and COURSE_ID. It denotes the map of students who are enrolled in given
courses.
ENROLLED
STUDENT_ID COURSE_ID
Student_1 DBMS
Student_2 DBMS
Student_1 OS
Student_3 OS
COURSE
COURSE_ID
DBMS
OS
Now the query is to return the STUDENT_ID of students who are enrolled in every course.
ENROLLED(STUDENT_ID, COURSE_ID)/COURSE(COURSE_ID)
STUDENT_ID
Student_1
Relational Calculus
Introduction
Relational Calculus in database management system (DBMS) is all about "What you want ?".
Relational calculus does not tell us how to get the results from the Database, but it just cares
about what we want.
1. Procedural Language - Those Languages which clearly define how to get the required
results from the Database are called Procedural Language. Relational algebra is a
Procedural Language.
2. Declarative Language - Those Language that only cares about What to get from the
database without getting into how to get the results are called Declarative
Language. Relational Calculus is a Declarative Language.
So Relational Calculus is a Declarative Language that uses Predicate Logic or First-Order Logic
to determine the results from Database.
Tuple Relational Calculus in DBMS uses a tuple variable (t) that goes to each row of the table
and checks if the predicate is true or false for the given row. Depending on the given predicate
condition, it returns the row or part of the row.
{t \| P(t)}
Where t is the tuple variable that runs over every Row, and P(t) is the predicate logic expression
or condition.
Let's take an example of a Customer Database and try to see how TRC expressions work.
Customer Table
Customer_id Name Zip code
1 Rohit 12345
2 Rahul 13245
3 Rohit 56789
4 Amit 12345.
Example 1: Write a TRC query to get all the data of customers whose zip code is 12345.
TRC Query: {t \| t ∈ Customer ∧ t.Zipcode = 12345} or TRC Query: {t \| Customer(t) ∧
t[Zipcode] = 12345 }
Workflow of query - The tuple variable "t" will go through every tuple of the Customer table.
Each row will check whether the Cust_Zipcode is 12345 or not and only return those rows that
satisfies the Predicate expression condition.
The TRC expression above can be read as "Return all the tuple which belongs to the
Customer Table and whose Zipcode is equal to 12345."
Example 2: Write a TRC query to get the customer id of all the Customers.
Customer_id
1
2
3
4
Domain Relational Calculus uses domain Variables to get the column values required from the
database based on the predicate expression or condition.
{<x1,x2,x3,x4...> \| P(x1,x2,x3,x4...)}
where,
<x1,x2,x3,x4...> are domain variables used to get the column values required,
and P(x1,x2,x3...) is predicate expression or condition.
Let's take the example of Customer Database and try to understand DRC queries with some
examples.
Customer Table
Customer_id Name Zip code
1 Rohit 12345
2 Rahul 13245
3 Rohit 56789
4 Amit 12345
Example 1: Write a DRC query to get the data of all customers with Zip code 12345.
Workflow of Query: In the above query x1,x2,x3 (ordered) refers to the attribute or column
which we need in the result, and the predicate condition is that the first two domain variables x1
and x2 should be present while matching the condition for each row and the third domain
variable x3 should be equal to 12345.
Example 2: Write a DRC query to get the customer id of all the customer.
Customer_id
1
2
3
4
NOTE: The above Queries are written for both TRC and DRC so that they can
be compared and can be corelated.