Skip to main content

Master SOQL for Salesforce

SOQL (Salesforce Object Query Language) is a query language used to retrieve record data from a Salesforce database. It is a simple variant of SQL data query language where you are querying data.

Creating SOQL queries is similar to building a report. When you create a report, you ask questions like:

  • What fields do I want to display?
  • Where can I find these fields?

These are the same types of questions you ask when constructing a SOQL query. In a SOQL query, your answers to these questions are the fields and objects you specify.

Where SOQL Can be Used?

SOQL queries can be created and executed throughout the Salesforce ecosystem of data tools and code intefaces including APEX, Developer Console, REST APIs, Dataloader and Einstein.

Apex Code

Inside Apex classes and triggers, developers use SOQL to retrieve Salesforce records programmatically.

Example: Querying Contacts associated with an Account in Apex.

apexCopyEditList<Contact> contacts = [SELECT Id, Name, Email FROM Contact WHERE AccountId = :accountId];

Salesforce Developer Console

  • The Query Editor in the Developer Console allows admins and developers to run SOQL queries to inspect and analyze data in real-time.

Salesforce Workbench

  • SOQL can be executed in Workbench under the “Queries” tab to fetch data from objects without writing Apex code.

REST & SOAP API Queries

  • SOQL is used in API calls when fetching Salesforce data via REST API (using /query endpoint) or SOAP API.

Example REST API query:

GET /services/data/vXX.X/query?q=SELECT+Name,Id+FROM+Account+WHERE+Industry='Finance'

Salesforce Data Loader & Other ETL Tools

  • Some ETL tools like Data Loader, MuleSoft, Talend, and DBAmp allow SOQL-based queries to extract Salesforce data.

Einstein Analytics (Tableau CRM)

  • When integrating with Tableau CRM (formerly Einstein Analytics), SOQL is used to define datasets.

How do you create a SOQL Query?

Writing a SOQL query follows a SQL-like syntax, using the SELECT statement to retrieve specific fields from Salesforce objects, with optional WHERE, ORDER BY, and LIMIT clauses for filtering and sorting. For example, SELECT Name, Email FROM Contact WHERE LastName = 'Smith' LIMIT 10 fetches the name and email of up to 10 contacts with the last name “Smith.”

SELECT Name, Email FROM Contact WHERE LastName = 'Smith'

Here’s what’s happening:

  • SELECT and FROM are SOQL keywords.
  • Name and Email are fields within the object.
  • Lead is the standard Salesforce object you’re querying.

This query retrieves (or “gets”) specific information about leads in the Salesforce database. To break it down:

  • SELECT specifies the fields you want to retrieve.
  • FROM indicates the object you’re querying from (in this case, the Lead object).

Let’s dive deeper and explore the key components of a SOQL query and how they work together!

Other SOQL Articles and Resources

Free Workbench to practice SOQL

SOQL NOT LIKE: Filtering Out Unwanted Data

SOQL SELECT All Fields: How to Retrieve Every Field in Salesforce

SOQL ORDER BY: Guide to Sorting your Salesforce Queries