Clearly understanding the structure-function relationship and rational design of efficient CO2 electrocatalysts are still the challenges. This article describes the molecular origin of high selectivity of formic acid on N-doped SnO2 nanoparticles, which obtained via thermal treatment of g-C3N4 and SnCl2·2H2O precursor. Combined with density functional theory (DFT) calculations, we discover that N-doping effectively introduces oxygen vacancies and increases the charge density of Sn sites, which plays a positive role in CO2 activation. In addition, N-doping further regulates the adsorption energy of *OCHO, *COOH, *H and promotes HCOOH generation. Benefited from above modulation, the obtained N-doped SnO2 catalysts with oxygen vacancies (Ov-N-SnO2) exhibit faradaic efficiency of 93% for C1 formation, 88% for HCOOH production and well-suppression of H2 evolution over a wide range of potentials.