Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Compare Two Values When One Is Null
In PostgreSQL, comparing two values when one or both can be NULL requires careful handling because NULL represents an unknown value. Standard equality checks like = and != will return NULL instead of TRUE or FALSE when NULL is involved.
1. Why NULL Is Tricky
In SQL, NULL means “unknown,” so expressions like NULL = NULL or NULL != value don’t behave as you might expect. Instead of TRUE or FALSE, the result is NULL—which behaves like FALSE in WHERE clauses.
2. Using COALESCE
COALESCE() is a useful function that replaces NULL with a default value. For example:
SELECT COALESCE(a, '') = COALESCE(b, '') FROM table_name;This treats NULL as an empty string (or another default), making the comparison straightforward.
3. Using IS DISTINCT FROM
PostgreSQL offers the IS DISTINCT FROM operator, which treats NULL values as comparable:
SELECT a IS DISTINCT FROM b FROM table_name;This returns TRUE if the values are different—including when one is NULL and the other is not.
To check for equality, you can use:
SELECT NOT (a IS DISTINCT FROM b) FROM table_name;4. Using NULL-Safe Functions
You can also write a custom check:
SELECT (a = b) OR (a IS NULL AND b IS NULL) FROM table_name;This explicitly handles both values being NULL as equality.
Summary
- Use
COALESCE()to replaceNULLwith a default. - Use
IS DISTINCT FROMfor a built-in,NULL-safe comparison. - Use custom logic like
(a = b) OR (a IS NULL AND b IS NULL)if you want full control.
By understanding how NULL works and using the right tools, you can safely compare values in PostgreSQL and avoid unexpected query results.