Minus Operator in DBMS

Minus Operator in DBMS

 

In this article, we will learn about 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

Consider the two tables

Table1

NameAddressAgeGrade
PriyaHyderabad19A
RahulChennai20B
KarthikMumbai21A
PayalDelhi20B

Table2

NameCourseAgeGrade
DivyaJava22B
AmithaC++19A
HarshithaPython21A
PayalJava20B

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:

NameAgeGrade
Priya19A
Rahul20B
Karthik21A

Note:

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