According to 10 U.S. Code § 113 - Secretary of Defense:
(a) There is a Secretary of Defense, who is the head of the Department of Defense, appointed from civilian life by the President, by and with the advice and consent of the Senate. A person may not be appointed as Secretary of Defense within seven years after relief from active duty as a commissioned officer of a regular component of an armed force.
Since the creation of the position in 1947, such a waiver has only been approved three times, for Army general George Marshall in 1950, Marine Corps General Jim Mattis in 2017, and retired Army general Lloyd J. Austin III in 2021.
But did Congress ever refuse to grant such an exception to a Presidential nominee?