Question: I write a sample query to fetch account Ids from cases of a particular record type.Then i wrote another sample query parent to child relationship to fetch opportunity ids from accounts.

My sample code

```
List<Case> c=new List<Case>();
c=[Select Id,accountId from case where RecordTypeId = '01290000000sF3L'];
List<Id> accountIds= new List<Id>();
List<Account> accounts=new List<Account>();
List<Opportunity> opps=new List<Opportunity>();
for(Case cl:c) 
{
accountIds.add(cl.accountId);
}
accounts=[Select Id,name,(select Id,name from opportunities) from account       
where Id in:accountIds];
System.debug('Data in accounts '+accounts);

```

But my issue is how to get opportunity id from above Soql accounts means how to add those opportunity id's to the list.

```
for(Account ao:accounts) {
//How to fetch Opportunity ids from above account list.
}

```

Answer: 
Most efficient:

```
Set<Id> childIds = new Map<Id, Opportunity>(ao.Opportunities).keySet();

```

You can also loop through them:

```
for (Opportunity child : ao.Opportunities)
{
    // ...
}

```
Not sure how big your org is, but this should get what you're looking for. I would run the actual query part here in workbench first to see how intensive it is and maybe put a filter on the account query. 

```
Set<Id> opptIds = new Map<Id,Opportunity>([
    SELECT Id 
    FROM   Opportunity 
    WHERE  AccountId IN (SELECT Id 
                         FROM   Account) 
        AND AccountId IN (SELECT AccountId 
                          FROM   Case 
                          WHERE  RecordTypeId = '01290000000sF3L') 
]).keyset(); 

```