INSERT
statementsupportedQueries & DMLUpdates a table by inserting one or more rows into the table. The values inserted into each column in the table can be explicitly-specified or the results of a query.
Syntax
supportedpartialnot supportednot scored
INSERT [ OVERWRITE ] INTO <target_table> [ ( <target_col_name> [ , ... ] ) ]
{
VALUES ( { <value> | DEFAULT | NULL } [ , ... ] ) [ , ( ... ) ]
| <query>
}
To replace selected rows, use OVERWRITE with DELETING WHERE
INSERT OVERWRITE INTO <target_table> [ AS <target_alias> ]
[ USING ( <using_query> ) [ AS ] <delta_alias> ]
DELETING WHERE ( <predicate> )
[ ( <target_col_name> [ , ... ] ) ]
{
VALUES ( { <value> | DEFAULT | NULL } [ , ... ] ) [ , ( ... ) ]
| <query>
}
Parameters
| Name | Required | Description |
|---|---|---|
target_table | required | Specifies the target table into which to insert rows. |
VALUES ( value | DEFAULT | NULL [ , ... ] ) [ , ( ... ) ] | required | Specifies one or more values to insert into the corresponding columns in the target table. In a VALUES clause, you can specify the following: value: Inserts the explicitly-specified value. The value can be a literal or an expression that evaluates to a single value. DEFAULT: Inserts the default value for the corresponding column in the target table. NULL: Inserts a NULL value. Each value in the clause must be separated by a comma. You can insert multiple rows by specifying additional sets of values in the clause. For more information, see the Usage notes and the Examples. |
query | required | Specify a query statement that returns values to be inserted into the corresponding columns. This allows you to insert rows into a target table from one or more source tables. |
OVERWRITE | optional | Specifies that existing rows are removed before new rows are inserted. Without DELETING WHERE, all existing rows are removed. With DELETING WHERE, only rows for which the predicate evaluates to TRUE are removed. Specifying this option doesn’t change the access control privileges granted on the table. INSERT statements with OVERWRITE can be processed within the scope of the current transaction, which avoids DDL statements that commit a transaction, such as:Copy code DROP TABLE t; CREATE TABLE t AS SELECT * FROM ... ; Default: No value (existing rows aren’t removed before performing the inserts). |
AS target_alias | optional | Specifies an alias for the target table in a conditional overwrite statement. Use this alias to qualify target columns in DELETING WHERE. The AS keyword is required for this alias. |
USING ( using_query ) [ AS ] delta_alias | optional | Defines a relation that subqueries in DELETING WHERE and the insertion query can both reference. The relation must have an alias; AS before this alias is optional. The USING query can’t reference the target alias. The relation isn’t an implicit row source in the deletion predicate. For example, use t.id IN (SELECT id FROM delta), rather than t.id = delta.id. |
DELETING WHERE ( predicate ) | optional | Specifies which existing target rows to remove. The predicate must be a Boolean expression enclosed in parentheses. Rows for which the predicate evaluates to FALSE or NULL remain in the table. Deletion and insertion are independent: all rows returned by the insertion query are inserted, even if they don’t satisfy the deletion predicate. Matching target rows are removed even when the insertion query returns no rows. The predicate doesn’t apply to newly inserted rows. This clause requires OVERWRITE. If you specify a target column list, place it after DELETING WHERE. |
( target_col_name [ , ... ] ) | optional | Specifies one or more columns in the target table into which the corresponding values are inserted. The number of target columns specified must match the number of specified values or columns (if the values are the results of a query) in the VALUES clause. Default: No value (all the columns in the target table are updated). |
Usage notes
- Using a single INSERT command, you can insert multiple rows into a table by specifying additional sets of values separated by commas in the VALUES clause. For example, the following clause would insert 3 rows in a 3-column table, with values 1, 2, and 3 in the first two rows and values 2, 3, and 4 in the third row: Copy code VALUES ( 1, 2, 3 ) , ( 1, 2, 3 ) , ( 2, 3, 4 )
- To use OVERWRITE on INSERT, you must use a role that has both INSERT and DELETE privileges on the target table. DELETING WHERE doesn’t require the TRUNCATE privilege.
- DELETING WHERE is supported only for single-target INSERT OVERWRITE. It isn’t supported for plain INSERT INTO or multi-table INSERT.
- Conditional overwrite supports permanent, temporary, and transient tables, and writable Iceberg tables. It doesn’t support versioned targets, such as an Iceberg branch, or other special table types such as hybrid, interactive, and event tables.
- To use conditional overwrite in a custom incremental dynamic table, specify SELF as the target inside REFRESH USING. Conditional overwrite isn’t supported as a direct statement against a dynamic table.
- Conditional overwrite preserves duplicates in retained rows and inserted rows. It doesn’t deduplicate replacement rows. Deletion and insertion form one transactional operation: if insertion fails, the deletion isn’t committed.
- Some types of expressions can’t be specified in the VALUES clause, including the following expressions: Subqueries For example: Copy code ... VALUES (SELECT id FROM other_table)
- Values of the semi-structured or structured data type. For example: Copy code ... VALUES (ARRAY_CONSTRUCT(1, 2, 3))
- Window functions For example: Copy code ... VALUES (ROW_NUMBER() OVER (...))
- Aggregate functions For example: Copy code ... VALUES (SUM(x))
- As an alternative to the VALUES clause, specify the expression in a query clause. For example, you can replace the following expression:
- with this expression:
- The VALUES clause is limited to 200,000 rows. This limit applies to a single INSERT INTO … VALUES statement and a single INSERT INTO … SELECT … FROM VALUES statement. Consider using the COPY INTO <table> command to perform a bulk data load. For more information about using the VALUES clause in a SELECT statement, see VALUES.
- For information about inserting data into hybrid tables, see Loading data.
- If error logging is turned on for the target table, a supported error, such as a NOT NULL or CHECK constraint violation, doesn’t cause the statement to fail. Instead, the statement records the error in the table’s error table and continues with the other rows. For more information, see Error logging for DML statements.