The early years of life are crucial for developing cognitive abilities, such as intelligence. This period is marked by rapid brain development, in which nutrition is essential. This study aimed to characterise dietary patterns at age 2 and investigate their association with cognitive performance at ages 6-7. The study used data from the 2015 Pelotas Birth Cohort, Brazil (n=4,275). Dietary intake was assessed using a habitual consumption questionnaire, and patterns were extracted using principal component analysis. Cognitive performance was assessed using the Wechsler Intelligence Scale for Children (4th edition), providing intelligence quotient (IQ) scores. Associations were tested using linear regression models. The unhealthy dietary pattern (characterised by packaged snacks, instant noodles, sweet biscuits, sweets and candies, soft drinks, sausages, and processed meats) was negatively associated with IQ (p <0.001). Exploratory analyses suggest that early-life deficits (low weight, height, or head circumference for age) may amplify the negative effects of unhealthy dietary pattern on IQ (interaction p=0.020). Children with high adherence to this pattern and early-life deficits showed a 4.78-point IQ reduction (95% CI: -7.06; -2.49), while those without deficits had a 2.24-point reduction (95% CI: -3.35; -1.13), compared to those with low adherence, even after adjustments. No significant associations were found between the healthy dietary pattern (characterised by beans, baby foods, fruits, vegetables, and natural fruit juices) and IQ scores. These findings suggest that an unhealthy diet in early childhood may impair cognitive performance, particularly in children with early-life deficits.