SF-PD2 Process Automation, Logic, and Integration Practice Question
A developer needs to write a SOQL query that retrieves all Account records with their related Opportunities, but only Opportunities that are in the 'Closed Won' stage. Which SOQL query correctly achieves this?
⚠ Common exam trap
The trap here is trying to filter child records in the outer WHERE clause or assuming that a subquery in the WHERE clause filters the child records in the result.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE StageName = 'Closed Won') FROM Account
The correct query uses a subquery in the SELECT clause to retrieve related Opportunities with a filter on StageName. This returns all Accounts and includes only Closed Won Opportunities for each. Filtering child records must be done within the subquery. The other options either incorrectly filter in the outer query, fail to filter the child records, or reference a non-existent field on Account.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE StageName = 'Closed Won') FROM Account WHERE StageName = 'Closed Won'
Why it's wrong here
This query filters Accounts by StageName, but Account does not have a StageName field. StageName is a field on Opportunity. This query would cause a compile error. The correct approach is to filter the child Opportunities within the subquery, not the parent Account. This option misunderstands the object model.
- ✓
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE StageName = 'Closed Won') FROM Account
Why this is correct
This query uses a subquery to retrieve related Opportunities filtered by StageName. The subquery is enclosed in parentheses and placed in the SELECT clause. It correctly returns all Accounts, and for each Account, only the Opportunities that are Closed Won. This is the standard way to query parent-child relationships with filters on the child records.
- ✗
SELECT Id, Name, (SELECT Id, Name FROM Opportunities WHERE StageName = 'Closed Won') FROM Account WHERE Id IN (SELECT AccountId FROM Opportunity WHERE StageName = 'Closed Won')
Why it's wrong here
This query filters Accounts to only those that have at least one Closed Won Opportunity, but it does not filter the child Opportunities themselves. The subquery in the SELECT will still return all Opportunities, not just Closed Won ones. To filter child records, the condition must be inside the subquery. This query is redundant and does not achieve the requirement.
- ✗
SELECT Id, Name, (SELECT Id, Name FROM Opportunities) FROM Account WHERE Opportunities.StageName = 'Closed Won'
Why it's wrong here
This query attempts to filter on a child relationship in the WHERE clause of the outer query, which is not allowed in SOQL. You cannot reference child fields in the outer WHERE clause like that. The correct approach is to filter within the subquery. This query would result in an error or return incorrect results.
About these practice questions
One of 226 original SF-PD2 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Salesforce exam blueprint
This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.