We consider an integrated sensing and communication (ISAC) system with a single communication user and multiple targets. For the communication functionality, the achievable rate is employed as the performance metric, while for sensing, we focus on minimizing the mean squared error (MSE) between the designed beampattern and a desired one for tracking the targets. Towards this, and by assuming that there are fewer radiofrequency (RF) chains than antenna elements at the transmitter (Tx), we focus on the joint antenna selection (AS) and covariance matrix (CM) optimization at the Tx. This is a mixed-integer optimization problem, yet we demonstrate that it can be efficiently solved, in polynomial time, by combining convex optimization tools with dynamic programming (DP). By introducing an adjustable trade-off parameter, we formulate a joint objective function that captures both the communication and sensing metric. In this way, different ISAC solutions can be obtained, considering the trade-off among the two functionalities. It is shown that selecting the active antennas with our proposed method is superior than assuming a uniform Tx array with fixed antenna positions. Notably, by individually considering the optimization of either the sensing or the communication system alone, our proposed algorithm outperforms the literature proposals, by incurring only a small increase in complexity.