diff --git a/includes/database/select.inc b/includes/database/select.inc
index 8d84460..a57467c 100644
--- a/includes/database/select.inc
+++ b/includes/database/select.inc
@@ -1579,6 +1579,14 @@ class SelectQuery extends Query implements SelectQueryInterface {
       $query .= "\nHAVING " . $this->having;
     }
 
+    // UNION is a little odd, as the select queries to combine are passed into
+    // this query, but syntactically they all end up on the same level.
+    if ($this->union) {
+      foreach ($this->union as $union) {
+        $query .= ' ' . $union['type'] . ' ' . (string) $union['query'];
+      }
+    }
+
     // ORDER BY
     if ($this->order) {
       $query .= "\nORDER BY ";
@@ -1598,14 +1606,6 @@ class SelectQuery extends Query implements SelectQueryInterface {
       $query .= "\nLIMIT " . (int) $this->range['length'] . " OFFSET " . (int) $this->range['start'];
     }
 
-    // UNION is a little odd, as the select queries to combine are passed into
-    // this query, but syntactically they all end up on the same level.
-    if ($this->union) {
-      foreach ($this->union as $union) {
-        $query .= ' ' . $union['type'] . ' ' . (string) $union['query'];
-      }
-    }
-
     if ($this->forUpdate) {
       $query .= ' FOR UPDATE';
     }
diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test
index 59d2e5d..56a5c32 100644
--- a/modules/simpletest/tests/database_test.test
+++ b/modules/simpletest/tests/database_test.test
@@ -1646,6 +1646,64 @@ class DatabaseSelectTestCase extends DatabaseTestCase {
   }
 
   /**
+   * Tests that we can UNION multiple Select queries together and set the ORDER.
+   */
+  function testUnionOrder() {
+    // This gives George and Ringo.
+    $query_1 = db_select('test', 't')
+        ->fields('t', array('name'))
+        ->condition('age', array(27, 28), 'IN');
+
+    // This gives Paul.
+    $query_2 = db_select('test', 't')
+        ->fields('t', array('name'))
+        ->condition('age', 26);
+
+    $query_1->union($query_2);
+    $query_1->orderBy('name', 'DESC');
+
+    $names = $query_1->execute()->fetchCol();
+
+    // Ensure we get all 3 records.
+    $this->assertEqual(count($names), 3, 'UNION returned rows from both queries.');
+
+    // Ensure that the names are in the correct reverse alphabetical order,
+    // regardless of which query they came from.
+    $this->assertEqual($names[0], 'Ringo', 'First query returned correct name.');
+    $this->assertEqual($names[1], 'Paul', 'Second query returned correct name.');
+    $this->assertEqual($names[2], 'George', 'Third query returned correct name.');
+  }
+
+  /**
+   * Tests that we can UNION multiple Select queries together with a LIMIT.
+   */
+  function testUnionOrderLimit() {
+    // This gives George and Ringo.
+    $query_1 = db_select('test', 't')
+        ->fields('t', array('name'))
+        ->condition('age', array(27, 28), 'IN');
+
+    // This gives Paul.
+    $query_2 = db_select('test', 't')
+        ->fields('t', array('name'))
+        ->condition('age', 26);
+
+    $query_1->union($query_2);
+    $query_1->orderBy('name', 'DESC');
+    $query_1->range(0, 2);
+
+    $names = $query_1->execute()->fetchCol();
+
+    // Ensure we get only 2 of the 3 records.
+    $this->assertEqual(count($names), 2, 'UNION with a limit returned rows from both queries.');
+
+    // Ensure that the names are in the correct reverse alphabetical order,
+    // regardless of which query they came from.
+    $this->assertEqual($names[0], 'Ringo', 'First query returned correct name.');
+    $this->assertEqual($names[1], 'Paul', 'Second query returned correct name.');
+  }
+
+  /**
    * Test that random ordering of queries works.
    *
    * We take the approach of testing the Drupal layer only, rather than trying
