My intention is to filter the child records which are != to a field of the master.

Account: Master Application__c : Child

Using Subquery:

select acc.id,acc.Callista_Org_Unit_Code__c , (select Agent_Org_Unit_Code__c from Applications__r)from Account acc 

I would like to add the following filter in the subquery:


This is not allowed in SOQL.

Or alternatively, filter from a child record:

select id,Agent_Org_Unit_Code__c,Agent__r.Callista_Org_Unit_Code__c  from Application__c where Agent__r.Callista_Org_Unit_Code__c!=Agent_Org_Unit_Code__c

This also gives an error.

Is there any way I can build out these filters?


Please note from the SOQL and SOSL Reference that you cannot compare one field to another.

fieldExpression Syntax

The field expression syntax of the WHERE clause in a SOQL query consists of a field name, a comparison operator, and a value that’s used to compare with the value in the field name.

fieldExpression uses the following syntax:

  fieldName comparisonOperator value


The name of a field in the specified object. Use of single or double quotes around the name will result in an error. You must have at least read-level permissions to the field. It can be any field except a long text area field, encrypted data field, or base64-encoded field. It does not need to be a field in the fieldList.

Case-insensitive operators that compare values.

value A value used to compare with the value in fieldName. You must supply a value whose data type matches the field type of the specified field. You must supply a native value—other field names or calculations are not permitted. If quotes are required (for example, they are not for dates and numbers), use single quotes. Double quotes result in an error.

Again, note:

You must supply a native value—other field names or calculations are not permitted.

This is a very common shortcoming of SOQL to need to circumvent. The standard workaround is to use a formula.

You need to create a formula on the child record to basically extend SOQL in this way. Name your formula something like Org_Unit_Code_Matches_Parent__c with an output type of Boolean, then simply set it to:

Agent_Org_Unit_Code__c = Account__r.Callista_Org_Unit_Code__c

The above assumes the lookup field has an API Name of Account__c.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.