San Francisco swept the Astros in three games, winning for the eighth time in 10 outings. The latest in a season full of Houston losing streaks has reached five games.
Houston owned a 4-0 lead through three innings, but San Francisco got back into the game with a three-run fifth that included a sacrifice fly from Sandoval.
Sandoval ignited a three-run seventh with an RBI single and then scored on a two-run base hit by Hunter Pence.
Brandon Belt had four of San Francisco's 15 hits and drove in a run with a ninth-inning double.
After giving up four runs early, Ryan Vogelsong (12-7) settled down to get the win. He allowed seven hits with seven strikeouts in six innings and became the winning pitcher when the Giants scored three times in the half inning after he had left the game.
The loss went to reliever Fernando Rodriguez (1-9), who gave up three runs in the seventh -- his only inning of work.