The spectroscopic Sloan Digital Sky Survey (SDSS) Data Release 7 (DR7) galaxy sample represents the final set of galaxies observed using the original SDSS target selection criteria. We analyse the clustering of galaxies within this sample, including both the luminous red galaxy and main samples, and also include the 2-degree Field Galaxy Redshift Survey data. In total, this sample comprises 893 319 galaxies over 9100 deg2. Baryon acoustic oscillations (BAO) are observed in power spectra measured for different slices in redshift; this allows us to constrain the distance–redshift relation at multiple epochs. We achieve a distance measure at redshift z= 0.275, of rs(zd)/DV(0.275) = 0.1390 ± 0.0037 (2.7 per cent accuracy), where rs(zd) is the comoving sound horizon at the baryon-drag epoch, DV(z) ≡[(1 +z)2D2Acz/H(z)]1/3, DA(z) is the angular diameter distance and H(z) is the Hubble parameter. We find an almost independent constraint on the ratio of distances DV(0.35)/DV(0.2) = 1.736 ± 0.065, which is consistent at the 1.1σ level with the best-fitting Λ cold dark matter model obtained when combining our z= 0.275 distance constraint with the Wilkinson Microwave Anisotropy Probe 5-year (WMAP5) data. The offset is similar to that found in previous analyses of the SDSS DR5 sample, but the discrepancy is now of lower significance, a change caused by a revised error analysis and a change in the methodology adopted, as well as the addition of more data. Using WMAP5 constraints on Ωbh2 and Ωc h2, and combining our BAO distance measurements with those from the Union supernova sample, places a tight constraint on Ωm= 0.286 ± 0.018 and H0= 68.2 ± 2.2 km s−1 Mpc−1 that is robust to allowing Ωk≠ 0 and w≠−1. This result is independent of the behaviour of dark energy at redshifts greater than those probed by the BAO and supernova measurements. Combining these data sets with the full WMAP5 likelihood constraints provides tight constraints on both Ωk=−0.006 ± 0.008 and w=−0.97 ± 0.10 for a constant dark energy equation of state.