San Marino 0-6 Spain Fernando Torres scored a hat-trick as the visitors sealed second place in Group 7 in emphatic fashion.
With Serbia and Montenegro defeating Bosnia-Herzegovina elsewhere in the section, Luis Aragonés's side had to settle for a place in Friday's play-off draw. The visitors had kicked off against Group 7's bottom side needing a win to be assured of a top-two finish.
They got off to a perfect start when left-back Antonio López broke the deadlock in the opening minute with a tremendous 25-metre strike. Torres added a second on ten minutes after controlling a long ball from José Antonio Reyes before Sergio Ramos made it 3-0 from close range on the half-hour mark to leave Aragonés' team cruising.
They did not relent after half-time, however, with Real Madrid CF defender Ramos heading in a fourth beyond San Marino goalkeeper Federico Gasperoni from a corner before two late goals from Torres, the first from the penalty spot, sealed an emphatic victory. The Club Atlético de Madrid striker's hat-trick goal was the 40th San Marino have conceded in their ten Group 7 matches, all of which have ended in defeat.