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

Extended Operators in Relational Algebra

Last Updated : 05 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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: 

  • Join
  • Intersection
  • Divide 

The relations used to understand extended operators are STUDENT, STUDENT_SPORTS, ALL_SPORTS and EMPLOYEE which are shown in Table 1, Table 2, Table 3 and Table 4 respectively. 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

Table 1

  STUDENT_SPORTS         

ROLL_NO SPORTS
1 Badminton
2 Cricket
2 Badminton
4 Badminton

Table 2

 ALL_SPORTS

SPORTS
Badminton
Cricket 

Table 3

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 4

Intersection (âˆĐ): Intersection on two relations R1 and R2 can only be computed if R1 and R2 are union compatible (These two relation should have same number of attributes and corresponding attributes in two relations have same domain). Intersection operator when applied on two relations as R1âˆĐR2 will give a relation with tuples which are in R1 as well as R2. Syntax:

 Relation1 âˆĐ Relation2
Example: Find a person who is student as well as employee-  STUDENT âˆĐ EMPLOYEE  

In terms of basic operators (union and minus) :

STUDENT âˆĐ EMPLOYEE = STUDENT + EMPLOYEE - (STUDENT U EMPLOYEE) 

RESULT:

ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
4 SURESH DELHI 9156768971 18

  Conditional Join(⋈c): Conditional Join is used when you want to join two or more relation based on some conditions. Example: Select students whose ROLL_NO is greater than EMP_NO of employees

STUDENT⋈c STUDENT.ROLL_NO>EMPLOYEE.EMP_NOEMPLOYEE

In terms of basic operators (cross product and selection) :

σ (STUDENT.ROLL_NO>EMPLOYEE.EMP_NO)(STUDENT×EMPLOYEE)

RESULT:

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

Equijoin(⋈): Equijoin is a special case of conditional join where only equality condition holds between a pair of attributes. As values of two attributes will be equal in result of equijoin, only one attribute will be appeared in result. Example: Select  students whose ROLL_NO is equal to EMP_NO of employees.

STUDENT⋈STUDENT.ROLL_NO=EMPLOYEE.EMP_NOEMPLOYEE

In terms∏ of basic operators (cross product, selection and projection) :

∏(STUDENT.ROLL_NO, STUDENT.NAME, STUDENT.ADDRESS, STUDENT.PHONE, STUDENT.AGE EMPLOYEE.NAME, EMPLOYEE.ADDRESS, EMPLOYEE.PHONE, EMPLOYEE>AGE)(σ (STUDENT.ROLL_NO=EMPLOYEE.EMP_NO) (STUDENT×EMPLOYEE))

RESULT:

ROLL_NO NAME ADDRESS PHONE AGE NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18 RAM DELHI 9455123451 18
4 SURESH DELHI 9156768971 18 SURESH DELHI 9156768971 18

Natural Join(⋈): It is a special case of equijoin in which equality condition hold on all attributes which have same name in relations R and S (relations on which join operation is applied). While applying natural join on two relations, there is no need to write equality condition explicitly. Natural Join will also return the similar attributes only once as their value will be same in resulting relation. Example: Select students whose ROLL_NO is equal to ROLL_NO of STUDENT_SPORTS as:

STUDENT⋈STUDENT_SPORTS

In terms of basic operators (cross product, selection and projection) :

∏(STUDENT.ROLL_NO, STUDENT.NAME, STUDENT.ADDRESS, STUDENT.PHONE, STUDENT.AGE STUDENT_SPORTS.SPORTS)(σ (STUDENT.ROLL_NO=STUDENT_SPORTS.ROLL_NO) (STUDENT×STUDENT_SPORTS))

RESULT:

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

Natural Join is by default inner join because the tuples which does not satisfy the conditions of join does not appear in result set. e.g.; The tuple having ROLL_NO 3 in STUDENT does not match with any tuple in STUDENT_SPORTS, so it has not been a part of result set. 

Left Outer Join(⟕): When applying join on two relations R and S, some tuples of R or S does not appear in result set which does not satisfy the join conditions. But Left Outer Joins gives all tuples of R in the result set. The tuples of R which do not satisfy join condition will have values as NULL for attributes of S. Example:Select students whose ROLL_NO is greater than EMP_NO of employees and details of other students as well

STUDENT⟕STUDENT.ROLL_NO>EMPLOYEE.EMP_NOEMPLOYEE

RESULT

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

Right Outer Join(⟖): When applying join on two relations R and S, some tuples of R or S does not appear in result set which does not satisfy the join conditions. But Right Outer Joins gives all tuples of S in the result set. The tuples of S which do not satisfy join condition will have values as NULL for attributes of R. Example: Select students whose ROLL_NO is greater than EMP_NO of employees and details of other Employees as well

