# 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).

**Topic:** Development · **Difficulty:** Medium · **Asked at:** Accenture, Deloitte, TCS · **Tags:** soql, scenario, parent-to-child, subquery

Read it on the site: https://www.forcechallenges.com/interview-questions/write-a-soql-query-to-fetch-accounts-along-with-only-their-closed-won-opportunities-in-a-subquery-select-id

## Answer

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.

---
Source: ForceChallenges · last updated 2026-10-19
