function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mike Lee PCLMike Lee PCL 

SOQL Subquery not working

Hello, I'm trying out various SOQL queries to help get me up to speed and am stumped.

I created the below query to:

1. Get 5 accounts (doesn't matter which ones)
2. For each of these Accounts bring back Opportunity id, name, and Account ID

SELECT id, Name, Account.id from Opportunity WHERE Account.id IN (SELECT Account.id FROM Account LIMIT 5)

When I execute the query I get back "Unknown Error parsing the query".

I found that hardcoding in some valid account ID's instead of using the sub select works. 

Any ideas why this wouldn't work?

Thanks,

Mike
Best Answer chosen by Mike Lee PCL
Michael DsozaMichael Dsoza
Hi,

You can't write LIMIT in inner query...If you want any five accounts opportunity then first query account with limit 5, copy its Id's & paste those ids in below query as,

 SELECT id, Name, Account.id from Opportunity WHERE Account.id IN (SELECT Account.id FROM Account where Id IN ('Id1', 'Id2', 'Id3', 'Id4', 'Id5' ))

Hope it will help you.
Mark it resolved,,,if it resolves your query.

All Answers

venkat-Dvenkat-D
Try below
SELECT id, Name, Accountid from Opportunity WHERE Accountid IN (SELECT id FROM Account LIMIT 5)
Michael DsozaMichael Dsoza
Hi,

You can't write LIMIT in inner query...If you want any five accounts opportunity then first query account with limit 5, copy its Id's & paste those ids in below query as,

 SELECT id, Name, Account.id from Opportunity WHERE Account.id IN (SELECT Account.id FROM Account where Id IN ('Id1', 'Id2', 'Id3', 'Id4', 'Id5' ))

Hope it will help you.
Mark it resolved,,,if it resolves your query.
This was selected as the best answer
Mike Lee PCLMike Lee PCL
Good to know about not being able to use LIMIT in the inner query. Thanks!