Table of contents
1.
Introduction
2.
Relational algebra operators:
3.
Example:
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Example on Relational Algebra

Author Ankit Kumar
5 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Relational algebra is one of the most integral parts of the DBMS. It is a query language that accepts instances of relations as input and produces instances of relations as output. It makes use of operators to carry out queries. A binary or unary operator can be used. They receive relations as input and produce relations as output. 

Recursive relational algebra is conducted on a relation, and intermediate outcomes are also considered relations. 

In the upcoming sections, we’ll see different types of Relational algebra operators and then we’ll also solve an example based on Relational algebra. 
Let’s see the type of Relational algebra operators now.

Must Recommended Topic, Generalization in DBMS.

Must Recommended Topic, Schema in DBMS

Relational algebra operators:

Let us now have a look at different operators used in Relational algebra

  • σ – Selection with conditions. 

This operator chooses all tuples that meet the requirements. Displays a whole table with relation to the structure.

  • Π – Projection operator.

It displays column(s) that fulfil the given criteria.

  • ⨝ - Natural join operator.

A binary operator that connects two relations based on the values of their shared attributes.

  • X - Cartesian Product.

This operator merges information from two different relationships into a single one.

  • -, ∪, and ∩ - Set operators.

These operators perform the set operations. The operations are difference, union and intersection respectively.

  • ρ -  Rename Operator.

The outcomes of relational algebra are also relations, although they lack a name. We may rename the output relation using the rename method. The operation rename is symbolised by the little Greek letter rho ρ.

Now its’ time to solve an example.

 

Recommended Topic, B+ Tree in DBMS, Multiple Granularity in DBMS

Example:

Consider the following relational database of a college.

Student(RollNumber, StudentName, Address)

Teachers(TeacherID, TecherName, TeachingSubject)

College(RollNumber, TeacherID)

Write SQL or Relational algebra expressions for the following requests.

  1. Find the name of Students who live in Lalitpur.
  2. Find the name of the teacher who teaches Database Management System subject.
  3. Find the name of the teacher who teaches Computer Organisation subject to John Smith.
  4. Insert a new tuple into relation teachers.
  5. Delete records of students whose address is "Pokhara".

Let us now see the queries.

  1.  Πstudentname(σAddress= “Lalitpur”(Student))
    Here we will be using select operator with projection operator to select all tuples that satisfy the condition where the address is Lalitpur.
  2. ΠTeacherNamet(σTeachingSubject=“Database Management System”))
    Here we will be using select operator with projection operator to select all tuples that satisfy the condition where the subject is DBMS.
  3. ΠTeacherNamet(σTeachingSubject=“Computer Organisation”^StudentName=“John Smith” S⨝X⨝T)) 
    Firstly here we will be using select operator with projection operator to select all tuples that satisfy the condition where the subject is computer organization and name is john smith. In the end, we join the relations (Student, College, Teacher) using the binary joining operator.
  4. Teachers<-Teacher ∪ {“T07”, “Manish”, “C++”)
    Here we will be using the assignment operator first and then the union operator to add the tuple.
  5. Student<Student-σAddress= “Pokhara”(Students))
    Here we will be using the assignment operator first and then subtracting with the select operator to remove the data.

Its’ time to head for some FAQs.

You can also read about the Multiple Granularity Locking and Checkpoint in DBMS

FAQs

  1. What are the types of operators used in Relational language?
    There is two types of operators used in Relational language i.e., Binary and Unary.
     
  2. Identify the operator (ρ) and write down its’ use.
    This is the rename operator. The outcomes of relational algebra are also relations, although they lack a name. We may rename the output relation using the rename method.
     
  3. Identify the operator (⨝) and write down its’ use.
    This is the natural join operator. It is a binary operator that joins two relations on common attributes’ values.
     
  4. Define Relational Algebra.
    Relational Algebra is a query language that accepts instances of relations as input and produces instances of relations as output. It makes use of operators to carry out queries. 
     
  5. How are Relational Algebra and Relational Calculus similar yet so different?
    Relational algebra and relational calculus are both formal languages related to the relational model that are used to define fundamental retrieval requests. 
    Relational algebra employs a procedural approach by outlining a step-by-step technique for carrying out a query, whereas relational calculus employs a non-procedural approach by describing the information.

Key Takeaways

In this article first, we extensively discussed Relational Algebra. We learnt the definition of relational algebra. In the next section, we learnt about the operators that are to be used while solving the problems. In the next section, we understood an example by solving it. In the final section, we went through the FAQs of the topic. 

Recommended Readings: 

We hope that this blog has helped you enhance your knowledge regarding Relational Algebra and if you would like to learn more, check out our articles on Top 100 SQL Problems and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass