How can I query Accounts with at least one closed won Opportunity in Salesforce

green fields near brown mountain
Reading Time: < 1 minutes

In Salesforce, you can query accounts with at least one closed won opportunity by using a SOQL (Salesforce Object Query Language) query. Here’s an example of how you could write this query:

SELECT Id, Name, (SELECT Id, Name, StageName 
  FROM Opportunities 
  WHERE StageName='Closed Won') 
FROM Account
WHERE Id IN (SELECT AccountId FROM Opportunity 
  WHERE StageName='Closed Won')
LIMIT 10

This query returns all accounts that have at least one opportunity in the “Closed Won” stage. The inner query (SELECT Id, Name, StageName FROM Opportunities WHERE StageName='Closed Won') retrieves the closed won opportunities associated with each account, and the outer query (SELECT Id, Name, (...) FROM Account WHERE Id IN (...)) retrieves the account information for accounts with at least one closed won opportunity.

You can use this query in a number of different ways, such as in the Salesforce Developer Console, in Apex code, or in a tool such as the Workbench. Once you have the results, you can use them for further analysis, reporting, or to perform additional actions.

%d bloggers like this: