# CREATE TABLE AS SELECT

## Syntax <a href="#synopsis" id="synopsis"></a>

```sql
CREATE TABLE
    table_name
[WITH (table_parameter = value [, ... ])] 
AS select_statement;
```

## Description <a href="#description" id="description"></a>

`CREATE TABLE AS` is a statement that:

* Generates a DDL statement to create a new [#table](https://docs.deltastream.io/overview/core-concepts/databases#table "mention").
* Launches a new query to write the results of the [select](https://docs.deltastream.io/reference/sql-syntax/query/select "mention") statement into the newly created table.

## Arguments <a href="#parameters" id="parameters"></a>

#### table\_name

This specifies the name of the new table. Optionally, use `<database_name>.<schema_name>` as the prefix to the name to create the relation in that namespace. If the name is case sensitive you must wrap it in double quotes; otherwise the system uses the lower case name.

#### WITH (\<table\_parameter> = \<value> \[, …​ ])

Optionally, this clause specifies [#stream\_parameters](#stream_parameters "mention").

#### select\_statement

This statement specifies the [select](https://docs.deltastream.io/reference/sql-syntax/query/select "mention") statement to run.

## Table Parameters <a href="#stream_parameters" id="stream_parameters"></a>

| Parameter Name | Description                                                                                                                                                                                                                                                                                                                                                                   |
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `store`        | <p>The name of the store that hosts the entity for this stream.<br></p><p><strong>Required:</strong> No<br><strong>Default value:</strong> User’s default <a data-mention href="../../../overview/core-concepts/store">store</a>.</p><p><strong>Type:</strong> String<br><strong>Valid values:</strong> See <a data-mention href="../command/list-stores">list-stores</a></p> |

### **Snowflake-Specific Parameters**

| Parameter Name            | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `snowflake.db.name`       | <p>The name of the Snowflake database that would host the Snowflake <a data-mention href="../../../../overview/core-concepts/databases#table">#table</a>.<br></p><p><strong>Required:</strong> Yes<br><strong>Default values:</strong> None<br><strong>Type:</strong> String<br><strong>Valid values:</strong> Database names available from <a data-mention href="../command/list-entities">list-entities</a>.</p>                                                                                                                                                                                         |
| `snowflake.schema.name`   | <p>The name of the Snowflake schema that would host the Snowflake <a data-mention href="../../../../overview/core-concepts/databases#table">#table</a>.<br></p><p><strong>Required:</strong> Yes<br><strong>Default value:</strong> None<br><strong>Type:</strong> String<br><strong>Valid values:</strong> Schema names available from <a data-mention href="../command/list-entities">list-entities</a> under the <code>snowflake.db.name</code> database name.</p>                                                                                                                                       |
| `snowflake.table.name`    | <p>The name of the Snowflake table to use when creating the <a data-mention href="../../../../overview/core-concepts/databases#table">#table</a>. If the table doesn't exist in the store, a table with the <code>table\_name</code> is created in the corresponding store.<br><br><strong>Required:</strong> No<br><strong>Default value:</strong> <code>table\_name</code><br><strong>Type:</strong> String</p>                                                                                                                                                                                           |
| `snowflake.buffer.millis` | <p>The amount of time to buffer events with the sink <a data-mention href="../../../../overview/core-concepts/databases#table">#table</a> before writing the data into the corresponding Snowflake table.<br></p><p>It is important to note that the longer events are buffered, the larger the internal state of the query gets. Depending on the volume and size of the events for the corresponding query, memory limitations may be reached.<br><br><strong>Required:</strong> No<br><strong>Default value:</strong> 1000<br><strong>Type:</strong> Long<br><strong>Valid values:</strong> (0, ...]</p> |

Snowflake stores provide a delivery guarantee of `at_least_once` when producing events into a sink [#table](https://docs.deltastream.io/overview/core-concepts/databases#table "mention"). Snowflake stores provide an insert-only mode when writing to Snowflake tables.

### **Databricks-Specific Parameters**

| Parameter Name             | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `databricks.catalog.name`  | <p>The name of the Databricks catalog that would host the Databricks <a data-mention href="../../../../overview/core-concepts/databases#table">#table</a>.<br></p><p><strong>Required:</strong> Yes<br><strong>Default values:</strong> None<br><strong>Type:</strong> String<br><strong>Valid values:</strong> Database names available from <a data-mention href="../command/list-entities">list-entities</a>.</p>                                                                                                                                                                                                                                                           |
| `databricks.schema.name`   | <p>The name of the Databricks schema that would host the Databricks <a data-mention href="../../../../overview/core-concepts/databases#table">#table</a>.<br></p><p><strong>Required:</strong> Yes<br><strong>Default value:</strong> None<br><strong>Type:</strong> String<br><strong>Valid values:</strong> Schema names available from <a data-mention href="../command/list-entities">list-entities</a> under the <code>databricks.catalog.name</code> catalog name.</p>                                                                                                                                                                                                   |
| `databricks.table.name`    | <p>The name of the Databricks table to use when creating the <a data-mention href="../../../../overview/core-concepts/databases#table">#table</a>. A table with the <code>table\_name</code> is created in the corresponding store. If that table already exists, then an error is reported back to the user.<br><br><strong>Required:</strong> No<br><strong>Default value:</strong> <code>table\_name</code><br><strong>Type:</strong> String</p>                                                                                                                                                                                                                            |
| `table.data.file.location` | <p>The S3 directory location for the Delta-formatted data to be written. This location is either a full S3 path, or a relative path to the data store's Cloud Provider Bucket (see <a data-mention href="../ddl/create-store">create-store</a>). The credentials for writing to S3 are given during data store creation (see <a data-mention href="../ddl/create-store">create-store</a>). Note that the S3 bucket from the location specified by this parameter must match the <code>databricks.cloud.s3.bucket</code> property defined in the data store.<br><br><strong>Required:</strong> Yes<br><strong>Default value:</strong> None<br><strong>Type:</strong> String</p> |

Databricks stores provide a delivery guarantee of `exactly_once` when producing events into a sink [#table](https://docs.deltastream.io/overview/core-concepts/databases#table "mention"). Databricks stores provide an insert-only mode when writing to Databricks tables.

### Postgres-Specific Parameters

| Parameter Name         | Description                                                                                                                                                                                                                    |
| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| postgresql.db.name     | <p><strong>Required</strong>:  Yes</p><p><br>See <a href="https://docs.deltastream.io/reference/sql-syntax/query/change-data-capture-cdc#step-1.-ddl-for-cdc-source">CDC source parameters</a> for the same parameter name</p> |
| postgresql.schema.name | <p><strong>Required</strong>:  Yes</p><p><br>See <a href="https://docs.deltastream.io/reference/sql-syntax/query/change-data-capture-cdc#step-1.-ddl-for-cdc-source">CDC source parameters</a> for the same parameter name</p> |
| postgresql.table.name  | <p><strong>Required</strong>:  No</p><p><br>See <a href="https://docs.deltastream.io/reference/sql-syntax/query/change-data-capture-cdc#step-1.-ddl-for-cdc-source">CDC source parameters</a> for the same parameter name</p>  |

### Iceberg-Specific Parameters

| Parameter Name                | Description                                                                                                                                                                                                                                      |
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `iceberg.aws.glue.db.name`    | <p>The name of the database (namespace) in the AWS Glue instance (used as the Iceberg Catalog implementation in the Iceberg Store) containing the existing Iceberg table. <strong>Required:</strong> Yes</p><p><strong>Type:</strong> String</p> |
| `iceberg.aws.glue.table.name` | <p>The name of the existing Iceberg table in the AWS Glue instance (used as the Iceberg Catalog implementation in the Iceberg Store).</p><p><strong>Required:</strong> Yes</p><p><strong>Type:</strong> String</p>                               |

## Examples — Snowflake

#### Create a copy of a stream in a Snowflake table

The following creates a replica of the source [#stream](https://docs.deltastream.io/overview/core-concepts/databases#stream "mention"), `pageviews` in the Snowflake [#table](https://docs.deltastream.io/overview/core-concepts/databases#table "mention"), `PV_TABLE`:

```sql
CREATE TABLE "PV_TABLE" WITH (
  'store' = 'sfstore',
  'snowflake.db.name' = 'DELTA_STREAMING',
  'snowflake.schema.name' = 'PUBLIC'
) AS SELECT * FROM pageviews;
```

#### Create a stream of changes for a changelog in a Snowflake table

The following CTAS query creates a new Snowflake [#table](https://docs.deltastream.io/overview/core-concepts/databases#table "mention") to store incremental changes resulting from a grouping aggregation on the `transactions` [#stream](https://docs.deltastream.io/overview/core-concepts/databases#stream "mention"):

```sql
CREATE TABLE "CC_TYPE_USAGE" WITH (
  'store' = 'sfstore',
  'snowflake.db.name' = 'DELTA_STREAMING',
  'snowflake.schema.name' = 'PUBLIC'
) AS SELECT
  cc_type AS "CREDIT_TYPE",
  tx_time AS "TRANSACTION_TS",
  tx_id AS "TRANSACTION_ID"
FROM transactions
GROUP BY cc_type;
```

This query stores insert and update changes to the grouping column `cc_type` to the sink table `CC_TYPE_USAGE`.

## Examples — PostgreSQL

Assumptions for the following 2 examples:

* `pageviews` is a stream you have already defined.
* `ps_store` is a PostgreSQL data store you have already created.&#x20;
  * For details and an example, see [CREATE STORE](https://docs.deltastream.io/reference/sql-syntax/ddl/create-store#create-a-postgresql-store).

#### Create a new table in a PostgresSQL data store — example 1

&#x20;In this example, you create a new table, named `pageviews`, in the given PostgreSQL data store under the public schema. The table has 3 columns:

1. viewtime
2. uid
3. pageid.

The query writes its results into this table as its sink.

```sql
CREATE TABLE pg_table WITH (
   'store'='ps_store', 
   'postgresql.db.name'='demo', 
   'postgresql.schema.name'='public', 
   'postgresql.table.name'='pageviews') AS 
SELECT viewtime, userid AS uid, pageid 
FROM pageviews;
```

#### Create a new table in a PostgresSQL data store — example 2

This  query creates a new table, named `pagevisits`, in the given PostgreSQL data store under the public schema.  The table has 2 columns:

1. `userid`
2. `cnt`

The query writes its results into this table.&#x20;

```sql
CREATE TABLE pagevisits WITH (
       'store'='ps_store', 
       'postgresql.db.name'='demo', 
       'postgresql.schema.name'='public') AS 
SELECT userid, count(*) AS cnt 
FROM pageviews 
GROUP BY userid;
```

{% hint style="info" %}
**Note**   Since the query includes a `GROUP BY` clause, the `userid` column — which is the grouping column — is considered the primary key for the results. The `pagevisits` table in PostgreSQL is created accordingly, with `userid` as its primary column.
{% endhint %}

## Examples — Databricks

#### Create a copy of a stream in a Databricks table

The following creates a replica of the source [#stream](https://docs.deltastream.io/overview/core-concepts/databases#stream "mention"), `pageviews` in the Databricks [#table](https://docs.deltastream.io/overview/core-concepts/databases#table "mention"), `pageviews_db`:

```sql
CREATE TABLE pageviews_db WITH (
  'store' = 'databricks_store', 
  'databricks.catalog.name' = 'catalog1', 
  'databricks.schema.name' = 'schema1', 
  'databricks.table.name' = 'pageviews', 
  'table.data.file.location' = 's3://mybucket/test/0/pageviews'
) AS SELECT * FROM pageviews;
```

Upon issuing this query, a Databricks table is created in `catalog1.schema1.pageviews` that uses `s3://mybucket/test/0/pageviews` as its external location. This query writes the Delta-formatted parquet files and updates the Delta log in that S3 location.
