Postgres excludes NULL in WHERE id != int query

Your "work around" is the usual way to do it. Everything is behaving as expected.

The reason is simple: nulls are neither equal, nor not equal, to anything. This makes sense when you consider that null means "unknown", and the truth of a comparison to an unknown value is also unknown.

The corollary is that:

  • null = null is not true
  • null = some_value is not true
  • null != some_value is not true

The two special comparisons IS NULL and IS NOT NULL exist to deal with testing if a column is, or is not, null. No other comparisons to null can be true.


The IS DISTINCT FROM predicate exists for this purpose. It's described as:

not equal, treating null like an ordinary value

So just doing id IS DISTINCT FROM 1 should work.

Reference: https://www.postgresql.org/docs/11/functions-comparison.html