SOQL for Beginners: Parent-Child Queries, Filters, and Performance Tips
Learn the most useful SOQL patterns step by step — with small examples you can copy and run right now.
Why this guide?
If you’re new to Salesforce development, SOQL (Salesforce Object Query Language) can look complicated. But at its heart, it’s simple: “find records with filters.”
In this guide, we’ll cover four core patterns that solve most real-world needs:
- Child → Parent: look up fields from a parent record (e.g., Contact → Account).
- Parent → Child: show all children for a parent (e.g., Account → Contacts).
- Has: find parents that have related records.
- Has Not: find parents with no related records.
I will also add basic speed tips and some mini-exercises to practice.
1. Child → Parent (Go “Up” Using Dot Notation)
Use this when the child object has a lookup field to the parent.
Example: Contact has an AccountId field that points to Account.
🔹 Example: Show Contact with its Account’s Name and Industry
SELECT Id, Name, Account.Name, Account.Industry
FROM Contact
WHERE CreatedDate = LAST_N_DAYS:30👉 Reads as: Get Contacts created in the last 30 days, and also pull their Account’s Name + Industry.
2. Parent → Child (Go “Down” Using a Subquery)
Here we use the relationship name (Contacts, Cases, Opportunities, etc.).
🔹 Example: Show Accounts with their Contacts
SELECT Id, Name,
(SELECT Id, LastName, Email FROM Contacts)
FROM Account
LIMIT 50👉 Reads as: List Accounts, and inside each one, also show its Contacts.
3. Has Related Records (Semi-Join)
Think of it as: “Only parents that have children.”
🔹 Example: Accounts that have at least one open Opportunity
SELECT Id, Name
FROM Account
WHERE Id IN (
SELECT AccountId
FROM Opportunity
WHERE IsClosed = FALSE
)👉 Reads as: Get Accounts where at least one Opportunity is still open.
4. Has No Related Records (Anti-Join)
Opposite of the above: parents without children. Perfect for cleanup lists.
Get Mr. Akash’s stories in your inbox
Join Medium for free to get updates from this writer.
🔹 Example: Accounts with no Opportunities
SELECT Id, Name
FROM Account
WHERE Id NOT IN (
SELECT AccountId
FROM Opportunity
)👉 Reads as: Get Accounts that don’t have any Opportunities.
5. Combine Patterns (Powerful Shortcuts)
You can mix child queries and filters together.
🔹 Example: Accounts with open Opportunities, and also show last 3 Cases each
SELECT Id, Name,
(SELECT Id, Subject, Status
FROM Cases
ORDER BY CreatedDate DESC
LIMIT 3)
FROM Account
WHERE Id IN (
SELECT AccountId
FROM Opportunity
WHERE IsClosed = FALSE
)
LIMIT 50👉 Reads as: Find Accounts that have open Opportunities, and list their last 3 Cases.
Beginner Speed Tips (Keep Queries Fast)
1. Filter by indexed fields first:
Id,RecordTypeId,OwnerId,CreatedDate.2. Add a date window:
LAST_N_DAYS:30,THIS_QUARTER, etc. → reduces data scanned.3. Select only fields you need. More fields = slower + more memory.
4. Limit child queries with
LIMIT, or filters likeStatusorCreatedDate.
Common Mistakes to Avoid
❌ Querying inside loops in Apex.
✔ Query once, loop the results.❌ Using leading wildcards like
'%term%'.
✔ Use'term%'when possible.❌ Pulling unlimited children without filters.
✔ Always useLIMITor filters.
Quick Reference
# Child → Parent:
SELECT Id, Name, Account.Name FROM Contact
# Parent → Child:
SELECT Id, Name, (SELECT Id FROM Contacts) FROM Account
# Has (Semi-Join):
SELECT Id FROM Account
WHERE Id IN (SELECT AccountId FROM Opportunity)
# Has Not (Anti-Join):
SELECT Id FROM Account
WHERE Id NOT IN (SELECT AccountId FROM Opportunity)🔍 Master SOQL Like a Pro
Parent/child queries, “has / has not” filters, and speed tricks — these 4 patterns cover 80% of real-world SOQL needs.
👉 Which SOQL pattern do you use the most in your projects? Drop it below — I’d love to hear your go-to query style.If this helped, hit 👏 and share it with your network to help more devs write clean, efficient SOQL.
Let’s Connect!👋
Thanks for reading! I love sharing Salesforce best practices, and I’d be happy to connect with fellow developers, admins, or anyone curious about Salesforce.
📱 Scan the QR Code below to connect with me directly (Gravatar):
Or just drop me a message — always happy to talk Salesforce, automation, and tech 🚀.










