# Write a SOQL query to fetch Accounts that have NO related Contacts (Anti-Join using WHERE Id NOT IN (SELECT AccountId FROM Contact)).

**Topic:** Development · **Difficulty:** Easy · **Asked at:** Deloitte, Cognizant, Capgemini, Wipro · **Tags:** soql, scenario, anti-join

Read it on the site: https://www.forcechallenges.com/interview-questions/write-a-soql-query-to-fetch-accounts-that-have-no-related-contacts-anti-join-using-where-id-not-in-select

## Answer

Use an anti-join: `WHERE Id NOT IN (SELECT AccountId FROM Contact)`.

```soql
SELECT Id, Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM Contact)
```
Returns Accounts with no Contacts. Note the subquery results are limited by the platform for semi/anti-joins, and `AccountId` is a lookup so the clause is valid.

---
Source: ForceChallenges · last updated 2026-10-19
