Edited again to add: the 2nd table ("dogs") have null user_guid values. If i filter out the NULL values then the NOT IN operator works without WHERE. And I still dont know why :|
Edited: Sorry I realized that I flipped the question. The question was the number of users who are in "users" table and NOT in the "dogs" table. My original code was:
select count(distinct u.user_guid)
from users u
where u.user_guid not IN (select d.user_guid
from dogs d)
Which got result of 0 count. The answer was supposed to be 2226
Only when I add the WHEN condition then i got the right answer:
select count(distinct u.user_guid)
from users u
where u.user_guid not IN (select d.user_guid
from dogs d
WHERE d.user_guid = u.user_guid);
I still don't know why the WHERE condition makes the difference here
The question was:
How could you determine the number of unique users in the "users" table who were also in the "dogs" table?
The correct answer was supposed to be
select count(u.user_guid)
from users u
where u.user_guid IN (select d.user_guid
from dogs d
where u.user_guid=d.user_guid);
Now I don't understand the point of the last WHERE filter. I thought the "IN" operator would have already made sure to filter only for values of u.user_guid that also exists in the d.user_guid (ie. the "IN" operator would have ensured that there are matching values) , which is the same as what the WHERE condition did (ie. u.user_guid = d.user_guid). And yet when I run the 2 codes (with and without the last WHERE line), the results were different. I'm not sure where I went wrong :/