SOQL in Salesforce
If you are Salesforce Developer working with Apex , understanding SOQL is important.
SOQL in Salesforce
If you are Salesforce Developer working with Apex , understanding SOQL is important.
What is SOQL?
SOQL is Salesforce Object Query Language, used to fetch data from standard and custom objects.
Basic SOQL Syntax:
SELECT Field1, Field2, ... FROM ObjectName WHERE condition ORDER BY Field ASC|DESC
WHERE Clause:
Filter your data using operators like:
- = , != , < , > , LIKE , NOT
- AND , OR , combinations
Example:
SELECT Id, Name FROM Contact
WHERE MailingCity = 'Bengaluru' AND CreatedDate >= 2025-01-01T00:00:00Z
ORDER BY Clause:
Sort results alphabetically or chronologically. Example:
SELECT Name FROM Account ORDER BY Name ASC, CreatedDate DESC
LIMIT and OFFSET
Limit is used to control the number of records returned. Offset is used to skip a specified number of rows in the query result. Example:
SELECT Id, Name FROM Account ORDER BY Name ASC LIMIT 10 OFFSET 5
LIMIT 10 — return only 10 records. OFFSET 5 — skip the first 5 records. So this query will return the 6th to 15th records in the sorted result set. Note: OFFSET works only with LIMIT and supports skipping up to 2000 rows.
Relationships in Query:
In Salesforce, objects are often related through lookup or master-detail relationships, like: Standard : Account(parent) → has many → Contacts(child) Custom : Invoicec(child) → belong to → Customerc(parent)
There are two types of relationships queries in Salesforce. They are,
1. Parent to Child (One to Many):
Use subqueries to fetch related child records from a parent.
Standard Object Example:
Get Accounts with their related Contacts.
SELECT Id, Name,
(SELECT Id, FirstName, LastName FROM Contacts)
FROM Account
WHERE Industry = 'Banking'
Contacts is the child relationship name (visible in Object Manager).
Custom Object Example: Assume Customerc is the parent Invoicec is the child with a lookup relationship to Customer__c
SELECT Id, Name,
(SELECT Id, Name FROM Invoices__r)
FROM Customer__c
WHERE Region__c = 'South'
Invoices__r is the child relationship name defined in the custom relationship.
2. Child to Parent (Many to One):
Use dot notation to access parent fields from the child object.
Standard Object Example:
Get Contacts along with their Account Name.
SELECT Id, FirstName, LastName, Account.Name
FROM Contact
WHERE Account.Industry = 'Banking'
Custom Object Example: Assume Invoicec has a lookup to Customerc
Fetch Invoice with its Customer Name.
SELECT Id, Name, Customer__r.Name
FROM Invoice__c
WHERE Amount__c > 1000
Use r instead of c to access custom relationship fields.
SOQL is a powerful tool once you learn how to traverse relationships, apply filters, sort results, and work with limits and offsets. Follow me for more Salesforce guides!
메타데이터
- post_id
- b3f60f23f1e4
- slug
- soql-in-salesforce-b3f60f23f1e4
- url
- https://medium.com/@rvaisu/soql-in-salesforce-b3f60f23f1e4
- canonical_url
- https://medium.com/@rvaisu/soql-in-salesforce-b3f60f23f1e4
- author_url
- https://medium.com/@rvaisu
- status
- ok
- fetched_at
- 2026-06-25 12:15:08