Minus Operator in DBMS

About Minus operator in DBMS

In this article, we will learn about minus operator in DBMS.
Minus operator helps to retrieve data from a set of data or table leaving behind the common data of the other table and thus returning the data belonging only to the first table.

 

Minus operator in DBMS

Minus Operator in DBMS

  • MINUS operator is used to subtract the result set obtained by first SELECT query from the result set obtained by second SELECT query.
  • Simply, we can say that MINUS operator will return only those rows which are unique in only first SELECT query and not those rows which are common to both first and second SELECT queries.

Syntax for MINUS

SELECT column name(s) FROM table1
MINUS
SELECT column name(s) FROM table2

The columns used in all the select statements must have the following

  • the same number of columns
  • Similar or compatible data types
Minus Operator in DBMS – 1
Consider the two tables

Table1

Name Address Age Grade
Priya Hyderabad 19 A
Rahul Chennai 20 B
Karthik Mumbai 21 A
Payal Delhi 20 B

Table2

Name Course Age Grade
Divya Java 22 B
Amitha C++ 19 A
Harshitha Python 21 A
Payal Java 20 B

Example

SELECT Name FROM Table1
MINUS
SELECT Name FROM Table2;

Output:

Name
Priya
Rahul
Karthik

Another Example

SELECT Name, Age,Grade FROM Table1
MINUS
SELECT Name,Age,Grade FROM Table2;

Output:

Name Age Grade
Priya 19 A
Rahul 20 B
Karthik 21 A

Note:

The MINUS operator is not supported with all databases. It is supported by Oracle database but not SQL server or PostgreSQL.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription