There are 2 tables in my system: journal and journal_contribution. Both have a field uuid as primary key and the journal_contribution contains the field journal_uuid to link it to a journal. The other fields are not of importance. The journal table has 461283 records and the journal_contribution 336136 records.
I want to determine how many journals I have that do not have a journal_contribution referring to them.
My first idea was the following query
select count(*)
from journal
where uuid not in
(select journal_uuid as uuid
from journal_contribution)
I know this is not very performant but I was surprised that after 2 minutes I got 0 as result. Even more so that if I run the same query without the 'not' I do get as result 124121. If I subtract this from the total number of journals I would have expected the original result to be 337162.
When I change the code to the following more performant version I do get the right result but I want to understand the 0 in the first place. Can somebody explain this to me?
select count(*)
from journal
where not exists
(select 1
from journal_contribution jc
where jc.journal_uuid = journal.uuid)
These queries were done in pgAdmin 4.21 running PostgreSQL 11