Comparison Operators in SOQL : In our previous Salesforce tutorial we have learned about WHERE clause in SOQl. In this Salesforce Training Tutorial we are going to learn about Comparison operators in SOQL, Comparison operators with SOQL statement examples.

In Salesforce Object Query Language (SOQL), Comparison Operators are used to filter and narrow down search results by comparing fields to specific values. They are essential for creating meaningful queries to fetch only the relevant data from Salesforce objects.

Here are the main SOQL comparison operators:

  1. Equals (=): Returns records where the field matches an exact value.
  • Example: SELECT Id FROM Account WHERE Industry = 'Technology'
  1. Not Equals (!=): Fetches records where the field does not match the specified value.
  • Example: SELECT Id FROM Contact WHERE LastName != 'Smith'
  1. Greater Than (>) and Less Than (<): Used for numeric or date fields to find values greater or less than a specific number or date.
  • Example: SELECT Id FROM Opportunity WHERE Amount > 5000
  1. Greater or Equal (>=) and Less or Equal (<=): Fetches records meeting or exceeding a threshold.
  • Example: SELECT Id FROM Task WHERE DueDate <= TODAY
  1. LIKE: Used for partial matches, with wildcards like % for flexible searching.
  • Example: SELECT Id FROM Account WHERE Name LIKE 'Tech%'

SOQL comparison operators allow users to retrieve precise data sets efficiently, whether by exact matching, range, or pattern matching.

Comparison Operators in SOQL

Comparison operators in SOQL are used to compare a value with another value to return TRUE or FALSE. Different types of comparison operators in SOQL(Salesforce Object Query Language) are

= (Equal)

This operator is used to retrieve records where the field value matches exactly with the provided value.

SELECT Name FROM Account WHERE Industry = 'Technology'

!= (Not Equal)

Fetches the records where the field value does not match the provided value.

SELECT Name FROM Account WHERE Industry != 'Finance'

> (Greater Than) & < (Less Than)

These operators are used to filter records where a field’s value is greater than or less than the specified value.

SELECT Name FROM Opportunity WHERE Amount > 50000

>= (Greater Than or Equal) & <= (Less Than or Equal)

These operators retrieve records where the field’s value is greater than or equal to, or less than or equal to, the given value. They also include records that match the boundary value.

SELECT Name FROM Opportunity WHERE Amount >= 10000

LIKE

Used for partial matching using wildcard characters, where % represents multiple characters.

SELECT Name FROM Contact WHERE Name LIKE 'John%'
      Operator                 Description        
=Equals
!=Not Equals
<Less than
<=Less than or equal to
>Greater than
>=Greater than or equal to
LIKELike

 Equals Operator in SOQL

let us see an example using Equals operator to implement SOQL statements. Using Equal Operator in SOQL can retrieve the data based on criteria and condition equals “=”. 

  • SELECT Firstname, Lastname FROM Contact WHERE firstname = ‘Adarsh
Comparison Operators in SOQL

From above SOQL statement the equals ” = ” checks whether the values of the two operands are equal. If the value is equal, the condition becomes ” TRUE ” . If not the condition Fails.

Not Equals Operator.

Let us see an example using Not equals operator to implement SOQL Statements. Using Not Equals ” != “ data can retrieved based on the condition or Criteria.

  • SELECT firstname, lastname FROM Contact WHERE firstname != ‘adarsh
Comparison Operators in SOQL

From the above SOQL statement the date is retrieved from all the contacts whose first name not equals to Adarsh.

Less than or Equal to Operator in SOQL

Let us see an example on Less than or Equal to operator in SOQL. using this operator we can retrieve records that are less than or equal to the given limit.

  • SELECT name, Fee_paid__c FROM student__c WHERE fee_paid__c  <= 4000.
Comparison Operators in SOQL

 Less than or equal to operator is used to check whether the values of the left operand is less than or equal to the value of the right operand. If the condition satisfies the condition will be TRUE.

From above SOQL statement the students who paid fee less than or equals to 40000 will be filtered.

Less than Operator in SOQL.

Comparison Operators in SOQL
Comparison Operators in SOQL

Greater than or Equal to operator in SOQL

Comparison Operators in SOQL

Greater than Operator in SOQL.

Comparison Operators in SOQL

LIKE operator in SOQL

Comparison Operators in SOQL
Comparison Operators in SOQL

Like operator is used to retrieve the data from the matched substring provided. LIKE operator is also called as pattern matching filtering technique. We have to use ”  % ” character to include names ending with “express