The Wayback Machine - https://web.archive.org/web/20240819101205/https://www.geeksforgeeks.org/set-difference-operator-in-relational-algebra/
Open In App

Set Difference Operator in Relational Algebra

Last Updated : 16 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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’ is called a Relation.
  • Tuple- In Relational Algebra ‘Row’ is called as Tuple.
  • Attribute- In Relational Algebra ‘Column’ is called an Attribute.

Set Difference(-)

set A-B

Set A-B

A binary operation that gives tuples in one relation but not present in another relation. In simple terms, it returns all the rows from the first relation, which is not present in the second one. For Example: Perform Set Difference Operation in relation R and Relation S.

Relation R

Relation R

Relation R

Relation S

Relation R-S, After Performing Set Difference operation

Relation R-S

Relation R-S

Here all the Alphabet which are in R but not in S are shown. Note Preference will be given to the first relation attributes in this case ‘Alphabet’ of relation R.

Condition for Set Difference

1. Both relations must have the same number of attributes.

2. The data type of their corresponding attributes must be the same.

Examples

Question 1: Consider the below tables Employee and Student. Perform Set Difference operation as (Employee-Student).

Employee

Employee Id

Employee Name

1

Manish

2

Rohit

3

Shubhum

4

Manish

Student

Student Id

Student Name

6

Sudesh

5

Deepak

2

Rishav

4

Manish

9

Aman

Solution:

Step 1: Check the condition for the set difference

  • Both the relation have the same number of attributes
  • Data Types of Corresponding Attributes are the same

Step 2: Result after performing Set difference (Employee-Student)

Employee-Student

Employee Id

Employee Name

1

Manish

2

Rohit

3

Shubhum

Detailed Explanation

  • In the question, As no specific column is mentioned, we compare both the values in each row.
  • Since it is given that we need to show (Employee-Student). So, the new relation will have the attribute name of the Employee i.e.; Employee id and Employee Name.
  • From the the employee table only that row will be printed in which none of the value matches in the entire row of student table.
  • For Employee IDthe ‘1’, is not present in the student id. So, the entire row is printed.
  • For Employee ID ‘2’, it is present in Student ID. So, we check for the Employee Name ‘Ronit’, it is not present for the same student id. So, the entire row is printed.
  • For the Employee ID ‘3’, it is not present in the Student ID. So, the entire row is printed.
  • For the Employee ID ‘4’, it is present in Student ID. So, check for the Employee Name ‘Manish’, it is also present for the same student Name for same student id. So, the entire row not printed.

Question 2: Consider the below tables Depositer and Borrower. List all the customers names who have accounts but have not taken any loans.

Depositor

Customer Name

Account No

Jack

A007

Harry

A001

Sam

A006

Alex

A003

Borrower

Customer Name

Loan No

Smith

L201

Jack

L211

John

L216

Alex

L214

Solution:

Step 1: Check the condition for the set difference

  • Both the relation have the same number of attributes
  • Data Types of Corresponding Attributes are the same

Step 2: Result after performing Set difference (Depositor-Borrower)

Depositor-Borrower

Customer Name

Harry

Sam

Detailed Explanation

  • In the given question, we asked about the customer name of the Depositor. So, we focus on customer name column.
  • Since, the question is interested in Depositor’s Customer Name, we do (Depositor-Borrower).
  • Print the Customer name from the depositor table when it is not matched with the customer name in borrower table.
  • In the given case, only ‘Harry’ and ‘Sam’ names are not matched. So, we printed their names only.

Conclusion

The Set Difference operator is a powerful tool when you want to know the uncommon element present in the table. Having a good understanding and the application of this operator can help in filtering the records and values from the given Relations.



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
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
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
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
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
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
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
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
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
Difference between Relational model and Document Model
1. Relational Model: Relational model is a model for the management of the database. It is called so because the data is stored in the form of relations i.e., in the form of tables. Every row consists of related data. These row in the table depict a real-world entity. To work with python and the Relational data model we need a prerequisite knowledg
2 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
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
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
Difference between Network and Relational data model
1. Network Data Model: It is advance version of hierarchical data model. To organize data it uses directed graphs instead of tree-structure. In this child can have more than one parent. It uses concept of two data structures i.e., Records and Sets. In the above figure, Project is root node which has two children i.e. Project 1 and Project 2. Projec
3 min read
Difference between Hierarchical, Network and Relational Data Model
1. Hierarchical Data Model: Hierarchical data model is the oldest type of the data model. It was developed by IBM in 1968. It organizes data in the tree-like structure. Hierarchical model consists of the following : It contains nodes which are connected by branches.The topmost node is called the root node.If there are multiple nodes appear at the t
4 min read
Difference between E-R Model and Relational Model in DBMS
In database management systems (DBMS), two key methods are the Relational model and the Entity-Relationship (E-R) model. Each has a specific function in the development and operation of databases. While the Relational model provides the practical structure for organizing and managing data in relational databases, the E-R model concentrates on offer
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
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
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 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
three90RightbarBannerImg