Leveraging data from Sweden and Chicago, we study the educational pipeline for STEM and economics majors to better understand the determinants of the gender gap, and when these determinants arise. We present three findings. First, females are less likely to select STEM courses in high school, despite equal or better preparation. Second, there are important gender differences in preferences and beliefs, even conditional on ability. Third, early differences in preferences and beliefs explain more of the gaps in high school sorting than other candidate variables. High school sorting then explains a large portion of the gender difference in college majors.