How to use NOT EXISTS condition in Drupal 8 & 9
$query = db_select('users');
$query->addField('users', 'uid');
$query_exists = db_select('users_roles');
$query_exists->addExpression('NULL');
$query_exists->where("users_roles.uid=users.uid");
$query->notExists($query_exists);
$result = $query->execute();
foreach ($result as $record) {
var_dump($result);
}
By convention, use the following rather using the condition() method:
$query->isNull($field);
$query->isNotNull($field);
$query->exists($field);
$query->notExists($field);
While a condition such as $query->condition($field, NULL, 'IS NOT NULL');
should work, the above convention is recommended.