ReferenceSQL ReferenceStatements

INSERT ... SELECT

Insert query results into an existing table.

INSERT ... SELECT

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.

Syntax

INSERT INTO <table> [(<columns>)] SELECT ...

The target table must exist in the selected database. The API key needs read and write access.

Clauses

ClauseMeaning
<table>Existing target table.
<columns>Optional target column list.
SELECTQuery that supplies rows for the insert.
SETTINGSOptional settings passed to query execution.

URL table functions are not supported in the source query. VALUES and FORMAT inserts are not supported; use the table insert API for JSON rows.

Query API behavior

  • Supported: INSERT INTO ... SELECT ... with a table in the selected database.
  • Not exposed by the Query API: VALUES, FORMAT, INSERT INTO FUNCTION, and URL table functions.
  • Query API scope: The Query API accepts only INSERT ... SELECT for SQL inserts.

Errors

Error: Unsupported INSERT statement.

Hint:

Only INSERT INTO <table> [(columns)] SELECT ... is supported. Use the tables insert API for JSON rows.

URL table functions return this error.

Table functions are not supported in INSERT ... SELECT.

Hint:

Select from tables in your database.

Examples

Create a target table before you insert rows.

CREATE TABLE daily_events ENGINE = ReplicatedRawMergeTree

Insert aggregated results into the target.

INSERT INTO daily_events SELECT service, count() AS event_count FROM events GROUP BY service