Sotorasib is a specific, irreversible inhibitor of the GTPase protein, KRASG12C. We compared the efficacy and safety of sotorasib with a standard-of-care treatment in patients with non-small-cell lung cancer (NSCLC) with the KRASG12C mutation who had been previously treated with other anticancer drugs. We conducted a randomised, open-label phase 3 trial at 148 centres in 22 countries. We recruited patients aged at least 18 years with KRASG12C-mutated advanced NSCLC, who progressed after previous platinum-based chemotherapy and a PD-1 or PD-L1 inhibitor. Key exclusion criteria included new or progressing untreated brain lesions or symptomatic brain lesions, previously identified oncogenic driver mutation other than KRASG12C for which an approved therapy is available (eg EGFR or ALK), previous treatment with docetaxel (neoadjuvant or adjuvant docetaxel was allowed if the tumour did not progress within 6 months after the therapy was terminated), previous treatment with a direct KRASG12C inhibitor, systemic anticancer therapy within 28 days of study day 1, and therapeutic or palliative radiation therapy within 2 weeks of treatment initiation. We randomly assigned (1:1) patients to oral sotorasib (960 mg once daily) or intravenous docetaxel (75 mg/m2 once every 3 weeks) in an open-label manner using interactive response technology. Randomisation was stratified by number of previous lines of therapy in advanced disease (1 vs 2 vs >2), ethnicity (Asian vs non-Asian), and history of CNS metastases (present or absent). Treatment continued until an independent central confirmation of disease progression, intolerance, initiation of another anticancer therapy, withdrawal of consent, or death, whichever occurred first. The primary endpoint was progression-free survival, which was assessed by a blinded, independent central review in the intention-to-treat population. Safety was assessed in all treated patients. This trial is registered at ClinicalTrials.gov, NCT04303780, and is active but no longer recruiting. Between June 4, 2020, and April 26, 2021, 345 patients were randomly assigned to receive sotorasib (n=171 [50%]) or docetaxel (n=174 [50%]). 169 (99%) patients in the sotorasib group and 151 (87%) in the docetaxel group received at least one dose. After a median follow-up of 17·7 months (IQR 16·4-20·1), the study met its primary endpoint of a statistically significant increase in the progression-free survival for sotorasib, compared with docetaxel (median progression-free survival 5·6 months [95% CI 4·3-7·8] vs 4·5 months [3·0-5·7]; hazard ratio 0·66 [0·51-0·86]; p=0·0017). Sotorasib was well tolerated, with fewer grade 3 or worse (n=56 [33%] vs n=61 [40%]) and serious treatment-related adverse events compared with docetaxel (n=18 [11%] vs n=34 [23%]). For sotorasib, the most common treatment-related adverse events of grade 3 or worse were diarrhoea (n= 20 [12%]), alanine aminotransferase increase (n=13 [8%]), and aspartate aminotransferase increase (n=9 [5%]). For docetaxel, the most common treatment-related adverse events of grade 3 or worse were neutropenia (n=13 [9%]), fatigue (n=9 [6%]), and febrile neutropenia (n=8 [5%]). Sotorasib significantly increased progression-free survival and had a more favourable safety profile, compared with docetaxel, in patients with advanced NSCLC with the KRASG12C mutation and who had been previously treated with other anticancer drugs. Amgen.