If you want to become a Salesforce Admin or Developer, learning SOQL is absolutely essential.
Almost every Salesforce project uses SOQL queries somewhere. Whether you are writing Apex code, building integrations, creating Lightning Web Components, or debugging records, you will regularly work with SOQL.
However, many beginners struggle because SOQL initially looks similar to SQL, yet Salesforce has its own syntax and rules.
The good news is that SOQL becomes much easier once you start practicing with real examples.
In this beginner-friendly guide, you will learn:
- what SOQL is,
- basic SOQL syntax,
- filtering queries,
- relationship queries,
- aggregate queries,
- and real SOQL examples commonly used in Salesforce projects.
Additionally, this guide will help you understand how SOQL is used inside Apex, Workbench, Developer Console, and Lightning Web Components.
If you are still setting up your Salesforce development environment, you should also read:
- Salesforce Workbench Tutorial for Beginners (2026 Guide)
- Salesforce Developer Console Tutorial for Beginners
- Salesforce CLI Installation and Setup Guide
- Apex Trigger Tutorial for Beginners in Salesforce (2026 Guide)
- Lightning Web Components (LWC) Tutorial for Beginners in Salesforce (2026 Guide)
These topics are closely connected with SOQL development.
What Is SOQL in Salesforce?
SOQL stands for Salesforce Object Query Language.
It is a query language used to retrieve data from Salesforce objects.
In simple words, SOQL helps you fetch records from Salesforce databases.
For example:
SELECT Id, Name FROM Account
This query retrieves:
- Account Id
- Account Name
from the Account object.
Although SOQL looks similar to SQL, it is designed specifically for Salesforce’s object-based architecture.
Why SOQL Is Important
SOQL is one of the most important Salesforce development skills because developers use it everywhere.
SOQL is commonly used in:
- Apex classes,
- Apex triggers,
- Lightning Web Components,
- integrations,
- APIs,
- Workbench,
- and Developer Console.
Without SOQL, retrieving Salesforce data becomes extremely difficult.
Basic SOQL Syntax
The basic SOQL syntax is:
SELECT Fields
FROM Object
WHERE Condition
Main Parts of SOQL
| Clause | Purpose |
|---|---|
| SELECT | Fields to retrieve |
| FROM | Object name |
| WHERE | Filter records |
| ORDER BY | Sort results |
| LIMIT | Restrict records |
Simple SOQL Query Examples
Fetch All Accounts
SELECT Id, Name FROM Account
This retrieves all Account records.
Fetch Contacts
SELECT Id, FirstName, LastName FROM Contact
This retrieves Contact names.
Fetch Opportunities
SELECT Id, Name, Amount FROM Opportunity
This retrieves Opportunity records with Amount values.
Using WHERE Clause in SOQL
The WHERE clause filters records.
Example: Filter Accounts by Industry
SELECT Id, Name
FROM Account
WHERE Industry = 'Technology'
Only Technology Accounts are returned.
Example: Filter Opportunities by Amount
SELECT Name, Amount
FROM Opportunity
WHERE Amount > 10000
This retrieves Opportunities greater than 10,000.
Using LIKE in SOQL
LIKE helps search partial values.
Example
SELECT Name
FROM Account
WHERE Name LIKE 'Sales%'
This retrieves Accounts starting with “Sales”.
Using LIMIT in SOQL
LIMIT restricts returned records.
Example
SELECT Name
FROM Contact
LIMIT 5
Only 5 records are returned.
LIMIT is very useful while testing queries.
Using ORDER BY in SOQL
ORDER BY sorts records.
Example
SELECT Name, CreatedDate
FROM Case
ORDER BY CreatedDate DESC
This retrieves newest Cases first.
Using AND and OR Conditions
Example Using AND
SELECT Name
FROM Account
WHERE Industry = 'Banking'
AND BillingCountry = 'USA'
Example Using OR
SELECT Name
FROM Lead
WHERE Status = 'Open'
OR Status = 'Working'
Relationship Queries in SOQL
Relationship queries are extremely important in Salesforce.
There are two types:
- Child-to-Parent
- Parent-to-Child
Child-to-Parent Query
Use dot notation to access parent records.
Example
SELECT Id, FirstName, Account.Name
FROM Contact
This retrieves:
- Contact Name
- Parent Account Name
Parent-to-Child Query
Use subqueries for child records.
Example
SELECT Name,
(SELECT LastName FROM Contacts)
FROM Account
This retrieves:
- Account Name
- Related Contacts
Relationship queries are heavily used in real Salesforce projects.
Aggregate Functions in SOQL
Aggregate functions perform calculations.
COUNT Example
SELECT COUNT()
FROM Contact
Counts total Contacts.
SUM Example
SELECT SUM(Amount)
FROM Opportunity
Calculates total Opportunity revenue.
AVG Example
SELECT AVG(Amount)
FROM Opportunity
Calculates average Opportunity Amount.
GROUP BY in SOQL
GROUP BY groups records.
Example
SELECT Industry, COUNT(Id)
FROM Account
GROUP BY Industry
This groups Accounts by Industry.
HAVING Clause in SOQL
HAVING filters grouped results.
Example
SELECT Industry, COUNT(Id)
FROM Account
GROUP BY Industry
HAVING COUNT(Id) > 5
Only Industries with more than 5 Accounts are returned.
Date Filters in SOQL
Salesforce provides special date literals.
Example: Last 7 Days
SELECT Id, Name
FROM Case
WHERE CreatedDate = LAST_N_DAYS:7
Example: This Month
SELECT Id, Name
FROM Opportunity
WHERE CloseDate = THIS_MONTH
These filters are commonly used in reporting and automation.
Using SOQL in Apex
SOQL is heavily used inside Apex.
Example
List<Account> accounts = [
SELECT Id, Name
FROM Account
WHERE Industry = 'Technology'
];
This retrieves Technology Accounts inside Apex code.
Related guide:
Apex Trigger Tutorial for Beginners in Salesforce.
Dynamic SOQL Example
Dynamic SOQL builds queries at runtime.
Example
String query = 'SELECT Id, Name FROM Account';
List<Account> accs = Database.query(query);
Dynamic SOQL is useful when queries depend on runtime conditions.
Common Beginner SOQL Mistakes
Querying Inside Loops
Bad Example:
for(Account acc : accounts){
Contact con = [
SELECT Id FROM Contact LIMIT 1
];
}
This can hit governor limits.
Always avoid SOQL inside loops.
Selecting Unnecessary Fields
Query only fields you actually need.
Large queries reduce performance.
Forgetting LIMIT During Testing
Without LIMIT, queries may return huge amounts of data.
SOQL Best Practices
To write optimized SOQL queries:
- Query only required fields
- Avoid SOQL inside loops
- Use selective WHERE conditions
- Use LIMIT while testing
- Optimize relationship queries
- Avoid unnecessary large data retrieval
- Use indexed fields whenever possible
Additionally, bulkified SOQL becomes extremely important in Apex development.
Difference Between SOQL and SOSL
Many beginners confuse SOQL and SOSL.
| SOQL | SOSL |
|---|---|
| Retrieves structured records | Searches text across objects |
| One object mainly | Multiple objects |
| Uses SELECT | Uses FIND |
| Better for exact queries | Better for keyword search |
SOSL Example
FIND 'Sales*'
RETURNING Account(Name), Contact(Name)
SOSL is useful for text searching across multiple objects.
Tools for Running SOQL Queries
You can run SOQL queries using:
- Salesforce Workbench
- Developer Console
- VS Code
- Salesforce Inspector
- Apex code
Real-World SOQL Scenarios
Fetch Recently Created Cases
SELECT CaseNumber, Status
FROM Case
WHERE CreatedDate = TODAY
Fetch Top Opportunities
SELECT Name, Amount
FROM Opportunity
ORDER BY Amount DESC
LIMIT 5
Fetch Contacts Without Email
SELECT FirstName, LastName
FROM Contact
WHERE Email = NULL
These are very common real-world queries.
Final Thoughts
SOQL is one of the most important skills in Salesforce development.
Initially, the syntax may look confusing. However, once you start practicing real examples, SOQL becomes much easier to understand.
Start with:
- simple SELECT queries,
- then learn filters,
- relationship queries,
- aggregate functions,
- and Apex integration.
Most importantly, practice regularly inside:
- Workbench,
- Developer Console,
- or VS Code.
Real hands-on experience improves SOQL skills much faster than theory alone.
If you want to continue learning Salesforce development, you should also explore:
- Related tutorials:
- Salesforce Workbench Tutorial for Beginners (2026 Guide)
- Salesforce Developer Console Tutorial for Beginners
- Salesforce Inspector Chrome Extension Guide for Beginners
These topics will strengthen your Salesforce developer skills significantly.