The Wayback Machine - https://web.archive.org/web/20240725060117/https://www.geeksforgeeks.org/basic-operators-in-relational-algebra-2/
Open In App

Basic Operators in Relational Algebra

Last Updated : 16 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Basics of Relational model: Relational Model 

Relational Algebra is a procedural query language that takes relations as an input and returns relations as an output. There are some basic operators which can be applied in relation to producing the required results which we will discuss one by one. We will use STUDENT_SPORTS, EMPLOYEE, and STUDENT relations as given in Table 1, Table 2, and Table 3 respectively to understand the various operators. 

Table 1: STUDENT_SPORTS

ROLL_NO SPORTS
1 Badminton
2 Cricket
2 Badminton
4 Badminton

 Table 2: EMPLOYEE

EMP_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
5 NARESH HISAR 9782918192 22
6 SWETA RANCHI 9852617621 21
4 SURESH DELHI 9156768971 18

Table 3: STUDENT 

ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 9156768971 18

Selection operator (?): Selection operator is used to selecting tuples from a relation based on some condition. Syntax: 

? (Cond)(Relation Name)

Extract students whose age is greater than 18 from STUDENT relation given in Table 3  

? (AGE>18)(STUDENT)

[Note: SELECT operator does not show any result, the projection operator must be called before the selection operator to generate or project the result. So, the correct syntax to generate the result is: ?(? (AGE>18)(STUDENT))]

RESULT: 

ROLL_NO NAME ADDRESS PHONE AGE
3 SUJIT ROHTAK 9156253131 20

Projection Operator (?): Projection operator is used to project particular columns from a relation. Syntax:  

?(Column 1,Column 2â€Ķ.Column n)(Relation Name)

Extract ROLL_NO and NAME from STUDENT relation given in Table 3  

?(ROLL_NO,NAME)(STUDENT)

RESULT:  

ROLL_NO NAME
1 RAM
2 RAMESH
3 SUJIT
4 SURESH

Note: If the resultant relation after projection has duplicate rows, it will be removed. For Example  ?(ADDRESS)(STUDENT) will remove one duplicate row with the value DELHI and return three rows. 

Cross Product(X): Cross product is used to join two relations. For every row of Relation1, each row of Relation2 is concatenated. If Relation1 has m tuples and and Relation2 has n tuples, cross product of Relation1 and Relation2 will have m X n tuples. Syntax: 

Relation1 X Relation2

To apply Cross Product on STUDENT relation given in Table 1 and STUDENT_SPORTS relation given in Table 2,  

STUDENT X STUDENT_SPORTS

RESULT: 

ROLL_NO NAME ADDRESS PHONE AGE ROLL_NO SPORTS
1 RAM DELHI 9455123451 18 1 Badminton
1 RAM DELHI 9455123451 18 2 Cricket
1 RAM DELHI 9455123451 18 2 Badminton
1 RAM DELHI 9455123451 18 4 Badminton
2 RAMESH GURGAON 9652431543 18 1 Badminton
2 RAMESH GURGAON 9652431543 18 2 Cricket
2 RAMESH GURGAON 9652431543 18 2 Badminton
2 RAMESH GURGAON 9652431543 18 4 Badminton
3 SUJIT ROHTAK 9156253131 20 1 Badminton
3 SUJIT ROHTAK 9156253131 20 2 Cricket
3 SUJIT ROHTAK 9156253131 20 2 Badminton
3 SUJIT ROHTAK 9156253131 20 4 Badminton
4 SURESH DELHI 9156768971 18 1 Badminton
4 SURESH DELHI 9156768971 18 2 Cricket
4 SURESH DELHI 9156768971 18 2 Badminton
4 SURESH DELHI 9156768971 18 4 Badminton

Union (U): Union on two relations R1 and R2 can only be computed if R1 and R2 are union compatible (These two relations should have the same number of attributes and corresponding attributes in two relations have the same domain). Union operator when applied on two relations R1 and R2 will give a relation with tuples that are either in R1 or in R2. The tuples which are in both R1 and R2 will appear only once in the result relation. Syntax: 

 Relation1 U Relation2

Find the person who is either student or employees, we can use Union operators like: 

STUDENT U EMPLOYEE

RESULT:  

ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 9156768971 18
5 NARESH HISAR 9782918192 22
6 SWETA RANCHI 9852617621 21

Minus (-): Minus on two relations R1 and R2 can only be computed if R1 and R2 are union compatible. Minus operator when applied on two relations as R1-R2 will give a relation with tuples that are in R1 but not in R2. Syntax: 

 Relation1 - Relation2

