Are beneficial branches of worldly knowledge regarded as part of shar‘i knowledge and it is stipulated that there be sincerity of intention when pursuing them?
Question

I am about to start postgraduate studies (masters and doctorate) in order to  improve my status among the people. But I heard a hadith from the Prophet (blessings and peace of Allah be upon him) which says “Whoever seeks knowledge in order to argue with the foolish or to show off before the scholars or to attract people’s attention, will be in Hell”. Is worldly knowledge such as history, languages, chemistry and medicine included in this hadith, or is what is meant by knowledge in the hadith religious knowledge only, and if I seek knowledge to improve my position in society, is that haraam?
Praise be to Allah. Firstly:
 The hadith mentioned was narrated by Ibn Maajah (253) from Ibn ‘Umar (may Allah be pleased with him), that the Prophet (blessings and peace of Allah be upon him) said: “Whoever seeks knowledge in order to argue with the foolish or to show off before the scholars or to attract people’s attention, will be in Hell.” Classed as saheeh by al-Albaani in Saheeh Ibn Maajah.
He also narrated it (254) from Jaabir ibn ‘Abdillah (may Allah be pleased with him). This version says: “Do not seek knowledge in order to show off in front of the scholars or to argue with the foolish, and do not choose the best seat in a gathering because of it, for whoever does that, the Fire, the Fire (awaits him).” Classed as saheeh by al-Albaani in Saheeh Ibn Maajah.
What is meant by knowledge in this hadith and others is religious knowledge.  This is what is referred to in principle, in the context of praise and respect, when it is mentioned in the religious texts.
That knowledge refers to the texts of the Lawgiver, which are the basis for accountability, and those who acquire this (religious) knowledge are the heirs of the Prophets, as  the Prophet (blessings and peace of Allah be upon him) said: “The scholars are the heirs of the Prophets.” Narrated by Abu Dawood (3641) and others; classed as saheeh by al-Albaani.
Shaykh ‘Abd al-‘Azeez ibn Baaz (may Allah have mercy on him) said: The word knowledge refers to many things, but as used by the scholars of Islam, what is meant by knowledge is religious knowledge. This is what is referred to in the Book of Allah and the Sunnah of His Messenger (blessings and peace of Allah be upon him) when the word is used without qualification. End quote.
Majmoo‘ Fataawa Ibn Baaz (2/302).
Secondly:
What we have mentioned, that the knowledge referred to in the hadith is religious (shar‘i) knowledge, does not mean that worldly branches of knowledge are not praiseworthy in all cases, or are not to be sought. Rather the praiseworthy branches of worldly knowledge are to be sought, because the interests of the ummah depend on them and need them.
Abu Haamid al-Ghazaali (may Allah have mercy on him) said:
Discussion of that knowledge which is a communal obligation:
You should understand that learning what is obligatory cannot be done unless we discuss branches of knowledge, and branches of knowledge in the context we are discussing here may be divided into religious knowledge and non-religious knowledge. What I mean by religious knowledge is what is taught by the Prophets (blessings and peace of Allah be upon them), which cannot be attained by reasoning, like mathematics, or by experience, like medicine, or by listening, like language.
Non-religious branches of knowledge may be divided into those which are praiseworthy, those which are blameworthy, and those which are permissible. Praiseworthy branches of knowledge are those which are connected to worldly interests, such as medicine and mathematics. These may be further divided into those which are a communal obligation (fard kifaayah) and those which are good to learn, but are not obligatory. As for those which are a communal obligation, this refers to knowledge that we cannot do without in putting our worldly affairs in order, such as medicine, which is essential to physical well-being, and mathematics, which is essential for business transactions, division of bequests and inheritances, and so on. If a community is lacking anyone who has this knowledge, that will cause problems to the members of that community, but if one person undertakes to acquire and use this knowledge, that is sufficient, and the obligation is waived from the others.
No one should be surprised if we say that medicine and mathematics are communal obligations, for the basic knowledge of various industries and crafts are communal obligations, such as agriculture, weaving and training horses, and even hijaamah (cupping) and sewing. If a community has no one who is skilled in hijaamah, they will soon become ill, and they will be exposing themselves to sickness and death. End quote.
Ihyaa’ ‘Uloom ad-Deen (1/16).
Thus it is clear that among the branches of worldly knowledge are some which are praiseworthy and needed, and are akin to skills in agriculture, trade and so on. They are required for worldly interests, and people learn and use them for that reason. There is nothing wrong with earning a living by means of them, because they are not like acts of worship that are done solely to draw close to Allah.
Thirdly:
If someone learns any of these permissible worldly branches of knowledge, in principle he can refine his intention in doing so, by making it to achieve good for himself and others, or to meet the needs of the needy, or to help the weak, or to free his community of the need to rely on others, or other aims which are praiseworthy from an Islamic point of view. To the extent that his intention is good and sound, he is to be praised for what he does and will be rewarded for it, if Allah wills, even though that this action is permissible in principle. If he does not have that noble intention, he is not regarded as sinning and is not to be blamed.
It says in Fataawa al-Lajnah ad-Daa’imah:
Any religious knowledge, and the means that help to attain it, is included in that for which Allah will raise in rank the one who learns it, acts upon it and does it sincerely for His sake. This is the primary meaning of the word knowledge, and any worldly knowledge that the ummah needs and on which its survival depends – such as medicine, agriculture, manufacturing and so on – may also be included, if the intention is good and the one who learns and acts upon that knowledge intends to benefit the Muslim ummah, support it, raise it in status and make it independent of any need for help from disbelieving, misguided nations. But this is a secondary meaning, and the status of each branch of knwoledge varies according to how closely it is related to religion, how strong its benefit is and how effective it is in meeting the ummah’s needs. End quote.
Fataawa al-Lajnah ad-Daa’imah (12/77).
And Allah knows best.
See also the answer to question no. 10675.