Semaglutide, a GLP-1 receptor agonist, reduces the risk of major adverse cardiovascular events (MACE) in people with overweight or obesity, but the effects of this drug on outcomes in patients with atherosclerotic cardiovascular disease and heart failure are unknown. We report a prespecified analysis of the effect of once-weekly subcutaneous semaglutide 2·4 mg on ischaemic and heart failure cardiovascular outcomes. We aimed to investigate if semaglutide was beneficial in patients with atherosclerotic cardiovascular disease with a history of heart failure compared with placebo; if there was a difference in outcome in patients designated as having heart failure with preserved ejection fraction compared with heart failure with reduced ejection fraction; and if the efficacy and safety of semaglutide in patients with heart failure was related to baseline characteristics or subtype of heart failure. The SELECT trial was a randomised, double-blind, multicentre, placebo-controlled, event-driven phase 3 trial in 41 countries. Adults aged 45 years and older, with a BMI of 27 kg/m2 or greater and established cardiovascular disease were eligible for the study. Patients were randomly assigned (1:1) with a block size of four using an interactive web response system in a double-blind manner to escalating doses of once-weekly subcutaneous semaglutide over 16 weeks to a target dose of 2·4 mg, or placebo. In a prespecified analysis, we examined the effect of semaglutide compared with placebo in patients with and without a history of heart failure at enrolment, subclassified as heart failure with preserved ejection fraction, heart failure with reduced ejection fraction, or unclassified heart failure. Endpoints comprised MACE (a composite of non-fatal myocardial infarction, non-fatal stroke, and cardiovascular death); a composite heart failure outcome (cardiovascular death or hospitalisation or urgent hospital visit for heart failure); cardiovascular death; and all-cause death. The study is registered with ClinicalTrials.gov, NCT03574597. Between Oct 31, 2018, and March 31, 2021, 17 604 patients with a mean age of 61·6 years (SD 8·9) and a mean BMI of 33·4 kg/m2 (5·0) were randomly assigned to receive semaglutide (8803 [50·0%] patients) or placebo (8801 [50·0%] patients). 4286 (24·3%) of 17 604 patients had a history of investigator-defined heart failure at enrolment: 2273 (53·0%) of 4286 patients had heart failure with preserved ejection fraction, 1347 (31·4%) had heart failure with reduced ejection fraction, and 666 (15·5%) had unclassified heart failure. Baseline characteristics were similar between patients with and without heart failure. Patients with heart failure had a higher incidence of clinical events. Semaglutide improved all outcome measures in patients with heart failure at random assignment compared with those without heart failure (hazard ratio [HR] 0·72, 95% CI 0·60-0·87 for MACE; 0·79, 0·64-0·98 for the heart failure composite endpoint; 0·76, 0·59-0·97 for cardiovascular death; and 0·81, 0·66-1·00 for all-cause death; all pinteraction>0·19). Treatment with semaglutide resulted in improved outcomes in both the heart failure with reduced ejection fraction (HR 0·65, 95% CI 0·49-0·87 for MACE; 0·79, 0·58-1·08 for the composite heart failure endpoint) and heart failure with preserved ejection fraction groups (0·69, 0·51-0·91 for MACE; 0·75, 0·52-1·07 for the composite heart failure endpoint), although patients with heart failure with reduced ejection fraction had higher absolute event rates than those with heart failure with preserved ejection fraction. For MACE and the heart failure composite, there were no significant differences in benefits across baseline age, sex, BMI, New York Heart Association status, and diuretic use. Serious adverse events were less frequent with semaglutide versus placebo, regardless of heart failure subtype. In patients with atherosclerotic cardiovascular diease and overweight or obesity, treatment with semaglutide 2·4 mg reduced MACE and composite heart failure endpoints compared with placebo in those with and without clinical heart failure, regardless of heart failure subtype. Our findings could facilitate prescribing and result in improved clinical outcomes for this patient group. Novo Nordisk.