Development questions

Write a SOQL query to fetch Accounts along with only their Closed Won Opportunities in a subquery (SELECT Id, Name, (SELECT Id, Amount FROM Opportunities WHERE StageName = 'Closed Won') FROM Account).

DevelopmentMediumAccentureDeloitteTCS

Filter the child records inside the subquery's own WHERE clause. The parent Accounts are all returned, but each Account's Opportunities list contains only Closed Won rows.

soql
SELECT Id, Name, (SELECT Id, Amount FROM Opportunities WHERE StageName = 'Closed Won') FROM Account

To return only Accounts that have a Closed Won Opportunity, add WHERE Id IN (SELECT AccountId FROM Opportunity WHERE StageName = 'Closed Won') to the outer query.

soqlscenarioparent-to-childsubquery