Praise be to Allaah.
It is mustahabb to offer condolences to everyone who is
affected by the loss of the deceased, old or young, except a child who has
not reached the age of understanding, or a young woman, to whom only her
mahrams should offer condolences, according to what we said previously in
the answer to question no. 157874.
It says in Kashshaaf al-Qinaa‘, 2/169: It is mustahabb
to offer condolences to the bereaved family, even children and even friends
of the deceased and so on, such as neighbours of the deceased. End quote.
Al-Hajjaawi said in Matn az-Zaad: It is Sunnah to
offer condolences to the bereaved.
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said: He
said “condolences to the bereaved” and not “condolences to the relatives”,
so everyone who is affected by the loss, even if he is not related, may be
offered condolences. And everyone who is not affected by the loss, even if
he is a relative, may not be offered condolences. So whoever is affected
should be offered condolences and whoever is not affected should not be
offered condolences.
End quote from ash-Sharh al-Mumti‘, 5/186
In Mawaahib al-Jaleel, 2/230, 231, it says:
Condolences may be offered to old and young, whoever will understand what is
said to him. … It says in al-Madkhal: A man should be offered
condolences for the death of his friend, because he is one of those affected
by the loss. End quote.
And Allah knows best.