Difference between SOQL and SOSL

Difference between SOQL and SOSL

If you have a need to search the salesforce database of your instance to find a record but don’t know whether to use Salesforce Object Query Language (SOQL) or Salesforce Object Search Language (SOSL) then this article will help guide you on which one to select and what the differences are between the two.

First, let’s see the definition of both.

What is SOQL?

Salesforce Object Query Language or SOQL can be used to search for data records in your salesforce database. It is very similar to the Structured Query Language SQL that is commonly used to query databases.

A SOQL query is the equivalent of a SELECT SQL statement and searches the org database.

Syntax

SELECT and FROM statement are required in any SOQL query syntax followed by one or more optional clauses, such as TYPEOFWHEREWITHGROUP BY, and ORDER BY. The SOQL SELECT statement uses the following syntax:

SELECT *Field Names or Aggregate Functions*  FROM *Object Name*

WHERE *Filter Conditions (Optional)*
GROUP BY *Field Name To Group By (Optional)*
ORDER BY *Field Name to Order By ASC or DESC (Optional)*
LIMIT *Number of records needed to be returned*

Example

SELECT Id, LastName, FirstName FROM Contact
WHERE FirstName = ‘Joe’
ORDER BY LastName, FirstName

What is SOSL?

Salesforce Object Search Language or SOSL can be constructed to find text keywords against the database. SOSL Queries scan all Objects but it is better to provide a selective criteria to narrow down the search.

Syntax

 clause is required in any SOSL Query. You can then add optional clauses to filter the query by object type, fields, data categories, and more. You can also determine what is returned.

The SOSL FIND statement uses the following syntax:

FIND *Text Keyword*
IN *Fieldnames (Optional)*
RETURNING *Objects (Optional)*
LIMIT *Number of records returned (Optional)*

Example

FIND {Joe Smith}
IN Name Fields
RETURNING lead(name, phone)

When to use SOQL vs SOSL?

The decision to use SOQL or SOSL depends on  which objects or fields you want to search, plus other considerations.

Difference between SOSL and SOQL

The following table details the key differences between the SOQL and SOSL functions.

Features

SOQL

SOSL

Required Syntax SELECT FIND
Unrelated Objects Searched Single Multiple
Related Objects Searched Multiple Multiple
Objects must be Known Yes No
Fields must be known Yes No
Search within List Views Not Available Available
Big Objects Support Available Not Available
Record Count Available Not Available
Spell Correction Not Available Available
Language Support Not Available Available
Ordered Result Available Available
OFFSET Result Available Available
Speed and Performance Slower Comparatively Optimal, Faster
Multiple Term Search Not Available Available
REST and SOAP support Available Available
REST and SOAP Methods Query Search
Grouped Result Available Not Available
Indexing Synchronous Asynchronous
Search Focus Accurate Relevance and Speec
Search Scope 1 Object at a time Multiple Objects at a time
Total Number of Queries issued 100 20
Total Number of Records retrieved 50000 200
Search Encrypted Fields Not Available Available

CONCLUSION

SOQL and SOSL are both useful tools to use when searching for records in Salesforce however both can be used for different use cases.

Simply put, use SOQL for records where you know exactly which field and object will have the value and use SOSL  if you know the text to search but don’t know where they may be.