Newer Version Available

This content describes an older version of this product. View Latest

Using null in SOQL Queries

You can search for null values by using the null keyword.

Use null to represent null values in SOQL queries.

For example, the following statement would return the account IDs of all events with a non-null activity date:
1SELECT AccountId
2FROM Event
3WHERE ActivityDate != null
If you run a query on a boolean field, null matches FALSE values. For example, if Test_c is a boolean field, then the following query returns all the account records where Test_c is false:
1SELECT Id, Name Test_c
2FROM Account
3WHERE Test_c = null

The clause WHERE Test_c = null has the same effect as WHERE Test_c = false. The clause WHERE Test_c != null has the same effect as WHERE Test_c = true.

Null values in WHERE clauses
The WHERE clause behaves in two different ways, depending on the version, when handling null values in a parent field for a relationship query. In a WHERE clause that checks for a value in a parent field, if the parent does not exist, the record is returned in version 13.0 and later but is not returned in versions before 13.0
1SELECT Id
2FROM Case
3WHERE Contact.LastName = null
Case record Id values are returned in version 13.0 and later, but are not returned in versions before 13.0.
Null values in aggregate functions
Aggregate functions in SOQL queries ignore null values, except for COUNT() and COUNT(Id). COUNT(fieldname) ignores null values.