1

I want to write a query to retrieve accounts that don't have any open opportunities.

I was looking at nested queries, so I wanted to run something like this:

Select id from Account WHERE id  = (SELECT AccountId from Opportunity where IsClosed = false)
4
  • 1
    Are you receiving any error during query execution..then please mention that also in your question. It will help community users to understand the issue in your side.
    – Raj
    Sep 26, 2021 at 11:10
  • @MohanRaj tells me unknown Error Parsing Query
    – Karen
    Sep 26, 2021 at 11:36
  • 1
    Hi, welcome to SFSE. Please take a tour and read How to Ask. I'd be great if you could edit your question and share more details about your problem, like a bigger piece of code and the exact error message you receive. Also, if you struggle with basic Apex code, I'd recommend you an Apex Basics & Database Trailhead module. Cheers! Sep 26, 2021 at 11:49
  • What is the question? Sep 26, 2021 at 20:42

2 Answers 2

4

You need to write a query with IN for receiving account id list from your nested query SELECT AccountId from Opportunity where IsClosed = false.

So your query will be followingly,

SELECT Id FROM Account WHERE Id IN (SELECT AccountId from Opportunity WHERE IsClosed = false)

And refer this link to learn more about SOQL queries.

2

You may want NOT IN in this case; this will find any accounts with no open opportunities (including those with no opportunities at all):

Select id from Account WHERE Id NOT IN (
  SELECT AccountId from Opportunity where IsClosed = true
)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .