BigQuery · DDL statement
BigQuery CREATE TABLE
CREATE TABLE creates a BigQuery table from a column list, from a query (CREATE TABLE AS SELECT), or both, with optional partitioning, clustering and OPTIONS such as expiration and description. Add OR REPLACE to overwrite a table or IF NOT EXISTS to skip one that exists.
CREATE TABLE takes a table name, a column list, a query, or a column list and a query together. With a column list, you define each column's type and can add NOT NULL, DEFAULT, OPTIONS(description = ...) and unenforced PRIMARY KEY / FOREIGN KEY constraints. With AS SELECT (CTAS), BigQuery takes column names and types from the query; if you also give a column list, columns are matched by position and the query's names are ignored. NOT NULL does not travel through a query: a CTAS from a table with REQUIRED columns produces NULLABLE columns.
CREATE OR REPLACE TABLE drops and recreates the table in one statement, so it is the idiomatic way to rebuild a derived table; IF NOT EXISTS does nothing when the table already exists. The two cannot be combined. Without either, creating an existing table fails with an Already Exists error. CREATE TEMP TABLE creates a table that lives only for a multi-statement query or session; see CREATE TEMP TABLE.
Table layout is decided here and is hard to change later. PARTITION BY splits the table by a date, timestamp or integer column so queries can skip partitions, and CLUSTER BY sorts it by up to four columns. You cannot add partitioning to an existing table, and CREATE OR REPLACE cannot replace a table that has a different kind of partitioning. Ingestion-time partitioning (_PARTITIONDATE) cannot be combined with AS SELECT.
OPTIONS sets table properties: expiration_timestamp (the table is deleted at that time), partition_expiration_days, require_partition_filter, description, friendly_name, labels, kms_key_name, default_rounding_mode and others. Options take constant expressions, so TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 7 DAY) works but a subquery does not. On cost: a plain CREATE TABLE processes no bytes, while CTAS bills the bytes its query reads, like any query, and the new table is billed as storage from then on.
Syntax
CREATE [OR REPLACE] [TEMP | TEMPORARY] TABLE [IF NOT EXISTS] table_name
[(
column_name data_type [NOT NULL] [DEFAULT expression] [OPTIONS (description = '...')][, ...]
[, PRIMARY KEY (column[, ...]) NOT ENFORCED]
)]
[DEFAULT COLLATE collation]
[PARTITION BY partition_expression]
[CLUSTER BY column[, ...]]
[OPTIONS (table_option = value[, ...])]
[AS query_statement]
CREATE TABLE table_name LIKE source_table
CREATE TABLE table_name COPY source_tableArguments and return value
| Argument | Description |
|---|---|
OR REPLACE | Replaces an existing table with the same name. Cannot be used with IF NOT EXISTS, or to change the kind of partitioning. |
IF NOT EXISTS | Does nothing, without an error, if a table with that name exists. |
table_name | project.dataset.table or dataset.table. Quote with backticks when the project ID contains dashes. |
column list | Column names and types, with optional NOT NULL (REQUIRED mode), DEFAULT, column OPTIONS and unenforced key constraints. Required unless AS query_statement is given. |
PARTITION BY / CLUSTER BY | Table layout. See PARTITION BY and CLUSTER BY. |
OPTIONS | Table properties such as expiration_timestamp, partition_expiration_days, require_partition_filter, description and labels. Values must be constant expressions. |
AS query_statement | Creates the table from the query result (CTAS). Column names and types come from the query unless a column list is given. |
Returns: A new, empty table, or a table filled with the query result for CREATE TABLE AS SELECT. The statement itself returns no rows.
Examples
A partitioned, clustered table with descriptions and labels
CREATE TABLE IF NOT EXISTS `project.dataset.orders`
(
order_id STRING NOT NULL OPTIONS (description = 'Order ID from the shop backend'),
customer_id STRING,
status STRING,
amount NUMERIC,
created_at TIMESTAMP NOT NULL,
PRIMARY KEY (order_id) NOT ENFORCED
)
PARTITION BY DATE(created_at)
CLUSTER BY customer_id, status
OPTIONS (
description = 'One row per order, loaded hourly',
labels = [('team', 'commerce'), ('tier', 'gold')],
require_partition_filter = TRUE
);Rebuild a reporting table with CREATE OR REPLACE TABLE AS SELECT
The usual pattern for a derived table that is recomputed on a schedule. On-demand it bills the bytes the SELECT reads; the date filter keeps that to 90 days of orders.
CREATE OR REPLACE TABLE `project.dataset.customer_revenue_90d`
CLUSTER BY customer_id
OPTIONS (description = 'Revenue per customer, last 90 days')
AS
SELECT
customer_id,
COUNT(*) AS orders,
SUM(amount) AS revenue
FROM `project.dataset.orders`
WHERE created_at >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 90 DAY)
AND status = 'paid'
GROUP BY customer_id;A scratch table that deletes itself
expiration_timestamp takes a constant expression evaluated when the statement runs. After seven days BigQuery deletes the table, so a forgotten copy stops costing storage.
CREATE TABLE `project.scratch.orders_debug_2026_11`
OPTIONS (
expiration_timestamp = TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 7 DAY),
description = 'Debug copy for ticket DATA-1432'
)
AS
SELECT *
FROM `project.dataset.orders`
WHERE DATE(created_at) = '2026-11-20';Copy only the schema with LIKE
LIKE copies the schema, partitioning, clustering and table options of an existing table into a new empty table. Use COPY instead to copy the data as well.
CREATE TABLE `project.dataset.orders_staging`
LIKE `project.dataset.orders`;Common mistakes
- Running a plain
CREATE TABLEin a scheduled job. The second run fails with Already Exists. UseCREATE OR REPLACE TABLEto rebuild, orIF NOT EXISTSto create once. - Expecting CTAS to keep REQUIRED columns, descriptions or partitioning from the source. A query result has none of these: declare them in the column list,
PARTITION BYandOPTIONSof the new table. - Using
CREATE OR REPLACEto add or change partitioning on an existing table name. A table with a different kind of partitioning cannot be replaced; create the new table under another name and swap, or drop the old one first. - Coming from PostgreSQL or Snowflake and expecting enforced keys and indexes. BigQuery primary and foreign keys must be declared
NOT ENFORCED; they inform the optimizer but do not reject duplicates. There is noCREATE INDEXfor ordinary lookups; partitioning and clustering play that role. - Putting a query in
OPTIONS. Option values must be constant expressions: literals, parameters and most scalar functions, but no subqueries, table references or UDFs.
Cost and performance
- A
CREATE TABLEwith a column list processes no bytes. CTAS bills the bytes read by its SELECT on demand, so filter the source on its partitioning column and select only the columns you need. CREATE OR REPLACE TABLE ... AS SELECTrewrites the whole table each run, which restarts the 90-day long-term storage timer. For tables that only grow, appending new rows with INSERT or MERGE keeps old partitions in cheaper long-term storage.- Set
expiration_timestampon scratch and debug tables, or a default table expiration on the dataset, so copies made during investigations stop accruing storage charges.
FAQ
How do I create a table from a query in BigQuery?
Use CREATE TABLE project.dataset.new_table AS SELECT ..., or CREATE OR REPLACE TABLE to overwrite an existing one. Column names and types come from the query.
What is the difference between CREATE OR REPLACE TABLE and CREATE TABLE IF NOT EXISTS?
OR REPLACE always produces a fresh table, dropping the old one. IF NOT EXISTS leaves an existing table untouched and does nothing. They cannot be used together.
How do I set a table to expire in BigQuery?
Add OPTIONS (expiration_timestamp = TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)) when you create it, or set it later with ALTER TABLE ... SET OPTIONS. For partitioned tables, partition_expiration_days expires individual partitions instead.
How do I copy a table's schema without data?
CREATE TABLE new_table LIKE existing_table copies the schema, partitioning, clustering and options without rows. CREATE TABLE new_table COPY existing_table copies the data as well.
Related
Errors you may hit
More DDL statements
All BigQuery DDL statements · All statements and clauses · BigQuery error messages
