diff --git a/includes/database/query.inc b/includes/database/query.inc
index c9c5a83..8b640b4 100644
--- a/includes/database/query.inc
+++ b/includes/database/query.inc
@@ -1836,6 +1836,7 @@ class DatabaseCondition implements QueryConditionInterface, Countable {
 
             $placeholders = array();
             if ($condition['value'] instanceof SelectQueryInterface) {
+              $this->ensureBrackets($operator);
               $condition['value']->compile($connection, $queryPlaceholder);
               $placeholders[] = (string) $condition['value'];
               $arguments += $condition['value']->arguments();
@@ -1956,6 +1957,25 @@ class DatabaseCondition implements QueryConditionInterface, Countable {
     return $return;
   }
 
+  /**
+   * Ensures that the value being tested against is bracketed.
+   *
+   * Some operators don't usually need brackets around the value being tested,
+   * but when the value being tested is a SelectQueryInterface it needs to be
+   * bracketed.  This function adds those necessary brackets.
+   *
+   * @param array $operator
+   *   The extra handling directives for the operator
+   */
+  private function ensureBrackets(&$operator) {
+    if (strpos($operator['prefix'], '(') === FALSE) {
+      $operator['prefix'] .= ' (';
+    }
+    if (strpos($operator['postfix'], ')') === FALSE) {
+      $operator['postfix'] = ')' . $operator['postfix'];
+    }
+  }
+
 }
 
 /**
