Not Exists

Database

Not Exists

Hi,

 

I have below query where I want to find all distinct records from table 1 where id in ('1', '2','7') but not in table 2.

 

SyntaxEditor Code Snippet

SELECT DISTINCT(ID)FROM TABLE1 A
WHERE ID IN ('1', '2', '7') NOT EXISTS 
                                     (SELECT *
                                     FROM TABLE2 B
                                     WHERE A.ID = B.ID)

 This query gives me error at where clasue in main query, can someone help me with that?

 

Thanks!


Accepted Solutions
Senior Apprentice

Re: Not Exists

You got two conditions, there's an AND missing:

SELECT DISTINCT(ID)FROM TABLE1 A
WHERE ID IN ('1', '2', '7') 
  AND NOT EXISTS 
      (SELECT *
       FROM TABLE2 B
       WHERE A.ID = B.ID)
1 ACCEPTED SOLUTION
2 REPLIES
Senior Apprentice

Re: Not Exists

You got two conditions, there's an AND missing:

SELECT DISTINCT(ID)FROM TABLE1 A
WHERE ID IN ('1', '2', '7') 
  AND NOT EXISTS 
      (SELECT *
       FROM TABLE2 B
       WHERE A.ID = B.ID)

Re: Not Exists

Thank you Dnoeth. That works.