Sample SOQL:
List<Account> listAccount = [SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Opportunity)];
system.debug(‘Output is ‘ + listAccount);
Cheers!!!