This paper addresses the problem of optimal H-2 control by output feedback. Necessary and sufficient conditions on the existence of a linear stabilizing output feedback gain are provided in terms of the intersection of a convex set and a set defined by a nonlinear real valued function. The results can be easily extended to deal with linear uncertain systems, where uncertainties are supposed to belong to convex bounded domains providing an H-2-guaranteed cost output feedback control. Thanks to the properties of the above-mentioned function, we show that under certain conditions, convex programming tools can be used for numerical purposes. Examples illustrate the theoretical results.