0

I have these two queries, the second one is returning same ID in multiple records while the first one doesn't. In my mind they both should give the same result, what am I missing here? Can someone help me understand the difference between these two queries.

------------FIRST-----------------

SELECT DISTINCT ID FROM TABLE WHERE KEY = 'TRUE' AND ID NOT IN
   (SELECT DISTINCT ID FROM TABLE WHERE KEY = 'TRUE' AND VALUE = 0));

----------SECOND-----------------

SELECT DISTINCT ID FROM TABLE WHERE KEY = 'TRUE' AND VALUE != 0;
jarlh
  • 42,561
  • 8
  • 45
  • 63
  • The first query takes other rows having the same ID into consideration. The second query only looks at one row at the time. – jarlh Mar 16 '23 at 12:45
  • 1
    Also depending on your DB and how VALUE is set, the value could be NULL and !=0 woudl return NULL values (or any other values that are not 0, a 1 or 2 for example. The first one says value has to = 0. – Brad Mar 16 '23 at 12:57

0 Answers0