Create table as Select, Create table Copy and Create table Like in BigQuery

Create Table As Select (CTAS) in BigQuery

The CTAS statement creates a new table by copying the schema and data from an existing table. It is a combination of CREATE TABLE statement and SELECT statement. The new table name given in the CREATE TABLE statement. The column details and source/existing table name given in the SELECT statement.

Syntax

  • OR REPLACEoptional argument. It replace the table if it is already exist. Cannot appear with IF NOT EXISTS.
  • IF NOT EXISTS optional argument. It creates the table only if it is not exist. Cannot appear with OR REPLACE.
  • table_name – New table name
  • OPTIONS(table_option_list)optional argument. Table options can be specified in this.
  • <Column_1>,<Column_2>.. – Either we can specify the exact column names of the source table or we can give star(*) for all the columns.
  • source_table_name – Existing table name

Example

Assume that we have a table called as job_post in BigQuery. It has the records as below.

CTAS example in BigQuery
CTAS example in BigQuery

Our goal is to take the backup of this table in BigQuery. In order to copy the schema and data of this table, we are writing the Create Table As Select statement.

The name of the back up table is job_post_bkup and the source table name is job_post. Since we want to copy all the columns from the source table , we have provided the Select * statement. To copy the specific columns, we can mention the column names in the Select statement.

Create Table As Select Statement example in BigQuery
Create Table As Select Statement example in BigQuery

The CTAS statement has created the back up table. Let’s verify the records in it.

Output of CTAS statement in BigQuery
Output of CTAS statement in BigQuery

As mentioned above, the new table job_post_bkup has the same schema and records as source table job_post.

Create Table Copy in BigQuery

The Create Table Copy statement creates a new table with the same metadata and data as source table. As we mentioned earlier, CTAS statement allows to mention the specific column names whereas Create Table Copy doesn’t allow to select the column names from source table.

Also the source table can be a table , table clone or table snapshot in BigQuery. While copying the table, it inherits the table properties from source table which includes partitioning, clustering and table options. The table options can be override by using the OPTIONS clause.

Syntax

Example

Let’s take another back up from the source table job_post. This time , we mentioned the new table name as job_post_bkup_july02.

As shown below, the back up table is created using the Create Table Copy statement in BigQuery.

Create Table Copy statement example in BigQuery
Create Table Copy statement example in BigQuery

The new table job_post_bkup_july02 has the same metadata and data as source table.

Select backup table in BigQuery
Select backup table in BigQuery

Create table Like in BigQuery

The Create table Like statement copies only the metadata of the source table. But we can include the as query_statement in this which copies both metadata and data of the source table.

By default, the new table inherits partitioning, clustering, and options metadata from the source table. The metadata can be customized using the OPTIONS clause.

Syntax

Example

In this example, we are copying only the metadata of the table job_post to new table job_post_popular.

Create table Like statement example in BigQuery
Create table Like statement example in BigQuery

Since it didn’t copy the data to the new table job_post_popular, the select statement is not returning any records.

table is empty for Create table Like in BigQuery
table is empty for Create table Like in BigQuery

Create table like with as select statement in BigQuery

Let’s copy the data using Create table like with as select statement. As shown below, we have added the as select * statement after the Create table like statement.

Along with this, we have mentioned the keyword or replace in the create statement to replace the existing table job_post_popular.

Create table like as select statement in BigQuery
Create table like as select statement in BigQuery

This time, we have added the where condition to select only the job post with Job title ‘Regional Sales Manager‘. So it copied only those records to the target table job_post_popular.

Copy both metadata and data of the table in BigQuery
Copy both metadata and data of the table in BigQuery

Recommended Articles