diff --git a/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php b/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php
index 49f18bbe7f1a35a5fbc17e6413b048414bd92626..b3a9905202e9bf186971f015086e9f221eb52115 100644
--- a/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php
+++ b/src/Lunr/Gravity/Database/DMLQueryBuilderInterface.php
@@ -397,6 +397,17 @@ interface DMLQueryBuilderInterface
      */
     public function sql_or();
 
+    /**
+     * 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 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..af74271a4f39ad49196bdaade28dd2bcf5951fc7 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 . ' 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;
+    }
+
 }
 
 ?>
diff --git a/src/Lunr/Gravity/Database/Tests/DatabaseDMLQueryBuilderBaseTest.php b/src/Lunr/Gravity/Database/Tests/DatabaseDMLQueryBuilderBaseTest.php
index de2642fc63e21c846e8d7d8fb3191e88f51bbab1..5951f870ce6c4c98448bf718fed493eac2178714 100644
--- a/src/Lunr/Gravity/Database/Tests/DatabaseDMLQueryBuilderBaseTest.php
+++ b/src/Lunr/Gravity/Database/Tests/DatabaseDMLQueryBuilderBaseTest.php
@@ -152,6 +152,14 @@ class DatabaseDMLQueryBuilderBaseTest extends DatabaseDMLQueryBuilderTest
     }
 
     /**
+     * Test that with is an empty string by default.
+     */
+    public function testWithEmptyByDefault()
+    {
+        $this->assertPropertyEquals('with', '');
+    }
+
+    /**
      * Test that prepare_index_hints prepares valid index hints correctly.
      *
      * @param Array  $hints    Array of index hints
diff --git a/src/Lunr/Gravity/Database/Tests/DatabaseDMLQueryBuilderQueryPartsWithTest.php b/src/Lunr/Gravity/Database/Tests/DatabaseDMLQueryBuilderQueryPartsWithTest.php
new file mode 100644
index 0000000000000000000000000000000000000000..ec5e30708249b291735d397bd012af9ed4ade65a
--- /dev/null
+++ b/src/Lunr/Gravity/Database/Tests/DatabaseDMLQueryBuilderQueryPartsWithTest.php
@@ -0,0 +1,145 @@
+<?php
+/**
+ * This file contains the DatabaseDMLQueryBuilderQueryPartsWithTest class.
+ *
+ * PHP Version 5.4
+ *
+ * @package    Lunr\Gravity\Database
+ * @author     Koen Hengsdijk
+ * @copyright  2012-2017, M2Mobi BV, Amsterdam, The Netherlands
+ * @license    http://lunr.nl/LICENSE MIT License
+ */
+
+namespace Lunr\Gravity\Database\Tests;
+
+use Lunr\Gravity\Database\DatabaseDMLQueryBuilder;
+
+/**
+ * This class contains the tests for the query parts methods.
+ *
+ * @covers Lunr\Gravity\Database\DatabaseDMLQueryBuilder
+ */
+class DatabaseDMLQueryBuilderQueryPartsWithTest extends DatabaseDMLQueryBuilderTest
+{
+
+    /**
+     * Test specifying the with part of a query without recursion and without column names
+     *
+     * @covers Lunr\Gravity\Database\DatabaseDMLQueryBuilder::sql_with
+     */
+    public function testNonRecursiveWithWithoutColumnNames()
+    {
+        $method = $this->get_accessible_reflection_method('sql_with');
+        $method->invokeArgs($this->class, [ 'alias', 'query' ]);
+
+        $string = 'WITH alias AS ( query )';
+
+        $this->assertEquals($string, $this->get_reflection_property_value('with'));
+    }
+
+    /**
+     * Test specifying multiple with statements in a query without recursion and without column names
+     *
+     * @covers Lunr\Gravity\Database\DatabaseDMLQueryBuilder::sql_with
+     */
+    public function testMultipleNonRecursiveWithWithoutColumnNames()
+    {
+        $this->set_reflection_property_value('with', 'WITH alias AS ( query )');
+
+        $method = $this->get_accessible_reflection_method('sql_with');
+        $method->invokeArgs($this->class, [ 'alias2', 'query2']);
+
+        $string = 'WITH alias AS ( query ), alias2 AS ( query2 )';
+
+        $this->assertEquals($string, $this->get_reflection_property_value('with'));
+    }
+
+    /**
+     * Test specifying the with part of a query without recursion but with column names
+     *
+     * @covers Lunr\Gravity\Database\DatabaseDMLQueryBuilder::sql_with
+     */
+    public function testNonRecursiveWithWithColumnNames()
+    {
+        $method = $this->get_accessible_reflection_method('sql_with');
+
+        $column_names = array('column1', 'column2', 'column3');
+
+        $method->invokeArgs($this->class, [ 'alias', 'query', $column_names ]);
+
+        $string = 'WITH alias (column1, column2, column3) AS ( query )';
+
+        $this->assertEquals($string, $this->get_reflection_property_value('with'));
+    }
+
+    /**
+     * Test specifying the with part of a query without recursion and with NULL as the column names
+     *
+     * @covers Lunr\Gravity\Database\DatabaseDMLQueryBuilder::sql_with
+     */
+    public function testNonRecursiveWithWithNullColumnNames()
+    {
+        $method = $this->get_accessible_reflection_method('sql_with');
+        $method->invokeArgs($this->class, [ 'alias', 'query', NULL ]);
+
+        $string = 'WITH alias AS ( query )';
+
+        $this->assertEquals($string, $this->get_reflection_property_value('with'));
+    }
+
+    /**
+     * Test specifying multiple with statements in a query without recursion and with column names
+     *
+     * @covers Lunr\Gravity\Database\DatabaseDMLQueryBuilder::sql_with
+     */
+    public function testMultipleNonRecursiveWithWithColumnNames()
+    {
+        $this->set_reflection_property_value
+            ('with', 'WITH alias (column1, column2, column3) AS ( query )');
+
+        $method = $this->get_accessible_reflection_method('sql_with');
+        $method->invokeArgs($this->class, [ 'alias2', 'query2']);
+
+        $string = 'WITH alias (column1, column2, column3) AS ( query ), alias2 AS ( query2 )';
+
+        $this->assertEquals($string, $this->get_reflection_property_value('with'));
+    }
+
+    /**
+     * Test specifying multiple with statements in a query without recursion and with multiple column names
+     *
+     * @covers Lunr\Gravity\Database\DatabaseDMLQueryBuilder::sql_with
+     */
+    public function testMultipleNonRecursiveWithWithMultipleColumnNames()
+    {
+        $this->set_reflection_property_value
+            ('with', 'WITH alias (column1, column2, column3) AS ( query )');
+
+        $method = $this->get_accessible_reflection_method('sql_with');
+
+        $column_names = array('column1', 'column2', 'column3');
+
+        $method->invokeArgs($this->class, [ 'alias2', 'query2', $column_names]);
+
+        $string = 'WITH alias (column1, column2, column3) AS ( query ), alias2 AS ( query2 )';
+
+        $this->assertEquals($string, $this->get_reflection_property_value('with'));
+    }
+
+    /**
+     * Test specifying a with statement with all values as NULL
+     *
+     * @covers Lunr\Gravity\Database\DatabaseDMLQueryBuilder::sql_with
+     */
+    public function testWithAllNull()
+    {
+
+        $method = $this->get_accessible_reflection_method('sql_with');
+        $method->invokeArgs($this->class, [NULL, NULL, NULL]);
+
+        $string = 'WITH  AS (  )';
+
+        $this->assertEquals($string, $this->get_reflection_property_value('with'));
+    }
+
+}
\ No newline at end of file
diff --git a/src/Lunr/Gravity/Database/Tests/SQLDMLQueryBuilderWithTest.php b/src/Lunr/Gravity/Database/Tests/SQLDMLQueryBuilderWithTest.php
new file mode 100644
index 0000000000000000000000000000000000000000..ab5fd68e831dc0b7e583328f618138cd5c51ea15
--- /dev/null
+++ b/src/Lunr/Gravity/Database/Tests/SQLDMLQueryBuilderWithTest.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * This file contains the DatabaseDMLQueryBuilderQueryPartsWithTest class.
+ *
+ * PHP Version 5.4
+ *
+ * @package    Lunr\Gravity\Database
+ * @author     Koen Hengsdijk
+ * @copyright  2012-2017, M2Mobi BV, Amsterdam, The Netherlands
+ * @license    http://lunr.nl/LICENSE MIT License
+ */
+
+namespace Lunr\Gravity\Database\Tests;
+
+use Lunr\Gravity\Database\SQLDMLQueryBuilder;
+
+/**
+ * This class contains the tests for the query parts necessary to build
+ * with statements.
+ *
+ * @covers Lunr\Gravity\Database\SQLDMLQueryBuilder
+ */
+class SQLDMLQueryBuilderWithTest extends SQLDMLQueryBuilderTest
+{
+
+    /**
+     * Test specifying the DELETE part of a query.
+     *
+     * @depends Lunr\Gravity\Database\Tests\DatabaseDMLQueryBuilderQueryPartsWithTest::testNonRecursiveWithWithoutColumnNames
+     * @covers  Lunr\Gravity\Database\SQLDMLQueryBuilder::delete
+     */
+    public function testWith()
+    {
+        $this->class->with('alias', 'query');
+        $value = $this->get_reflection_property_value('with');
+
+        $this->assertEquals('WITH alias AS ( query )', $value);
+    }
+
+    /**
+     * Test fluid interface of the with method.
+     *
+     * @covers Lunr\Gravity\Database\SQLDMLQueryBuilder::with
+     */
+    public function testWithReturnsSelfReference()
+    {
+        $return = $this->class->with('alias', 'query');
+
+        $this->assertInstanceOf('Lunr\Gravity\Database\SQLDMLQueryBuilder', $return);
+        $this->assertSame($this->class, $return);
+    }
+
+}
diff --git a/tests/phpunit.xml b/tests/phpunit.xml
index 9e4991178982bcc782d6df5841acda2f0b627b6d..869940f9cff0e9c5dbedf2183ef9a23881bcc3b6 100644
--- a/tests/phpunit.xml
+++ b/tests/phpunit.xml
@@ -115,6 +115,7 @@
             <file>../src/Lunr/Gravity/Database/Tests/DatabaseDMLQueryBuilderQueryPartsInsertTest.php</file>
             <file>../src/Lunr/Gravity/Database/Tests/DatabaseDMLQueryBuilderQueryPartsUpdateTest.php</file>
             <file>../src/Lunr/Gravity/Database/Tests/DatabaseDMLQueryBuilderQueryPartsUsingTest.php</file>
