SOQL Query: Campaigns With Their Campaign members
Parent-to-child SOQL that returns campaigns together with their campaign members, plus the relationship-name rules for custom objects.
Updated 2026-09-09
One query, two levels: a subquery in the SELECT list pulls each campaign's campaign members along with the parent row - no second call, no manual joining.
Campaign records are low volume but heavily rolled up, so aggregate queries here are cheap and safe to run in production.
Parent to child (subquery)
The subquery uses the relationship name (CampaignMembers), not the child object's API name. For custom objects the relationship name ends in __r.
SELECT Id, Name, Type,
(SELECT Id, Status FROM CampaignMembers)
FROM Campaign
LIMIT 50Filter parents by a child condition
SOQL has no JOIN, but a semi-join with IN gets you the same result: query the child, feed the IDs into the parent filter.
SELECT Id, Name, Type
FROM Campaign
WHERE Id IN (
SELECT CampaignId FROM CampaignMember
WHERE CreatedDate = LAST_N_DAYS:30
)Relationship naming rules
- Child to parent uses the lookup field with
__rfor custom fields:Account.Name,Custom_Lookup__r.Name. - Parent to child uses the child relationship name, which is plural for standard objects and
__r-suffixed for custom ones. - You can dot-walk up to five levels toward a parent, but only one level down in a subquery.
Common mistakes to avoid
- Use the API name
Campaign, not the UI label - the two differ on several standard objects. - Wrap literal text values in single quotes; date literals such as
LAST_N_DAYS:30must stay unquoted. - Run the query in a sandbox first if it feeds a bulk update - a
WHEREclause that is one character off can select the whole table. - Subquery rows count toward the total row limit, so a parent query with large child sets can blow past it unexpectedly.
- The relationship name is set on the lookup field, and admins rename it - check the field definition rather than guessing.
Run it faster with TurboKit
Relationship queries are where autocomplete stops being a convenience and starts being the only practical way to write the query.
TurboKit's AI SOQL builder lets you describe the result you want in plain English, returns the query, and exports the rows to CSV or Excel in the same panel - without leaving the Salesforce tab you are already on.
Frequently asked questions
- Does SOQL support JOIN?
- No. SOQL replaces joins with relationship traversal (dot notation upward, subqueries downward) and semi-joins using
INwith an inner query. - How many levels can I traverse?
- Five levels from child to parent in a single query, and one level of parent-to-child subquery. Deeper structures need multiple queries.
- Where do I find the relationship name?
- Setup, then the lookup or master-detail field on the child object - the Child Relationship Name field. TurboKit's metadata viewer shows the same value inline on the record page.
More from the SOQL Library
- SOQL GROUP BY: Campaigns by TypeAggregate SOQL that breaks campaigns down by Type, with COUNT, SUM, and the AggregateResult alias rules.
- SOQL Query for Campaigns Created in the Last 30 DaysSOQL for campaigns created in the last 30 days, plus the date literals (TODAY, THIS_MONTH, LAST_N_DAYS) that replace hard-coded dates.
- SOQL Query for Campaigns Modified TodaySOQL for campaigns changed today, including LastModifiedBy, audit-field filters, and how to spot integration-driven updates.
- SOQL Query for the Latest Campaigns (ORDER BY and LIMIT)Return the most recent campaigns with ORDER BY and LIMIT, plus OFFSET paging and the null-ordering options most admins never touch.
- SOQL Query to Count Campaigns in SalesforceCopy-paste SOQL to count campaigns in Salesforce, with variants for filtered counts, COUNT(Id) vs COUNT(), and grouped totals.
- SOQL Query: Accounts With Their ContactsParent-to-child SOQL that returns accounts together with their contacts, plus the relationship-name rules for custom objects.
Related reading
- Admin How-ToHow to Write Salesforce Queries Without Knowing SOQL (AI SOQL)Describe the records you want in plain English and get a runnable SOQL query back - how AI SOQL builders work, what to check before you run the result, and where they still fall short.
- GlossaryWhat Is an AI SOQL Builder in Salesforce?An AI SOQL builder turns a plain-English description of the records you want into a runnable SOQL query, using the schema of the org you are working in so the