diff --git a/includes/database/select.inc b/includes/database/select.inc
index 16c352ea37eaf53639634566bbea48d61746b274..8a6a4ab305f6f08ae5b6bb9224dbccc84db57173 100644
--- a/includes/database/select.inc
+++ b/includes/database/select.inc
@@ -1319,7 +1319,10 @@ public function __toString() {
 
       // If the table is a subquery, compile it and integrate it into this query.
       if ($table['table'] instanceof SelectQueryInterface) {
-        $table_string = '(' . (string)$table['table'] . ')';
+        // Run preparation steps on this sub-query before converting to string.
+        $subquery = $table['table'];
+        $subquery->preExecute();
+        $table_string = '(' . (string) $subquery . ')';
       }
       else {
         $table_string = '{' . $this->connection->escapeTable($table['table']) . '}';
diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test
index 59080c71397da009e3f261ca5faac141460c460d..1005553d4550be8f52f8a0934fe264c51a644aee 100644
--- a/modules/simpletest/tests/database_test.test
+++ b/modules/simpletest/tests/database_test.test
@@ -2326,22 +2326,6 @@ class DatabaseAlterTestCase extends DatabaseTestCase {
     $this->assertEqual($records[0]->$pid_field, 1, t('Correct data retrieved.'));
     $this->assertEqual($records[0]->$task_field, 'sleep', t('Correct data retrieved.'));
   }
-}
-
-/**
- * Select alter tests, part 2.
- *
- * @see database_test_query_alter()
- */
-class DatabaseAlter2TestCase extends DatabaseTestCase {
-
-  public static function getInfo() {
-    return array(
-      'name' => 'Query altering tests, part 2',
-      'description' => 'Test the hook_query_alter capabilities of the Select builder.',
-      'group' => 'Database',
-    );
-  }
 
   /**
    * Test that we can alter the fields of a query.
@@ -2390,6 +2374,31 @@ class DatabaseAlter2TestCase extends DatabaseTestCase {
 
     $this->assertEqual($num_records, 4, t('Returned the correct number of rows.'));
   }
+
+  /**
+   * Test that we can do basic alters on subqueries.
+   */
+  function testSimpleAlterSubquery() {
+    // Create a sub-query with an alter tag.
+    $subquery = db_select('test', 'p');
+    $subquery->addField('p', 'name');
+    $subquery->addField('p', 'id');
+    // Pick out George.
+    $subquery->condition('age', 27);
+    $subquery->addExpression("age*2", 'double_age');
+    // This query alter should change it to age * 3.
+    $subquery->addTag('database_test_alter_change_expressions');
+
+    // Create a main query and join to sub-query.
+    $query = db_select('test_task', 'tt');
+    $query->join($subquery, 'pq', 'pq.id = tt.pid');
+    $age_field = $query->addField('pq', 'double_age');
+    $name_field = $query->addField('pq', 'name');
+
+    $record = $query->execute()->fetch();
+    $this->assertEqual($record->$name_field, 'George', t('Fetched name is correct.'));
+    $this->assertEqual($record->$age_field, 27*3, t('Fetched age expression is correct.'));
+  }
 }
 
 /**
@@ -3227,5 +3236,4 @@ class DatabaseEmptyStatementTestCase extends DrupalWebTestCase {
 
     $this->assertEqual($result->fetchAll(), array(), t('Empty array returned from empty result set.'));
   }
-
 }