I have two tables and in the output, I want a single table with rows from both the tables one below the other.
- Requirement is: Get all the rows from
table1
but exclude rows fromtable2
with condition thatemail
orproductname
is not present intable1
.
Refer image. Yellow cells in table2
should not be present in the output, since they are present in table1
, as highlighted. So only sr.no. 2,4,5,7 from table-2 should be present in output table.
Can someone suggest solution?
The logic I tried is UNION
to first get all rows one below the other. However I'm not getting logic to exclude matching rows from table2
after UNION
. I tried multiple options as commented below in my code:
with a AS (
SELECT
email AS leademail,
website AS leadwebsite,
productname AS leadproductname,
source AS leadsource,
detail AS leaddetail,
leaddate
FROM
`table1`),
b AS (
SELECT
email AS contactemail,
website AS contactwebsite,
productname AS contactproductname,
source AS contactsource,
detail AS contactdetail,
contactdate
FROM
`table2`)
SELECT
*
FROM
a union all
SELECT
*
FROM
b
--option1 except distinct select * from b
--option2 union all select * from b
--option3 except distinct select * from b
--option4 left join
--(select * from b where contactemail not in (select leademail from a))
ON
leademail=contactemail
AND leadproductname=contactproductname