diff --git a/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php b/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php
index 49f18bbe7f1a35a5fbc17e6413b048414bd92626..1b8ddf109b60ce7a22dc6ed62786aac68241370e 100644
--- a/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php
+++ b/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php
@@ -397,6 +397,19 @@ interface DMLQueryBuilderInterface
      */
     public function sql_or();
 
+    /**
+     * @param $with the alias of the WITH statement
+     * @return SQLDMLQueryBuilder $self Self reference
+     */
+    public function with($with);
+
+    /**
+     * Define the end of a WITH statement
+     *
+     * @return SQLDMLQueryBuilder $self Self reference
+     */
+    public function end_with();
+
 }
 
 ?>
diff --git a/src/Lunr/Gravity/Database/DatabaseDMLQueryBuilder.php b/src/Lunr/Gravity/Database/DatabaseDMLQueryBuilder.php
index 5fe54a79defa383719e6e847f7dd8508d4c07eb2..53c818d9145069dbca045ee3e0a459ff6ec3263b 100644
--- a/src/Lunr/Gravity/Database/DatabaseDMLQueryBuilder.php
+++ b/src/Lunr/Gravity/Database/DatabaseDMLQueryBuilder.php
@@ -167,6 +167,19 @@ 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_query;
+
+    /**
+     * SQL Query part: A boolean identifying if the with statement is finished
+     * @var Boolean
+     */
+    protected $with_ended;
+
+
+    /**
      * Constructor.
      */
     public function __construct()
@@ -194,6 +207,8 @@ abstract class DatabaseDMLQueryBuilder implements DMLQueryBuilderInterface
         $this->compound           = '';
         $this->is_unfinished_join = FALSE;
         $this->join_type          = '';
+        $this->with_query         = '';
+        $this->with_ended         = TRUE;
     }
 
     /**
@@ -224,6 +239,8 @@ abstract class DatabaseDMLQueryBuilder implements DMLQueryBuilderInterface
         unset($this->select_statement);
         unset($this->is_unfinished_join);
         unset($this->join_type);
+        unset($this->with_query);
+        unset($this->with_ended);
     }
 
     /**
@@ -243,7 +260,13 @@ 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 = "";
+
+        if($this->with_ended){
+            $standard .= $this->with_query;
+        }
+
+        $standard .= 'SELECT ' . $this->implode_query($components);
         if ($this->compound == '')
         {
             return $standard;
@@ -400,6 +423,54 @@ abstract class DatabaseDMLQueryBuilder implements DMLQueryBuilderInterface
     }
 
     /**
+     * Define a WITH clause
+     *
+     * @param $with the alias of the WITH statement
+     */
+    protected function sql_with($with){
+        $this->with_ended = FALSE;
+
+        if($this->with_query != ''){
+           $this->with_query .= ', '.$with. ' AS ( ';
+        } else {
+            $this->with_query .= "WITH " . $with . " AS ( ";
+                }
+        }
+
+    /**
+     * Define the end of a WITH clause
+     */
+    protected function end_sql_with(){
+
+        $this->with_query .= $this->get_select_query() .") ";
+        $this->select             = '';
+        $this->select_mode        = array();
+        $this->update             = '';
+        $this->update_mode        = array();
+        $this->delete             = '';
+        $this->delete_mode        = array();
+        $this->from               = '';
+        $this->join               = '';
+        $this->where              = '';
+        $this->group_by           = '';
+        $this->having             = '';
+        $this->order_by           = '';
+        $this->limit              = '';
+        $this->connector          = '';
+        $this->into               = '';
+        $this->insert_mode        = array();
+        $this->set                = '';
+        $this->column_names       = '';
+        $this->values             = '';
+        $this->select_statement   = '';
+        $this->compound           = '';
+        $this->is_unfinished_join = FALSE;
+        $this->join_type          = '';
+        $this->with_ended         = TRUE;
+    }
+
+
+    /**
      * 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..3dd22384dd05dab05d414609ffb27ce6275bfcf8 100644
--- a/src/Lunr/Gravity/Database/SQLDMLQueryBuilder.php
+++ b/src/Lunr/Gravity/Database/SQLDMLQueryBuilder.php
@@ -555,6 +555,30 @@ abstract class SQLDMLQueryBuilder extends DatabaseDMLQueryBuilder
         return $this;
     }
 
+    /**
+     * Define a WITH clause
+     *
+     * @param $with the alias of the WITH statement
+     * @return $this SQLDMLQueryBuilder $self Self reference
+     */
+    public function with($with)
+    {
+        $this->sql_with($with);
+        return $this;
+    }
+
+    /**
+     * Define the end of a WITH statement
+     *
+     * @return $this SQLDMLQueryBuilder $self Self reference
+     */
+    public function end_with()
+    {
+        $this->end_sql_with();
+        return $this;
+    }
+
+
 }
 
 ?>
