SQL Minus

The Minus is an operator in Structured Query Language, which is used with two SELECT queries.

This operator returns only unique records of the first table, not the common records of both tables.

Syntax of Minus operator in Structured Query Language:

Minus

The data type and the number of fields must be the same for every SELECT statement connected with the Minus SQL operator.

Example of Minus operator in SQL

To understand the example of Minus operator in Structured query Language, we have to create the two different tables Old_Worker_Info and New_Worker_Info, and insert the records of Workers in both the tables.

The following query creates the Old_Worker_Info table in the Industry Database:

The following INSERT queries insert the records of old workers in the Old_Worker_Info table:

The following query shows the data of the Old_Worker_Info table.


Worker_IdWorker_First_NameWorker_Last_NameWorker_Dept_IdWorker_Joining_DateWorker_CityWorker_Salary
1001ArushSharma40012020-01-02Delhi20000
1002BulbulRoy40022019-12-31Delhi38000
1004SaurabhRoy40012020-10-10Mumbai45000
1005ShivaniSinghania40012019-07-15Kolkata42000
1006AvinashSharma40022019-11-11Delhi28000
1007ShyamBesas40032021-06-21Lucknow35000

The following query creates the New_Worker_Info table in the Industry Database:

The following INSERT queries insert the records of new workers in the New_Worker_Info table:

Step 4: View the Inserted Data

The following query shows the data of the New_Worker_Info table.


Worker_IdWorker_First_NameWorker_Last_NameWorker_Dept_IdWorker_Joining_DateWorker_CityWorker_Salary
1010AnkushRoy40042018-10-02Delhi25000
1001ArushSharma40012020-01-02Delhi20000
1009RajSinghania40052021-05-10Noida40000
1005ShivaniSinghania40012019-07-15Kolkata42000
1008AvinabhChetya40022018-11-11Banglore22000
1007ShyamBesas40032021-06-21Lucknow35000

The following query uses the Minus operator with the above tables:

Output:

Worker_IdWorker_First_NameWorker_Last_NameWorker_Dept_IdWorker_Joining_DateWorker_CityWorker_Salary
1002BulbulRoy40022019-12-31Delhi38000
1004SaurabhRoy40012020-10-10Mumbai45000
1006AvinashSharma40022019-11-11Delhi28000

Minus operator with WHERE clause

The WHERE clause can also be used with the Minus operator for filtering the records of the first table.

Syntax of Minus with WHERE clause

Minus

Example of Minus with WHERE Clause

The following query shows those records of Workers from the above tables whose salary is greater than and equal to 35000:

Output:

Worker_IdWorker_First_NameWorker_Last_NameWorker_Dept_IdWorker_Joining_DateWorker_CityWorker_Salary
1002BulbulRoy40022019-12-31Delhi38000
1004SaurabhRoy40012020-10-10Mumbai45000





Latest Courses