-3
SELECT AccountId, Account.name
FROM Opportunity
WHERE StageName LIKE '%Closed%'

I seek a query that works the same but should be called "From Account". Also that returns records that have null as their name or AccountId.

philipxy
  • 14,867
  • 6
  • 39
  • 83
Harsh Verma
  • 1
  • 1
  • 1

1 Answers1

0
SELECT Id, Name
FROM Account
WHERE Id IN (
    SELECT AccountId 
    FROM Opportunity
    WHERE StageName IN ('Closed Won', 'Closed Lost')
)

It's almost verbatim in the documentation, you could have searched bit more.

https://developer.salesforce.com/docs/atlas.en-us.236.0.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_comparisonoperators.htm

(...)the following query returns account IDs if an associated opportunity is lost

SELECT Id, Name 
FROM Account 
WHERE Id IN 
  ( SELECT AccountId
    FROM Opportunity
    WHERE StageName = 'Closed Lost' 
  )
eyescream
  • 18,088
  • 2
  • 34
  • 46