SOQL means Salesforce Object Query Language. It is the query language used to read records stored in Salesforce objects such as Account, Contact, Opportunity, Case, and custom objects. In Salesforce Apex, SOQL is the main way to retrieve the data your code needs before applying business logic, validations, automation, or user-interface processing.
Salesforce Object Query Language looks similar to SQL, but it is designed for the Salesforce data model. A SOQL query works with Salesforce objects, fields, record IDs, relationship fields, and platform governor limits. It does not support every SQL feature, and it does not update or delete data by itself. For changes to records, Apex uses DML statements after the records are queried.
- Use SOQL when you know which Salesforce object contains the data you need.
- Use field API names, not field labels, in the SELECT clause.
- Use filters such as WHERE, IN, LIKE, and date literals to reduce the number of returned records.
- Use relationship queries instead of SQL-style JOIN syntax when related Salesforce data is required.
- Use the Salesforce Developer Console, VS Code with Salesforce extensions, Trailhead playgrounds, or API tools to test SOQL. Older tools such as Force.com Explorer and Force.com IDE may still appear in legacy tutorials, but new work usually uses the current Salesforce tooling.
What SOQL Means in Salesforce Apex Development
In Apex, a SOQL query returns a list of sObject records or a single sObject record when exactly one row is expected. For example, a query on Account returns Account records. A query on Student__c returns records from a custom object named Student. The returned records can be stored in a list, looped through, displayed in a Lightning component, or used in later Apex logic.
| SOQL concept | How it works in Salesforce |
|---|---|
| Object | The table-like container in Salesforce, such as Account or Student__c. |
| Field | The column-like value on a record, such as Name, Phone, CreatedDate, or Subject1__c. |
| Record | A single row of data, identified by an Id. |
| Relationship | A lookup or master-detail connection between objects, queried through relationship fields and subqueries. |
| Governor limit | A Salesforce runtime limit that controls query count, returned rows, CPU time, heap size, and other shared resources. |
SOQL Features and Limits That Salesforce Developers Must Know
- SOQL retrieves records from one main object at a time.
- SOQL can filter records with WHERE, compare values, use date literals, sort data with ORDER BY, and restrict rows with LIMIT.
- SOQL can query related data through child-to-parent and parent-to-child relationship syntax.
- SOQL supports aggregate functions such as COUNT(), SUM(), MIN(), MAX(), AVG(), and COUNT_DISTINCT().
- SOQL does not support SELECT *; you must name the fields that should be returned.
- SOQL does not use normal SQL JOIN syntax; Salesforce relationship names are used instead.
- SOQL is read-focused. To insert, update, delete, or undelete records, use Apex DML statements.
Salesforce governor limits can change by context and release, so production code should always be checked against the current Salesforce documentation. Common Apex limits to keep in mind are the number of SOQL queries per transaction, the total number of records returned by SOQL, DML statements, DML rows, heap size, callouts, and CPU time. The official Salesforce SOQL documentation is available at Salesforce SOQL and SOSL Reference, and Trailhead introduces the same topic in Get Started with SOQL Queries.
Salesforce Apex SOQL Limits in Practical Code Reviews
- Do not place SOQL queries inside loops. Query the required records once, store them in a list or map, and then process the collection.
- Do not query fields that the code does not use. Selecting unnecessary fields increases heap usage and makes code harder to review.
- Use WHERE clauses on indexed or selective fields when querying large objects.
- Use LIMIT only when the business logic can safely work with a limited result set.
- Handle empty query results before accessing a list element.
- Use bind variables in Apex instead of building unsafe query strings wherever possible.
SOQL SELECT Query Syntax for Salesforce Records
A basic SOQL statement has three important parts: SELECT, FROM, and optional filter or sorting clauses. SELECT lists the fields to return. FROM names the Salesforce object. WHERE, ORDER BY, and LIMIT narrow the result.
SELECT Field1, Field2
FROM ObjectName
WHERE FilterField = 'Filter Value'
ORDER BY CreatedDate DESC
LIMIT 10
Keywords such as SELECT and FROM are commonly written in uppercase for readability. Object and field API names must be typed correctly. Custom objects and custom fields end with __c, for example Student__c and Subject1__c.
First SOQL Query on the Standard Account Object
The following query fetches the record ID and account name from the standard Account object.
Select Id, Name From Account
In this statement, Id and Name are fields, and Account is the Salesforce standard object. The query returns Account records with only those selected fields available in the result.
SOQL Query on a Salesforce Custom Object
To query a custom object, use the object’s API name. Custom object API names end with __c. Custom field API names also end with __c.
SELECT Name, Subject1__c FROM Student__c
In this example, Student__c is the custom object. The query returns the record name and the custom field Subject1__c. If the field label shown in Setup is “Subject 1”, the API name used in SOQL can still be Subject1__c, so always verify API names before writing the query. You can learn this step in How to know API names for Objects and Field?
SOQL WHERE Clause Examples for Salesforce Filters
The WHERE clause is used to return only the records that match a condition. This keeps Apex code efficient and helps avoid querying more data than needed.
SELECT Id, Name, Industry
FROM Account
WHERE Industry = 'Technology'
Use comparison operators for numeric, date, text, checkbox, and picklist fields. Common operators include =, !=, <, <=, >, >=, LIKE, IN, and NOT IN.
SELECT Id, Name, CreatedDate
FROM Account
WHERE CreatedDate = LAST_N_DAYS:30
ORDER BY CreatedDate DESC
Date literals such as TODAY, YESTERDAY, LAST_N_DAYS:30, and THIS_MONTH make SOQL filters easier to read and maintain than hard-coded dates when the logic is relative to the current date.
SOQL IN, LIKE, ORDER BY, and LIMIT in Salesforce Queries
SOQL provides several clauses that are frequently used in real Apex code. The IN operator checks whether a field matches one of several values. The LIKE operator is used for pattern matching. ORDER BY sorts records, and LIMIT restricts the number of returned rows.
SELECT Id, Name, Rating
FROM Account
WHERE Rating IN ('Hot', 'Warm')
ORDER BY Name ASC
LIMIT 20
SELECT Id, Name
FROM Contact
WHERE Email LIKE '%.com'
When using LIKE, the percent sign (%) works as a wildcard for zero or more characters. Pattern queries can be useful, but on large data volumes they should be tested carefully because not every pattern is selective.
Using SOQL in Apex Without Querying Inside Loops
In Salesforce Apex, queries are commonly assigned to a list. The safest pattern is to collect IDs or filter values first, run one SOQL query, and then process the returned records.
List<Account> accounts = [
SELECT Id, Name, Industry
FROM Account
WHERE Industry = 'Technology'
];
for (Account acc : accounts) {
System.debug(acc.Name);
}
This pattern uses one SOQL query and then loops through the returned list. Avoid writing a separate SOQL query for each record inside a loop, because that can quickly exceed Apex governor limits.
SOQL Relationship Queries for Salesforce Object Relationships
Salesforce uses relationship names instead of SQL JOIN syntax. Relationship queries are important when data is connected through lookup or master-detail fields. Review the relationship type first in Salesforce Object Relationships: Master-Detail and Lookup.
Child-to-Parent SOQL Query from Contact to Account
A child-to-parent query uses dot notation. For example, Contact has an Account relationship, so a Contact query can return fields from the related Account.
SELECT Id, FirstName, LastName, Account.Name
FROM Contact
WHERE Account.Name != null
Parent-to-Child SOQL Query from Account to Contacts
A parent-to-child query uses a nested SELECT statement and the child relationship name. For Account to Contact, the child relationship name is usually Contacts.
SELECT Id, Name,
(SELECT Id, FirstName, LastName FROM Contacts)
FROM Account
WHERE Name != null
For custom relationships, the child relationship name can be different from the object name. Check the lookup or master-detail field definition in Setup before writing the parent-to-child subquery.
SOQL Aggregate Functions for Counts and Summary Values
SOQL aggregate functions return summary values instead of normal sObject records. They are useful for counts, totals, minimum values, maximum values, and grouped reporting logic in Apex.
- COUNT() counts rows.
- COUNT(fieldName) counts rows where the field has a value.
- COUNT_DISTINCT(fieldName) counts unique values.
- SUM(fieldName) returns the total of a numeric field.
- MIN(fieldName) returns the smallest value.
- MAX(fieldName) returns the largest value.
- AVG(fieldName) returns the average value for a numeric field.
SELECT Industry, COUNT(Id) totalAccounts
FROM Account
WHERE Industry != null
GROUP BY Industry
HAVING COUNT(Id) > 5
Aggregate queries in Apex return AggregateResult values. Use the alias name, such as totalAccounts, to read the grouped result in Apex.
SOQL Compared with SQL and SOSL in Salesforce
| Question | SOQL answer |
|---|---|
| Is SOQL the same as SQL? | No. SOQL is similar to SQL SELECT syntax, but it is built for Salesforce objects and relationships. |
| Can SOQL search across many objects at once? | Not in the same way as SOSL. SOQL queries one main object. SOSL is better for text search across multiple objects. |
| Can SOQL update records? | No. SOQL retrieves records. Apex DML statements perform insert, update, delete, and undelete actions. |
| Can SOQL use SELECT *? | No. You must list the fields you want to retrieve. |
| Can SOQL join tables? | SOQL does not use SQL JOIN syntax. It uses Salesforce relationship query syntax. |
Writing Your First SOQL Statement in Salesforce Developer Tools
Salesforce Object data can be retrieved using SOQL queries. For current Salesforce practice, the Developer Console Query Editor and VS Code with Salesforce extensions are commonly used to test queries. Older tutorials may mention Force.com Explorer and Force.com IDE. If you are following an older Salesforce org setup, those names may appear, but new learners should be comfortable with the Developer Console and current Salesforce CLI-based tools.
- Open Developer Console from the Salesforce setup gear menu.
- Use the Query Editor tab to run a SOQL statement.
- Select “Use Tooling API” only when querying Tooling API objects.
- Copy a working query into Apex only after checking object and field API names.
- Start with a small LIMIT while learning, then remove or adjust it based on the actual requirement.
Learn SOQL (Salesforce Object Query Language) – APEX Tutorials.