STUDENT⟖STUDENT.ROLL_NO>EMPLOYEE.EMP_NOEMPLOYEE

RESULT:

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

Full Outer Join(⟗): When applying join on two relations R and S, some tuples of R or S does not appear in result set which does not satisfy the join conditions. But Full Outer Joins gives all tuples of S and all tuples of R in the result set. The tuples of S which do not satisfy join condition will have values as NULL for attributes of R and vice versa. Example:Select students whose ROLL_NO is greater than EMP_NO of employees and details of other Employees as well and other Students as well

STUDENT⟗STUDENT.ROLL_NO>EMPLOYEE.EMP_NOEMPLOYEE

RESULT:

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

Division Operator (÷): Division operator A÷B or A/B can be applied if and only if:

  • Attributes of B is proper subset of Attributes of A.
  • The relation returned by division operator will have attributes = (All attributes of A – All Attributes of B)
  • The relation returned by division operator will return those tuples from relation A which are associated to every B’s tuple.

                                                                                                  A

x y
a 1
b 2
a 2
d 4

                                                                                                    ÷∏

                                                                                                  B

y
1
2

 The resultant of A/B is  

                                                                                                A ÷ B

x
a

 Division can be expressed in terms of Cross Product , Set Difference and Projection.

 In the above example , for A/B , compute all  x values that are not disqualified by some y in B.

 x value is disqualified if attaching y value from B, we obtain xy tuple that is not in A.

 Disqualified x values:     ∏x(( ∏x(A) × B ) – A)

 So     A/B  = ∏x( A )  âˆ’ all disqualified tuples

          A/B   = ∏x( A )  âˆ’  âˆx(( ∏x(A) × B ) – A)

 In the above example , disqualified tuples are 

b 2
d 4

 So, the resultant is 

x
a

Advantages:

Expressive Power: Extended operators allow for more complex queries and transformations that cannot be easily expressed using basic relational algebra operations.

Data Reduction: Aggregation operators, such as SUM, AVG, COUNT, and MAX, can reduce the amount of data that needs to be processed and displayed.

Data Transformation: Extended operators can be used to transform data into different formats, such as pivoting rows into columns or vice versa.

More Efficient: Extended operators can be more efficient than expressing the same query in terms of basic relational algebra operations, since they can take advantage of specialized algorithms and optimizations.

Disadvantages:

Complexity: Extended operators can be more difficult to understand and use than basic relational algebra operations. They require a deeper understanding of the underlying data and the operators themselves.

Performance: Some extended operators, such as outer joins, can be expensive in terms of performance, especially when dealing with large data sets.

Non-standardized: There is no universal set of extended operators, and different relational database management systems may implement them differently or not at all.

Data Integrity: Some extended operators, such as aggregate functions, can introduce potential problems with data integrity if not used properly. For example, using AVG on a column that contains null values can result in unexpected or incorrect results.

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. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



Previous Article
Next Article

Similar Reads

