If you do need to group the Accounts by name for some reason, and you only want to do this for the 5 accounts that were most recently modified, I think you can use one of the following two options:
Two Queries
List<Account> recentlyModified = [
SELECT ID
FROM Account
WHERE Name LIKE :sql OR name IN:sql1
ORDER BY LastModifiedDate DESC LIMIT 5
];
AggregateResult[] groupedResults = [
SELECT Name
FROM Account
WHERE Id IN :recentlyModified
GROUP BY Name
];
1 Query and a Map
List<Account> recentlyModified = [
SELECT Name
FROM Account
WHERE Name LIKE :sql OR name IN:sql1
ORDER BY LastModifiedDate DESC LIMIT 5
];
//now group the Accounts by Name in a Map
Map<String, List<Account>> groupedResults = new Map<String, List<Account>();
for(Account acc : recentlyModified){
if(groupedResults.containsKey(acc.Name)){
groupedResults.get(acc.Name).add(acc);
}else{
groupedResults.put(acc.Name, new Account[]{acc});
}
}
The above answer by Adrian Larson is correct and I voted for it, but I was in the middle of writing mine, so I figured I'd post it nonetheless as it gives a slightly different approach which can be helpful.