Background: WHO-recommended second-line antiretroviral therapy (ART) of a pharmacologically enhanced (boosted) protease inhibitor plus nucleoside or nucleotide reverse transcriptase inhibitors (NtRTIs) might be compromised by resistance. Results of the 96 week SECOND-LINE randomised trial showed that NtRTI-sparing ART with ritonavir-boosted lopinavir and raltegravir (raltegravir-group) provided non-inferior efficacy to ritonavir-boosted lopinavir and two or three NtRTIs (NtRTI-group) in participants with virological failure composed of a first-line regimen of a non-nucleoside reverse transcriptase inhibitor plus two NtRTIs. We report the relation of baseline virological resistance with virological failure and emergent resistance on study. Methods: As part of the randomised open-label SECOND-LINE trial, second-line ART NtRTI selection was made by either genotype (local laboratory) or algorithm. Genotypic resistance for the entire cohort at baseline was assessed on stored samples at a central laboratory. Virological failure was defined as plasma viral load greater than 200 copies per mL. Baseline viral isolates were assigned genotypic sensitivity scores (GSSs) by use of the Stanford HIV Database version 6.3.1: a global GSS (gGSS), defined as the combined GSS for lamivudine or emtricitabine, abacavir, zidovudine, stavudine, didanosine, and tenofovir and a specific GSS (sGSS) defined as the GSS for the ART regimen initiated by a specific participant. Emergent resistance was reported on samples with a viral load greater than 500 copies per mL. We used multivariate logistic regression with backward elimination to assess predictors of virological failure and emergent resistance. Findings: From April 19, 2010, to July 22, 2013, 271 patients were included in the NtRTI group and and 270 in the raltegravir group. In the NtRTI group 215 had available baseline sequence data, and 240 had viral load measurements at 96 weeks; in the raltegravir group 236 had baseline sequence data and 255 had viral load measurements at 96 weeks. Median (IQR) gGSS was 3·0 (1·3-4·3) in the NtRTI group and 3·0 (1·0-4·3) in the raltegravir group. The median sGSS in the NtRTI group was 1·0 (0·5-1·8). Multivariate analysis showed significant associations between virological failure and less than complete adherence at week 4 (odds ratio [OR] 2·18, 95%CI 1·07-4·47; p=0·03) and week 48 (2·49, 1·09-5·69; p=0·03), baseline plasma viral load greater than 100 000 copies per mL (3·43, 1·70-6·94; p=0·0006), baseline gGSS >4·25 (4·73, 1·94-11·6; p=0·0007), and being Hispanic (3·13, 1·21-8·13; p=0·02) or African (3·49, 1·68-7·28; p=0·0008) rather than Asian. We observed emergent major mutations in one (1%) of 129 participants for protease (both groups), eight (13%) of 64 for reverse transcriptase (NtRTI group) and 16 (20%) of 79 for integrase. Emergent resistance was associated with the raltegravir group (OR 2·47, 95% CI 1·02-5·99; p=0·05), baseline log10 viral load (1·83, 1·12-2·97; p=0·02), and absence of the Lys65Arg (K65R) or Lys70Glu (K70E) mutation at baseline (3·18, 1·12-9·02; p=0·03). Interpretation: Poor adherence was a major determinant of virological failure in people on second-line ART. In settings with limited resources, investment in optimisation of adherence rather than implementation of drug resistance testing might be advisable. Funding: University of New South Wales Australia, Merck, AbbVie, and the Foundation for AIDS Research.