You need to sign in to do that
Don't have an account?

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
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
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
SELECT id, Name, Accountid from Opportunity WHERE Accountid IN (SELECT id FROM Account LIMIT 5)
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.