How to determine which `Foos` have no associated `Bars` I want to find all Foos that do not have any associated Bars.

Here's the query I'm using.  It's returning no records:

select * from foos f where f.id not in(select b.foo_id from bars b);


However, I know that the foo with id = 1583 has no associated Bars, so there should be at least one result in my previous query.

Can somebody point out a mistake I am making?  Thanks.
There are probably bars that have NULL in foo_id.

select 1 where 55 not in (select 1 union all select null)


The above query returns 0 rows.

The solution is to change the query to use NOT EXISTS instead of NOT IN as follows:

select * from foos f where not exists(select * from bars b where b.foo_id = f.id)


Not only is this safer as you don't have to manually exclude NULLs from the subquery, but also this construct will generally provide better performance. So for the most part, I recommend getting into a habit of doing it this way instead of using NOT IN.