diff --git a/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php b/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php
index 49f18bbe7f1a35a5fbc17e6413b048414bd92626..ea0baf66e1cd3f5588009bdd861671a8b0be8ec9 100644
--- a/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php
+++ b/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php
@@ -397,6 +397,15 @@ interface DMLQueryBuilderInterface
      */
     public function sql_or();
 
+    /**
+     * @param String $alias        the alias of the WITH statement
+     * @param String $sql_query    sql query reference
+     * @param Array  $column_names an optional parameter to give the result columns a name
+     *
+     * @return DMLQueryBuilderInterface $self Self reference
+     */
+    public function with($alias, $sql_query, $column_names = NULL);
+
 }
 
 ?>
diff --git a/src/Lunr/Gravity/Database/DatabaseDMLQueryBuilder.php b/src/Lunr/Gravity/Database/DatabaseDMLQueryBuilder.php
index 5fe54a79defa383719e6e847f7dd8508d4c07eb2..11385a3f4045aef9b33bc811310d5d2d50e7f57e 100644
--- a/src/Lunr/Gravity/Database/DatabaseDMLQueryBuilder.php
+++ b/src/Lunr/Gravity/Database/DatabaseDMLQueryBuilder.php
@@ -167,6 +167,12 @@ abstract class DatabaseDMLQueryBuilder implements DMLQueryBuilderInterface
     protected $join_type;
 
     /**
+     * SQL Query part: String that contains a select query within a with statement
+     * @var String
+     */
+    protected $with;
+
+    /**
      * Constructor.
      */
     public function __construct()
@@ -194,6 +200,7 @@ abstract class DatabaseDMLQueryBuilder implements DMLQueryBuilderInterface
         $this->compound           = '';
         $this->is_unfinished_join = FALSE;
         $this->join_type          = '';
+        $this->with               = '';
     }
 
     /**
@@ -224,6 +231,7 @@ abstract class DatabaseDMLQueryBuilder implements DMLQueryBuilderInterface
         unset($this->select_statement);
         unset($this->is_unfinished_join);
         unset($this->join_type);
+        unset($this->with);
     }
 
     /**
@@ -243,7 +251,7 @@ abstract class DatabaseDMLQueryBuilder implements DMLQueryBuilderInterface
         array_push($components, 'select_mode', 'select', 'from', 'join', 'where');
         array_push($components, 'group_by', 'having', 'order_by', 'limit', 'lock_mode');
 
-        $standard = 'SELECT ' . $this->implode_query($components);
+        $standard = $this->with . ' SELECT ' . $this->implode_query($components);
         if ($this->compound == '')
         {
             return $standard;
@@ -400,6 +408,32 @@ abstract class DatabaseDMLQueryBuilder implements DMLQueryBuilderInterface
     }
 
     /**
+     * Define a WITH clause.
+     *
+     * @param String $alias        the alias of the WITH statement
+     * @param String $sql_query    sql query reference
+     * @param Array  $column_names an optional parameter to give the result columns a name
+     *
+     * @return void
+     */
+    protected function sql_with($alias, $sql_query, $column_names = NULL)
+    {
+        if($column_names != NULL)
+        {
+            $column_names = '(' . implode(', ', $column_names) . ')';
+        }
+
+        if($this->with != '')
+        {
+            $this->with .= ', ' . $alias . $column_names . ' AS ( ' . $sql_query . ' )';
+        }
+        else
+        {
+            $this->with .= 'WITH ' . $alias . $column_names . ' AS ( ' . $sql_query . ' ) ';
+        }
+    }
+
+    /**
      * Define a UPDATE clause.
      *
      * @param String $table_references The tables to update
diff --git a/src/Lunr/Gravity/Database/SQLDMLQueryBuilder.php b/src/Lunr/Gravity/Database/SQLDMLQueryBuilder.php
index bf19268cc2c213c0bba44d585599c9a113643550..d5cde4c138ae1c82849fab2405200b7c534cd791 100644
--- a/src/Lunr/Gravity/Database/SQLDMLQueryBuilder.php
+++ b/src/Lunr/Gravity/Database/SQLDMLQueryBuilder.php
@@ -555,6 +555,21 @@ abstract class SQLDMLQueryBuilder extends DatabaseDMLQueryBuilder
         return $this;
     }
 
+    /**
+     * Define a WITH clause.
+     *
+     * @param String $alias        the alias of the WITH statement
+     * @param String $sql_query    sql query reference
+     * @param Array  $column_names an optional parameter to give the result columns a name
+     *
+     * @return $this SQLDMLQueryBuilder $self Self reference
+     */
+    public function with($alias, $sql_query, $column_names = NULL)
+    {
+        $this->sql_with($alias, $sql_query, $column_names);
+        return $this;
+    }
+
 }
 
 ?>