Salesforce Developer Tutorials – SOQL Basics
- How to Enable Developing Mode in Salesforce?
- Overview About developer Console.
- SOQL Syntax and Classes.
- How to write First SOQL Statement in Force.com Explorer?
- How to know API names for Objects and Field?
SOQL Statement Types Covered in This Salesforce Tutorial Series
SOQL Basic Clauses for Salesforce Object Queries
- The Alias Notation.
- The WHERE Clause.
- IN Operator.
- ORDER BY clause.
- INCLUDES and EXCLUDES operators for multi-select picklists.
- Comparison Operators.
- Equals.
- Not Equals.
- Less Than.
- Less Than or Equal to.
- Greater than.
- Greater than or equal to.
- Like.
SOQL Advanced Relationship and Filtering Statements
- SOQL Relationships.
- Relationships between Standard Objects.
- Relationships between Custom Objects.
- Many-to-One Relationships (n:1) in SOQL.
- Semi-Join and Anti-Join.
- Relationship query patterns that replace SQL-style joins.
- Filtering multi-select picklist values.
- Escape sequences in string filters.
- Date formats and date literals.
- GROUP BY ROLLUP clause.
- FOR REFERENCE clause.
- FOR VIEW clause.
SOQL Aggregate Functions and HAVING Clauses
- GROUP BY Clause.
- COUNT ( ).
- COUNT (FIELD_NAME).
- COUNT_DISTINCT (FIELD_NAME).
- SUM (FIELD_NAME).
- MIN (FIELD_NAME).
- MAX (FIELD_NAME).
- AVG (FIELD_NAME).
- HAVING Clause.
SOQL Tutorial Editorial QA Checklist
- Confirm every SOQL example uses valid Salesforce object and field API names.
- Check that custom object and custom field examples use the __c suffix correctly.
- Verify that any Apex example avoids SOQL inside loops.
- Check whether LIMIT is used for learning convenience or for a real business rule.
- Review relationship query examples against the actual relationship names in the Salesforce org.
- Keep Salesforce governor limit references aligned with the current official Salesforce documentation.
SOQL Salesforce Object Query Language FAQs
What is SOQL in Salesforce?
SOQL, or Salesforce Object Query Language, is the query language used to retrieve records from Salesforce objects. It is used in Apex, Developer Console, APIs, and other Salesforce development tools.
How is SOQL different from SQL?
SOQL is similar to SQL SELECT syntax, but it is built for Salesforce objects, fields, and relationships. It does not support SELECT *, normal SQL JOIN syntax, or every advanced SQL feature.
What is the difference between SOQL and SOSL?
SOQL is used when you know the object you want to query. SOSL is used for text search across multiple objects and fields. For example, use SOQL to query Accounts by Industry, and use SOSL when searching a keyword across several searchable objects.
How do I query a custom object in SOQL?
Use the custom object API name ending in __c. For example, SELECT Name FROM Student__c queries a custom object named Student. Custom fields also use the __c suffix.
Can SOQL update or delete Salesforce records?
No. SOQL only retrieves records. To update or delete records in Apex, first query the records with SOQL and then use DML statements such as update or delete.
In this Salesforce Developer tutorial, you learned what SOQL is, how Salesforce Object Query Language queries are written, how standard and custom objects are queried, and how SOQL fits with Apex code. Continue with enabling development mode in Salesforce, SOQL Syntax, and the Developer Console tutorials to practise the examples in your own Salesforce org.
TutorialKart.com