Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Relational Algebra in DBMS | Core Computer Science

Ujjwal Abhishek
Ujjwal Abhishek

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 (⋂)
Projection (π)

It is used to retrieve data from a column of a table.

Eg. 

students  
roll_noname stream
7AshishCSE
8AnkitIT
9AshishCSE

In the above relation named students, data of students are stored. There are three attributes roll_no, name, and stream.

πName(students) - The output of this statement is the ‘name’ column from the ‘students’ Table.

name
Ashish
Ankit

Here, it can be seen that there are two Ashish in the original table but in the output, there is only one Ashish. This is because the projection never shows the duplicate data. All the duplicate rows automatically get removed.

Selection (σ)

It is used to select the required tuple/row from the table.

Eg. 

students  
roll_noname stream
7AshishCSE
8AnkitIT
9AshishCSE

σroll_no>7(students)

The above statement will select the tuples having roll_no greater than 7. The rows which will be selected are given below.

roll_noname stream
8AnkitIT
9AshishCSE
Cross Product (X)

Cross Product is done between two relations. It takes two relations as an input and gives one relation the cross product as the output. If there are X attributes in 1st relation and Y in 2nd relation then the final relation will have X+Y attributes. The number of tuples in the Final relation will be X*Y.

students  
roll_noname stream
7AshishCSE
8AnkitIT
9AshishCSE

 

enrollment 
courseidcourse 
1OS
2DBMS

By doing the cross product of the above two tables students and enrollment. The output will be

roll_nonamestreamcourseidcourse
7AshishCSE1OS
7 AshishCSE2DBMS
8AnkitIT1OS
8 AnkitIT2DBMS
9AshishCSE1OS
9 AshishCSE2DBMS
Union (U)

It is the same as the general Union operation in the set theory. It eliminates the duplicate tuples and selects the tuples which have appeared at least once in any table. For Union operation, both the tables must have the same set of attributes.

students 
namecourse
AshishOS
AnkitDBMS
PrashantOS

 

record 
namestream
AshishCSE
KaranIT

(πname(students) U πname(record))

The output of the above statement will be

name
Ashish
Ankit
Prashant
Karan
Set Difference (-)

It is also the same as the set difference in set theory. It is denoted by “ - ”. If there are two tables A and B then A-B means all the tuples which are present in  A but not in B. Both the tables must have the same set of attributes.

students 
namecourse
AshishOS
AnkitDBMS
PrashantOS

 

record 
namestream
AshishCSE
KaranIT

πname(students) - πname(record)

The output of the above statement will be

name
Ankit
Prashant
Rename (ρ)

Rename is denoted by “ρ” (rho). It is used to rename the attributes of a relation. ρ(A/B)(R) will rename the attribute ‘B’ of relation R by ‘A’.

Natural Join (⋈)

If there are two relations A and B then Natural join between A and B will show the set of all the tuples in which they have the equal common attribute.

students  
Rollnamecourse
1AshishOS
2AnkitDBMS
3PrashantCN

 

professor 
teachercourse
ShantanuDBMS
MukherjeeOS

students ⋈ professor

The output of the above statement will be

rollnamecourseteacher
1AshishOSMukherjee
2AnkitDBMSShantanu
Conditional Join

It is also similar to natural join. The only difference between Natural Join and Conditional Join is in natural join the attributes must be equal by default but in conditional join, we can specify the conditions like greater than, less than, etc.

Intersection (⋂)

If there are two relations A and B then the output of A ⋂ B will be the set of tuples that are common in both A and B. 

students 
namecourse
AshishOS
AnkitDBMS
PrashantOS

 

enrollment  
roll_nonamestream
8AnkitIT
9AshishCSE

πname(students) ⋂ πname(enrollment)

The output of the above statement will be

name
Ashish
Ankit

 

Ujjwal Abhishek
Ujjwal Abhishek
Ujjwal is final-year CSE Undergraduate, a competitive coder, and a web developer passionate about problem-solving and data structures and algorithms.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet