select DISTINCT B.`Product#`,
                B.`Contact Email Address`,
CASE when A.`Product#` IS NULL then 'Yes' ELSE 'NO' END  as New customer
from `Table B` as B
LEFT JOIN `Table A` as A 
ON A. `product#`= B.`product#`                  

What wrong in my query. I see lot of product # is repeated. Also it says "Yes" when Product # is there in Table B but none in table A..

  • 1
    Can't really help you if you tables are called TABLE A and TABLE B. At least use a more meaningful table names, or give a description of the relationship between table A and table B.
    – Eric
    Commented Jan 10, 2020 at 23:37
  • 1
    As I see one of the tables contains info about customers, and another about products. But where does the relation between customers and products is stored? Show full DDLs for both tables with description for each field meaning, sample data and the result which you need for this data.
    – Akina
    Commented Jan 11, 2020 at 11:13
  • Putting spaces in variable (table, field) names in SQL is a recipe for disaster and will make porting the application to another RDBMS difficult if ever required. Just use the _ (underscore) character as a word separator - called python_case.
    – Vérace
    Commented Jan 11, 2020 at 18:09

1 Answer 1


Consider using this for the yes/no:

IF(EXISTS( SELECT 1 FROM A WHERE product_no = B.product_no ), 'Yes', 'No') AS NewCustomer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.