Find the person who is a student but not an employee, we can use minus operator like:  

STUDENT - EMPLOYEE

RESULT:  

ROLL_NO NAME ADDRESS PHONE AGE
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20

Rename(?): Rename operator is used to giving another name to a relation. Syntax:  

?(Relation2, Relation1)

To rename STUDENT relation to STUDENT1, we can use rename operator like:  

?(STUDENT1, STUDENT)

If you want to create a relation STUDENT_NAMES with ROLL_NO and NAME from STUDENT, it can be done using rename operator as:  

?(STUDENT_NAMES, ?(ROLL_NO, NAME)(STUDENT))

Extended Relational Algebra Operators                                        Overview of Relational Algebra Operators

Previous Year Gate Questions 
https://www.geeksforgeeks.org/gate-gate-cs-2012-question-50/ 
https://www.geeksforgeeks.org/gate-gate-cs-2012-question-43/

Article contributed by Sonal Tuteja.



Previous Article
Next Article

Similar Reads

Difference between Relational Algebra and Relational Calculus
Both Relational Algebra and Relational Calculus are formal query languages. Relational Algebra: Relational Algebra is a procedural language. In Relational Algebra, The order is specified in which the operations have to be performed. In Relational Algebra, frameworks are created to implement the queries. The basic operation included in relational al
3 min read
Extended Operators in Relational Algebra
Basic idea about relational model and basic operators in Relational Algebra: Relational Model Basic Operators in Relational Algebra Extended operators are those operators which can be derived from basic operators. There are mainly three types of extended operators in Relational Algebra: JoinIntersectionDivide The relations used to understand extend
6 min read
Difference between Tuple Relational Calculus (TRC) and Domain Relational Calculus (DRC)
1. Tuple Relational Calculus (TRC) : A tuple relational calculus is a non-procedural query language that specifies to select of the tuples in a relation. It can select the tuples with a range of values or tuples for certain attribute values etc. The resulting relation can have one or more tuples. Notation : {T | P (T)} or {T | Condition (T)} where
3 min read
Introduction of Relational Algebra in DBMS
Pre-Requisite: Relational Model in DBMS Relational Algebra is a procedural query language. Relational algebra mainly provides a theoretical foundation for relational databases and SQL. The main purpose of using Relational Algebra is to define operators that transform one or more input relations into an output relation. Given that these operators ac
4 min read
Example Queries on Relational Algebra
Given below are a few examples of a database and a few queries based on that. (1). Suppose there is a banking database which comprises following tables : Customer(Cust_name, Cust_street, Cust_city) Branch(Branch_name, Branch_city, Assets) Account (Branch_name, Account_number, Balance) Loan(Branch_name, Loan_number, Amount) Depositor(Cust_name, Acco
1 min read
PROJECT Operation in Relational Algebra
Prerequisite - Relational Algebra Project operation selects (or chooses) certain attributes discarding other attributes. The Project operation is also known as vertical partitioning since it partitions the relation or table vertically discarding other columns or attributes. Notation: πA(R) where 'A' is the attribute list, it is the desired set o
2 min read
Cartesian Product Operation in Relational Algebra
Prerequisite - Relational Algebra We already are aware of the fact that relations are nothing but a set of tuples, and here we will have 2 sets of tuples. On applying CARTESIAN PRODUCT on two relations that is on two sets of tuples, it will take every tuple one by one from the left set(relation) and will pair it up with all the tuples in the right
3 min read
RENAME (ρ) Operation in Relational Algebra
Prerequisites - Introduction of Relational Algebra in DBMS, Basic Operators in Relational Algebra The RENAME operation is used to rename the output of a relation. Sometimes it is simple and suitable to break a complicated sequence of operations and rename it as a relation with different names. Reasons to rename a relation can be many, like - We may
2 min read
Set Difference Operator in Relational Algebra
Relational Algebra is used to play with the data stored in relational databases. Relational Algebra has many operations to play with tables. One of the fundamental operations is set difference. This article will discuss Set Difference, its condition, and its examples. Key Terms Used in Set Difference Operator Relation- In Relational Algebra 'Table'
4 min read
How to solve Relational Algebra problems for GATE
In this article, Let's discuss common types of questions in relational algebra which are asked in GATE. Before reading this article, you should have an idea about Basic Operators and Extended Operators in relational algebra. Type-1: Given a relational algebra expression, find the result. Suppose you have a relation Order(Prod_Id, Agent_Id, Order_Mo
5 min read
Article Tags :
three90RightbarBannerImg