Both righteousness and wisdom are achieved through the fear of God.

Ecclesiastes 7:15-18  In my vain life I have seen everything. There is a righteous man who perishes in his righteousness, and there is a wicked man who prolongs his life in his evildoing. Be not overly righteous, and do not make yourself too wise. Why should you destroy yourself? Be not overly wicked, neither be a fool. Why should you die before your time? It is good that you should take hold of this, and from that withhold not your hand, for the one who fears God shall come out from both of them.

WAYNE A. BRINDLE, offers an in-depth look at the passage, and asks:
Righteousness vs WickedIs he suggesting that since personal righteousness is no guarantee of long life or happiness (7:15), the reader should become “amoral,” steering a middle course between right and wrong? Or is he warning against becoming “too goody-goody or too impossibly naughty”?
Many people would have one of two types of reaction:
  1. They might decide that if they could reach perfection in character and knowledge, their problems would be solved;
  2. they might decide that God is unfair and simply devote themselves to immorality and foolish living as the best they can get out of life.
Qoheleth warns them against both of these options, since both of them lead to disaster. The best life, he says, depends on the fear of God.

 Following the negative admonitions of 7:16-17, Qoheleth now describes positively a “good” in life. Though neither righteousness nor wisdom can guarantee prosperity or unlock the mystery of the future, they are nevertheless good and necessary. It is good to hold on to righteousness, and not to let go of wisdom. Both wickedness and foolishness lead to disaster. Both righteousness and wisdom are achieved through the fear of God. It is through trust in, and obedience to, God that righteousness and wisdom can actually be balanced and made worthwhile.