Javatpoint Logo
Javatpoint Logo

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_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
1001 Arush Sharma 4001 2020-01-02 Delhi 20000
1002 Bulbul Roy 4002 2019-12-31 Delhi 38000
1004 Saurabh Roy 4001 2020-10-10 Mumbai 45000
1005 Shivani Singhania 4001 2019-07-15 Kolkata 42000
1006 Avinash Sharma 4002 2019-11-11 Delhi 28000
1007 Shyam Besas 4003 2021-06-21 Lucknow 35000

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_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
1010 Ankush Roy 4004 2018-10-02 Delhi 25000
1001 Arush Sharma 4001 2020-01-02 Delhi 20000
1009 Raj Singhania 4005 2021-05-10 Noida 40000
1005 Shivani Singhania 4001 2019-07-15 Kolkata 42000
1008 Avinabh Chetya 4002 2018-11-11 Banglore 22000
1007 Shyam Besas 4003 2021-06-21 Lucknow 35000

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

Output:

Worker_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
1002 Bulbul Roy 4002 2019-12-31 Delhi 38000
1004 Saurabh Roy 4001 2020-10-10 Mumbai 45000
1006 Avinash Sharma 4002 2019-11-11 Delhi 28000

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_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
1002 Bulbul Roy 4002 2019-12-31 Delhi 38000
1004 Saurabh Roy 4001 2020-10-10 Mumbai 45000

Next TopicSQL MODIFY COLUMN





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA