These expressions are highly arbitrary, not often completely logical, and they may have to be memorized. in is used mostly with generic names of educational settings, at is used with specific institutions. It isn't usually in a, by the way -- no a.
when I was in high school when I was in grade school when I was in elementary school