Difference between Relational Algebra and Relational Calculus
Relational Algebra and Relational Calculus are two preliminarily important topics in the study of DBMS. Both are standardized query languages used for querying databases but the two have different approaches and operations. Relational Algebra is imperative as it deals mainly with how the data is to be obtained whereas Relational Calculus is imperat
6 min read
Basic Operators in Relational Algebra
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 rela
3 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
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
Query Optimization in Relational Algebra
Query: A query is a request for information from a database. Query Plans: A query plan (or query execution plan) is an ordered set of steps used to access data in a SQL relational database management system. Query Optimization: A single query can be executed through different algorithms or re-written in different forms and structures. Hence, the qu
10 min read
Set Theory Operations in Relational Algebra
Relational Algebra in DBMS These Set Theory operations are the standard mathematical operations on set. These operations are Binary operations that are, operated on 2 relations unlike PROJECT, SELECT and RENAME operations. These operations are used to merge 2 sets in various ways. The set operation is mainly categorized into the following: Union op
3 min read
What are the Unary Operations in Relational Algebra?
The unary operations in Relational algebra are selection, projection, and rename. The select, project, and rename operators in relational algebra work on one relation only so they are called unary operators. In this article, we will discuss the unary operations in relational algebra. We will also discuss each unary operator in detail along with exa
4 min read
Query Tree in Relational Algebra
A Query Tree is a data structure used for the internal representation of a query in RDBMS. It is also known as the Query Evaluation/Execution Tree. The leaf nodes of the query tree represent the relations, and the internal nodes are the relational algebra operators like SELECT (σ), JOIN (⋈), etc. The root node gives the output of the query on execu
4 min read
SELECT Operation in Relational Algebra
Prerequisite - Relational Algebra Select operation chooses the subset of tuples from the relation that satisfies the given condition mentioned in the syntax of selection. The selection operation is also known as horizontal partitioning since it partitions the table or relation horizontally. Notation: σ c(R)where 'c' is the selection condition which
2 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
Spatial Operators, Dynamic Spatial Operators and Spatial Queries in DBMS
1. Spatial operators : Spatial operators these operators are applied in geometric properties of objects. It is then used in the physical space to capture them and the relation among them. It is also used to perform spatial analysis. Spatial operators are grouped into three categories : Topological operators : Topological properties do not vary when
3 min read
Introduction of Relational Model and Codd Rules in DBMS
Pre-Requisite: Relational Model in DBMS It was proposed by Dr. E.F. Codd. It uses the concept of relations to represent each and every file. Relations are Two-Dimensional Tables. It is easy to implement and easy to simplification in the operations to manipulate the data. This is the most popular data model. It is simple to implement. It uses the pr
12 min read
Tuple Relational Calculus (TRC) in DBMS
Tuple Relational Calculus (TRC) is a non-procedural query language used in relational database management systems (RDBMS) to retrieve data from tables. TRC is based on the concept of tuples, which are ordered sets of attribute values that represent a single row or record in a database table. TRC is a declarative language, meaning that it specifies
4 min read
Python | Relational fields in Django models
Prerequisite: Django models Django models represent real-world entities, and it is rarely the case that real-world entities are entirely independent of each other. Hence Django supports relational databases and allows us to establish relations between different models. There are three types of relational fields which Django supports: many-to-one, m
4 min read
Properties of Relational Decomposition
When a relation in the relational model is not appropriate normal form then the decomposition of a relation is required. In a database, breaking down the table into multiple tables termed as decomposition. The properties of a relational decomposition are listed below : Attribute Preservation: Using functional dependencies the algorithms decompose t
3 min read
Violation of constraints in relational database
Here, we will learn about the violations that can occur on a database as a result of any changes made in the relation. There are mainly three operations that have the ability to change the state of relations, these modifications are given below: Insert - To insert new tuples in a relation in the database. Delete - To delete some of the existing rel
3 min read
Relational Query Evaluation | Set 1
There is a need for conversion of relational query to algebraic expressions. This expression is operated on so that final output is received. The relational algebra operators that are present are Union, Select, Join, Project, etc. These are used to formulate relational query for adequate results. Grouping, partitioning and aggregation are all parts
3 min read
Difference between Relational database and NoSQL
1. Relational Database : RDBMS stands for Relational Database Management Systems. It is most popular database. In it, data is store in the form of row that is in the form of tuple. It contain numbers of table and data can be easily accessed because data is store in the table. This Model was proposed by E.F. Codd. 2. NoSQL : NoSQL Database stands fo
2 min read
Relational Overview Vs Cassandra Overview
In this article, we are going to discuss Cassandra and Relational overview in which we will cover and able to learn how Cassandra differentiates the Relational overview. Let's discuss one by one. 1. Relational Overview : In Relational Database, you can handle and manage Small, Medium Data such that Postgres, MySQL, Oracle, etc. is fine and It also
2 min read
Difference between CouchDB and Relational Database
1. CouchDB : Apache CouchDB is an open-source document-oriented NoSQL database that uses multiple formats and protocols to store, transfer, and process its data, it uses JSON to store data, JavaScript as its query language using MapReduce, and HTTP for an API. It was developed by Apache Software Foundation and initially released in 2005. It is writ
2 min read
Relational Query Evaluation | Set 2
Prerequisite - Relational Query Evaluation | Set 1 Data is stored in disks. Disks are then manipulated using read and write methods. More number of manipulations, lesser number of disks would be failing. To overcome this and to increase life of disks we want to optimize disks used by ensuring fact that we minimize read and write operations using di
2 min read
Difference between Hierarchical and Relational data model
1. Hierarchical Data Model : Hierarchical data model is the oldest type of data model. It was developed by IBM in 1968. It organizes data in a tree-like structure. Hierarchical model consists of the following : It contains nodes that are connected by branches. Topmost node is called the root node. If there are multiple nodes that appear at the top
3 min read
Relational Query Language in DBMS
SQL has its own querying methods to interact with the database. But how do these queries work in the database? These queries work similarly to Relational Algebra that we study in mathematics. In the database, we have tables participating in relational Algebra. Relational Database systems are expected to be equipped with a query language that assist
4 min read
Constraints on Relational Database Model
In modeling the design of the relational database we can put some restrictions like what values are allowed to be inserted in the relation, and what kind of modifications and deletions are allowed in the relation. These are the restrictions we impose on the relational database. In models like Entity-Relationship models, we did not have such feature
6 min read
Anomalies in Relational Model
Anomalies in the relational model refer to inconsistencies or errors that can arise when working with relational databases, specifically in the context of data insertion, deletion, and modification. There are different types of anomalies that can occur in referencing and referenced relations which can be discussed as: These anomalies can be categor
5 min read
Article Tags :