+            <file>../src/Lunr/Gravity/Database/Tests/DatabaseDMLQueryBuilderQueryPartsWithTest.php</file>
             <file>../src/Lunr/Gravity/Database/Tests/SQLDMLQueryBuilderSelectTest.php</file>
             <file>../src/Lunr/Gravity/Database/Tests/SQLDMLQueryBuilderInsertTest.php</file>
             <file>../src/Lunr/Gravity/Database/Tests/SQLDMLQueryBuilderUpdateTest.php</file>
@@ -123,6 +124,7 @@
             <file>../src/Lunr/Gravity/Database/Tests/SQLDMLQueryBuilderOrderByTest.php</file>
             <file>../src/Lunr/Gravity/Database/Tests/SQLDMLQueryBuilderLimitTest.php</file>
             <file>../src/Lunr/Gravity/Database/Tests/SQLDMLQueryBuilderUsingTest.php</file>
+            <file>../src/Lunr/Gravity/Database/Tests/SQLDMLQueryBuilderWithTest.php</file>
             <file>../src/Lunr/Gravity/Database/MySQL/Tests/MySQLDMLQueryBuilderSelectTest.php</file>
             <file>../src/Lunr/Gravity/Database/MySQL/Tests/MySQLDMLQueryBuilderDeleteTest.php</file>
             <file>../src/Lunr/Gravity/Database/MySQL/Tests/MySQLDMLQueryBuilderInsertTest.php</file>
