'Write a query to fetch the rows of one table plus the rows of other table not present in first table?

We have two tables:

Product

ProductGuid ProductName
Cell 1 Cell 2
Cell 3 Cell 4

JoinProductDocument

JoinProductGuid ProductGuid
Cell 1 Cell 2
Cell 3 Cell 4

I want to retrieve all rows in product table with an addition attribute (IsPresent) which will be 1 if that product is present in JoinProductDoucment table and 0 if it is not present.

Thanks in advance!

I tried using sql subqueries but could not figure it out.



Solution 1:[1]

Select ProductGuid,ProductName,
       (Case when b.ProductGuid is null then 0 else 1 end) IsPresent
from Product a 
  left join JoinProductDocument b on a.ProductGuid=b.ProductGuid

Try this

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GuidoG