Query to find the data which not common in two tables

Query to find the data which not common in two tables – Learn how to select the data from  table TAB1 that are not exist in the TAB2.

Below are the two tables  TBL1 and TBL2.

Creating tables:

CREATE TABLE TBL1
(C1 VARCHAR2(1)
);
CREATE TABLE TBL2
(C1 VARCHAR2(1)
);

Inserting data into tables:

INSERT ALL 
INTO TBL1 VALUES('A')
INTO TBL1 VALUES('B')
INTO TBL1 VALUES('C')
INTO TBL1 VALUES('D')
INTO TBL1 VALUES('E')
SELECT * FROM DUAL;
INSERT ALL 
INTO TBL2 VALUES('A')
INTO TBL2 VALUES('B')
INTO TBL2 VALUES('E')
INTO TBL2 VALUES('G') 
SELECT * FROM DUAL;

Verifying data in the tables:

 SELECT * FROM TBL1;
C1
A
B
C
D
E
SELECT * FROM TBL2;
C1
A
B
E
G

Query  to select the data from  table TAB1 that are not exist in the TAB2

Query 1 using NOT IN operator

SELECT * FROM TBL1 t1 WHERE C1 NOT IN (SELECT C1 FROM TBL2);

Query 2 using NOT EXISTS operator

SELECT * FROM TBL1 t1 WHERE NOT exists (SELECT 1 FROM TBL2 t2 where t1.c1=t2.c1);

Query 3 using co related query

SELECT * FROM TBL1 t1 WHERE c1 <>all(SELECT c1 FROM TBL2 );

Query 4 using MINUS operator

SELECT * FROM TBL1 MINUS SELECT * FROM TBL2;

Query 5 using co related query

SELECT * FROM TBL1 T1 WHERE 1 > (SELECT COUNT(*) FROM TBL2 T2 WHERE T1.C1=T2.C1 );

Query 6 using Left Outer Join

SELECT T1.C1 FROM TBL1 T1 left outer join TBL2 T2
on T1.C1 = T2.C1 WHERE T2.C1 IS NULL;

Query 6 using Right Outer Join

SELECT T1.C1 FROM TBL1 T1 FULL outer join TBL2 T2
on T1.C1 = T2.C1 WHERE T2.C1 IS NULL;

Query 7 using inline view condition

SELECT * FROM TBL1 T1 WHERE (SELECT COUNT(1) FROM TBL2 T2 WHERE T1.C1=T2.C1 )=0;

Below is the Result for all the above queries

Result

C1
A
B

Related Posts