# CREATE TABLE (/docs/reference/sql/statements/create-table)



CREATE TABLE [#create-table]

This page describes statements accepted by the Query API in RawTree Cloud
and RawTree BYOC. Its restrictions apply to the API, not to all RawTree SQL.
See [ClickHouse compatibility and API scope](/docs/reference/clickhouse-compatibility).

Syntax [#syntax]

```sql
CREATE TABLE [IF NOT EXISTS] <table>
  ENGINE = ReplicatedRawMergeTree
  [ORDER BY <expressions>]
  [SETTINGS <name> = <value> [, ...]]
```

`CREATE TABLE` requires admin permission. The table belongs to the selected
database.

Clauses [#clauses]

| Clause          | Meaning                                                 |
| --------------- | ------------------------------------------------------- |
| `IF NOT EXISTS` | Do not return an error when the table exists.           |
| `ENGINE`        | Use `ReplicatedRawMergeTree`.                           |
| `ORDER BY`      | Set an optional sorting-key expression.                 |
| `SETTINGS`      | Set table settings with the engine's native validation. |

<a id="rawtree-behavior" />

Query API behavior [#query-api-behavior]

* **Supported:** The syntax shown above.
* **Not exposed by the Query API:** Column definitions, engine arguments, `AS SELECT`, other engines, and other table clauses.
* **Query API scope:** The API creates raw tables and selects the engine and database.

`CREATE TABLE ... SETTINGS` returns `400` on databases with custom S3 storage.
Create the table without a `SETTINGS` clause. Some settings can only be set at creation.

Errors [#errors]

Error: `Unsupported CREATE TABLE statement.`

Hint: The API returns the supported form with your table name.

```text
Only CREATE TABLE [IF NOT EXISTS] <table> ENGINE = ReplicatedRawMergeTree [ORDER BY <expressions>] [SETTINGS name = value, ...] is supported. Use: CREATE TABLE my_table ENGINE = ReplicatedRawMergeTree
```

Examples [#examples]

Create a table with an explicit sorting key.

```sql test
CREATE TABLE events ENGINE = ReplicatedRawMergeTree ORDER BY (tenant, event_time)
```

Create a table only when it does not exist.

```sql test
CREATE TABLE IF NOT EXISTS archive_events ENGINE = ReplicatedRawMergeTree
```

Related [#related]

* [Raw-table concepts](/docs/concepts/raw-tables)
* [Sorting-key concepts](/docs/concepts/sorting-keys)
* [Change a sorting key](/docs/guides/operate/change-sorting-key)
