I need a soql query to query to all contacts where lastname = firstname

  • 1
    Welcome to SFSE! Generally speaking, it's a good idea to provide people with things that you've tried so far. People are usually more willing to help, and provide better answers, if you can show what you've tried and what you've read about.
    – Derek F
    Feb 9 '16 at 14:35
  • 1
    I think it's a fine question. It's requirement is simple and IMO doesn't need to be specific. Welcome to SFSE!
    – dphil
    Feb 9 '16 at 14:41

This is not allowed in SOQL. Please refer the below link which confirms the same.


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.

fieldName comparisonOperator value

fieldName 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.

comparisonOperator 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.

As a workaround what you can do is:

  1. Create a formula field with return type as checkbox
  2. Set the formula to true if FirstName = LastName, else set it false
  3. Write a SOQL to query the contacts where the formula field is true

Select Id, FirstName, LastName from Contact where *formula_we_Created* = true

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.