Rapid technological innovation and digitalization have intensified the demand for STEM-related skills, yet many educational systems face stagnant or declining student engagement in STEM, particularly in the later years of secondary education. This study examined how gender, study track (STEM versus non-STEM) and grade level are related to secondary school students’ STEM motivation, interest and identity. The study also examined the role of perceived autonomy, relatedness and competence support from mathematics teachers in these associations. Using a comparative cross-sectional survey design, data were collected from 317 students in two secondary schools in the Kempenrand region of Antwerp, Flanders (Belgium). Linear regression models and school-specific simple-effects analyses were used to examine school and subgroup differences. Concurrent indirect-association and moderation analyses were performed to examine the role of perceived need support with Benjamini–Hochberg corrections applied for multiple testing. The results revealed variation between school contexts. The association between gender and STEM identity differed between schools, while study-track differences in STEM interest, identity and intrinsic motivation were greater in one school than in the other. The clearest contextual variation concerned grade level. In one school, 5th-year students reported higher STEM identity and identified regulation, whereas 6th-year students reported higher levels of these outcomes in the other; 6th-year students in the latter school also reported higher STEM interest and intrinsic motivation. No total concurrent indirect associations through perceived need support remained statistically significant after correction. However, the association between study track and STEM identity varied significantly as a function of perceived autonomy and relatedness support, with both forms of support positively associated with STEM identity among STEM track students but not among non-STEM students. These findings indicate that subgroup differences in STEM-related outcomes are not uniform across school contexts and that pooled estimates may obscure meaningful contextual heterogeneity. Context-sensitive analyses are therefore important when identifying groups with lower STEM motivation, interest or identity.