In recent years, many countries have experienced a significant expansion of higher education enrolment. There is a particular interest among policy makers for further growth in STEM subjects, which could lead to larger classes in these fields. This study estimates the effect of class size on academic performance of university students, distinguishing between STEM and non-STEM fields. Using administrative data from a large UK higher education institution, we consider a sample of 25,000 students and a total of more than 190,000 observations, spanning six cohorts of first-year undergraduate students across all disciplines. Our identification of the class size effects rests on within student-across course variation. Overall, we find that larger classes are associated with significantly lower grades (effect size of -0.04) and the effect varies across academic fields, with no effect in non-STEM fields, and a large effect in STEM fields (-0.08). We further explore the heterogeneity of the effect along the dimensions of students socio-economic status, ability, and gender, finding that in STEM disciplines smaller classes appear to be particularly beneficial for students from a low socio-economic background, with higher attainment in A-levels and to male students.