In this beginner-friendly guide, I’ll walk you through what SOQL is, how it works, and how to write your first query.
🌟 What is SOQL?
SOQL (Salesforce Object Query Language) is a language used to retrieve data from Salesforce’s database. You can use SOQL to:
- Select records from a single object
- Filter those records based on conditions
- Access related records via relationships
Note: Unlike SQL, SOQL doesn’t support INSERT
, UPDATE
, or DELETE
. It is strictly used for querying (read-only).
🛠Basic SOQL Syntax
SELECT field1, field2 FROM ObjectName WHERE condition
📌 Example:
SELECT Name, Industry FROM Account WHERE Industry = 'Technology'
This returns a list of Account records where the Industry is “Technology”, showing only the Name and Industry fields.
🧰 Common Clauses in SOQL
- SELECT – Choose which fields to retrieve
- FROM – Specify the object to query
- WHERE – Filter records
- ORDER BY – Sort the results
- LIMIT – Limit the number of records returned
SELECT Name FROM Contact ORDER BY CreatedDate DESC LIMIT 5
🔗 Working with Relationships
1. Child-to-Parent (dot notation):
SELECT FirstName, LastName, Account.Name FROM Contact
2. Parent-to-Child (subquery):
SELECT Name, (SELECT LastName FROM Contacts) FROM Account
⚖️ SOQL vs SQL – Key Differences
Feature | SOQL | SQL |
---|---|---|
Used With | Salesforce Objects | Relational Databases |
Write Operations | No (Read-only) | Yes (Read/Write) |
Joins | Not supported (use relationships) | Supported |
DML Operations | Handled separately in Apex | Included in SQL |
✅ Best Practices
- Always use selective filters to avoid hitting governor limits.
- Use
LIMIT
while testing queries. - Use Developer Console, Workbench, or VS Code with Salesforce CLI to run and test queries.
- Avoid
SELECT *
. Always specify fields explicitly.
🚀 Your First Steps
Start practicing by querying standard objects like Account, Contact, and Opportunity in your Salesforce Developer Org. Try combining filters, limits, and ordering.
Here’s a simple one to get started:
SELECT Id, Name FROM Opportunity WHERE StageName = 'Prospecting'
💬 Final Thoughts
SOQL is a foundational skill for every Salesforce developer. With just a few keywords, you can access powerful data and insights from your org. Start small, practice regularly, and explore how SOQL fits into Apex, automation, and reporting.
Have you tried writing a SOQL query yet? What challenges did you face? Drop a comment below!
0 Comments
Post a Comment