# Write a SOQL query to find all Contacts where the Contact's OwnerId is different from its Parent Account's OwnerId (Explain why SOQL cannot directly compare two fields like WHERE OwnerId != Account.OwnerId and how to solve it via a Formula checkbox field).

**Topic:** Development · **Difficulty:** Hard · **Asked at:** Salesforce, Deloitte, Infosys, PwC · **Tags:** soql, scenario, limitations, formula-fields

Read it on the site: https://www.forcechallenges.com/interview-questions/write-a-soql-query-to-find-all-contacts-where-the-contact-s-ownerid-is-different-from-its-parent-account-s

## Answer

SOQL cannot compare two fields in a WHERE clause (e.g. `WHERE OwnerId != Account.OwnerId` is invalid); filters compare a field with a literal or bind variable only.

**Solutions:**
1. Create a formula checkbox on Contact, e.g. `Owner_Differs__c = OwnerId <> Account.OwnerId`, then query `WHERE Owner_Differs__c = true`. Formula fields aren't indexed, so it may be non-selective on large data.
2. Query both fields and compare in Apex:
```apex
for (Contact c : [SELECT Id, OwnerId, Account.OwnerId FROM Contact WHERE AccountId != null]) {
    if (c.OwnerId != c.Account.OwnerId) { /* mismatch */ }
}
```

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