Teachers’ gender does not necessarily have a big effect during elementary school but seems to make more of a difference when children are older. Then, girls do better with a female teacher and boys with a male one, said Thomas Dee, a professor of education at Stanford.

When eighth graders had a female teacher instead of a male one, boys fell behind girls by the equivalent of three and a half months of learning, according to a well-regarded study he wrote, which compared the effect of two teachers of different genders on the same students. When students and teachers were the same gender, teachers also had more positive impressions of students, and students looked forward more to the subject. The study used Department of Education data on 25,000 eighth graders from 1,000 schools.

In high school and college math and science courses, studies have shown that when women have a female instructor, they get higher grades, participate more in class and are more likely to continue to pursue the subject.

Researchers say it’s not entirely clear why teachers’ gender and race make a difference; it’s likely to be a combination of things. Students tend to be inspired by role models they can relate to. Same-race teachers might be able to present new material in a more culturally relevant way. Also, teachers sometimes treat students differently based on their own backgrounds and stereotypes. Social scientists call this implicit bias, when stereotypes influence people’s thinking, often unconsciously.

A variety of research, for instance, has shown that teachers tend to assess black students differently from white students. Preschool teachers judge black children more harshly for the same behavior. White teachers are less likely than black teachers to assign black students to gifted and talented programs even if their test scores match those of white students. When black students had both a white and black teacher, the black teachers consistently had higher expectations for the children’s potential.