Inhibition of sodium-glucose cotransporter 2 (SGLT2) represents an emerging pharmaceutical approach for the treatment of heart failure. The mechanisms by which SGLT2 inhibitors reduce the risk of heart failure are not well understood. The objective of this study was to investigate the association between single nucleotide polymorphisms (SNPs) in the SLC5A2 gene, encoding SGLT2, and heart failure, and to assess potential mediators of this association. Regression and mediation analyses were conducted with individual participant data of the UK Biobank (n = 416,737) and validated in the cardiovascular high-risk cohort of the LUdwigshafen RIsk and Cardiovascular Health study (LURIC; n = 3316). Two intronic SNPs associated with SLC5A2 expression were included in a genetic score, which was associated with lower risk of heart failure in UK Biobank (odds ratio 0.97, 95% confidence interval, 0.95−0.99, P = 0.016). This association was also present in participants without type 2 diabetes or coronary artery disease (CAD). The associations of the genetic score with HbA1c, high-density lipoprotein cholesterol, uric acid, systolic blood pressure, waist circumference, and body composition mediated 35% of the effect of the score on heart failure risk. No associations of the genetic SGLT2 score with atherosclerotic cardiovascular disease outcomes or markers of volume status were observed, which was confirmed in the LURIC study. Variations in the gene encoding SGLT2 were associated with the risk of prevalent or incident heart failure. This association was mediated by several mechanisms and did not depend on the presence of type 2 diabetes or previous CAD events.