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: 
One quick addition to Himanshu's post - add [] to accounts in the query. This will make the query to fetch 200 records per iteration than 1 record per iteration. You can find this at [Trailhead data modelling](https://trailhead.salesforce.com/en/modules/data_modeling).

However I would suggest not to put SOQL in a for loop. Query it outside as in your code.

```
for(Account[] ao:[Select Id,name,(select Id,name from opportunities) from account       
where Id in:accountIds]) {
   for(Opportunity opp : ao.opportunities)
   {
       //Other Operation you want to perform with every opportunity
       System.debug(opp.Id);
   }
}

```
Just to add in your code:

```
for(Account ao:accounts) {
    opps.add(ao.opportunities);
}

```