'TRUE or FALSE in column after LEFT JOIN

Let's say I have table A and table B.
A has 5 columns: F, G, H, I and X.
B has 1 column: X.
X has the same kind of values in both A and B.

Say I run the following.

SELECT *  
FROM A  
LEFT JOIN B  
ON A.X = B.X

The resulting data set has 6 columns. Sometimes, however, because of the LEFT JOIN, the data in the row from table B is NULL.

How to return * from A but to have an extra (sixth) column in the result set that shows 'Yes' if the join did find a match and 'No' if it didn't?



Solution 1:[1]

Does this work?

SELECT 
  A.*,
  CASE
    WHEN B.X IS NULL THEN 'No'
    ELSE 'Yes'
  END AS BExists
FROM A 
LEFT JOIN B 
ON A.X = B.X

If A.X and/or B.X can be null you might change it to something like this:

SELECT 
  A.*,
  CASE
    WHEN B.Id IS NULL THEN 'No'
    ELSE 'Yes'
  END AS BExists
FROM A 
LEFT JOIN B 
ON ISNULL(A.X,0) = ISNULL(B.X,0)

Solution 2:[2]

SELECT * FROM A 
LEFT OUTER JOIN B ON A.X = B.X

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
Solution 2 RF1991