How to Compare Two Values When One Is Null in PostgreSQL

Imagine you're comparing two PostgreSQL columns and you want to know how many rows are different. No problem, you think:

select count(1)
from items
where width != height;

Not so fast. If some of the widths or heights are null, they won't be counted! Surely that wasn't your intention. That's where is distinct from comes into play:

select count(1)
from items
where width is distinct from height;

Now, your count will be "null aware" and you'll get the result you want 💥

database icon
Finally, a unified workspace for your SQL development
Get more done, together, with PopSQL and PostgreSQL