Let us create two tables blog1 and blog2.
The two tables, column names and datatypes should be same.
--------------------------------------------------------------------------------------------------------------------------
Create Table blog1
(
Id int primary key,
Name
Gender
)
Go
Insert into blog1 values (1, '
Insert into blog1 values (2, 'xxx', 'F')
Insert into blog1 values (3, '
Insert into blog1 values (4, '
Insert into blog1 values (5, 'yyy', 'F')
Go
-------------------------------------------------------------------------------------------------------------------
Create Table blog2
(
Id int primary key,
Name
Gender
)
Go
Insert into blog2 values (4, '
Insert into blog2 values (5, 'yyy', 'F')
Insert into blog2 values (6, '
Insert into blog2 values (7, '
Insert into blog2 values (8, '
Go
---------------------------------------------------------------------------------------------------------------------
Now execute both the tables.
------------------------------------------------------------------------------------------------------------------
Now it's time to apply except operator, as discussed in the first step it will return the unique rows from the left query which are not placed in right query. From the above tables blog1 is the left table and blog 2 is the right table.
-------------------------------------------------------------------------------------------------------------
Apply except operator.
OR
Select Id, Name, Gender From blog1
Where Id NOT IN (Select Id from blog2)
The difference between Except and NOT IN operator is that NOT IN does not filter duplicates where as
Now the result will be as follows. Where 1,2,3 are unique which are not in blog2 table.
----------------------------------------------------------------------------------------------------------------------
If you want right query data, then write the query as follows.
Now the result will be as follows. Where 6,7,8 are unique which are not in blog1 trouble.
No comments:
Post a Comment