View a markdown version of this page

CREATE TABLE and CREATE TABLE AS statements in DuckDB - Amazon Athena

CREATE TABLE and CREATE TABLE AS statements in DuckDB

Use CREATE TABLE to create a Hive table or an Apache Iceberg table in the AWS Glue Data Catalog. Use CREATE TABLE AS to create a table and fill it with the rows of a query in one statement. Both statements take the type of the table, its location in Amazon S3, and its other storage settings in a WITH clause.

Run each statement as its own query. A CREATE TABLE statement returns no rows. To add rows to a table that you create with CREATE TABLE, use INSERT INTO. For more information about INSERT INTO, see INSERT, UPDATE, and DELETE statements in DuckDB.

The examples create tables whose names start with ct_ in a database named sales_db, and store their data under s3://amzn-s3-demo-bucket/tables/. Some examples read sales_parquet, a Hive table in the same database that holds six orders in the regions east, north, and west. The sales_parquet table has the columns order_id (INTEGER), customer (VARCHAR), region (VARCHAR), amount (DECIMAL(10,2)), and order_date (DATE). Order 6 has a NULL amount. The amount column holds the order total in US dollars (USD). Many examples read the tables that other examples create, so run the examples in the order in which this topic shows them. To run an example, replace the database name and the Amazon S3 location with your own.

For more information about the statements that each table type supports, see SQL reference.

Synopsis

CREATE TABLE [ IF NOT EXISTS ] [ [ catalog. ]database. ]table_name ( column_name data_type [, ...] ) [ PARTITIONED BY ( { partition_column | transform( column ) } [, ...] ) ] WITH ( property_name = value [, ...] ) CREATE TABLE [ IF NOT EXISTS ] [ [ catalog. ]database. ]table_name [ PARTITIONED BY ( partition_column [, ...] ) ] WITH ( property_name = value [, ...] ) AS query [ WITH NO DATA ]

Create a Hive table

To create a Hive table, list its columns with their data types, and set type = 'HIVE' and location in the WITH clause. The statement registers the table in the AWS Glue Data Catalog. When you add rows, DuckDB writes the table's data files under location in Amazon S3. The format property sets the format of the data files. Without format, the table uses Parquet.

Name the new table with its database, as in sales_db.ct_orders_parquet, or with its catalog and database, as in awsdatacatalog.sales_db.ct_orders_parquet. A table name without a database refers to the database of the query execution context.

Syntax

CREATE TABLE [ IF NOT EXISTS ] [ [ catalog. ]database. ]table_name ( column_name data_type [, ...] ) WITH ( type = 'HIVE', location = 's3_location' [, format = 'file_format' ] [, property_name = value [, ...] ] )

Examples

The following SQL statement creates an unpartitioned Hive table that stores its data in Parquet files.

CREATE TABLE sales_db.ct_orders_parquet ( order_id INTEGER, customer VARCHAR, amount DECIMAL(10,2), order_date DATE ) WITH ( type = 'HIVE', format = 'parquet', location = 's3://amzn-s3-demo-bucket/tables/ct_orders_parquet/' )

The statement returns no rows.

The following SQL statement adds two rows to the new table.

INSERT INTO sales_db.ct_orders_parquet VALUES (1, 'alice', 120.50, DATE '2026-10-01'), (2, 'bob', 75.00, DATE '2026-10-01')

The statement returns no rows.

The following SQL query reads the rows of the new table.

SELECT order_id, customer, amount, order_date FROM sales_db.ct_orders_parquet ORDER BY order_id

The query returns the following results.

order_id customer amount order_date

1

alice

120.50

2026-10-01

2

bob

75.00

2026-10-01

The following SQL query reads the definition of the new table from the AWS Glue Data Catalog with glue_get_table_response. The table_type column shows how DuckDB reads the table. The glue_table_type column shows the table type that AWS Glue records. The returned location has no trailing slash.

SELECT table_type, glue_table_type, location FROM glue_get_table_response( 'sales_db.ct_orders_parquet' )

The query returns the following result.

table_type glue_table_type location

HIVE

EXTERNAL_TABLE

s3://amzn-s3-demo-bucket/tables/ct_orders_parquet

Considerations

A Hive table needs both type and location in the WITH clause.

If location already holds data files, for example the files of a table that you dropped, the new table reads them as its rows. Use an empty location for a new table.

Differences from Athena engine version 3

In Athena engine version 3, you create a Hive table with CREATE EXTERNAL TABLE and the STORED AS and LOCATION clauses. DuckDB does not accept CREATE EXTERNAL TABLE. Write CREATE TABLE, and set type = 'HIVE', format, and location in the WITH clause.

File formats of Hive tables

The format property of a Hive table sets the format of its data files: 'parquet', 'csv', 'json', or 'avro'. The default is 'parquet'. The value is not case-sensitive.

For a CSV table, you can also set delimiter, header, quote, and escape in the WITH clause. The default delimiter is a comma.

With header = true, the data files that DuckDB writes for the table start with a line of column names, and queries of the table skip that line. AWS Glue stores the setting in the table parameter skip.header.line.count, with the value 1.

To see the SerDe that AWS Glue records for a table, read the serde_library column of glue_get_table_response. For more information about glue_get_table_response, see glue_get_table_response function.

Syntax

WITH ( type = 'HIVE', location = 's3_location', format = { 'parquet' | 'csv' | 'json' | 'avro' } [, delimiter = 'character' ] [, header = true ] [, property_name = value [, ...] ] )

Examples

The following SQL statement creates a CSV table whose fields are separated by a vertical bar (|).

CREATE TABLE sales_db.ct_orders_csv ( order_id INTEGER, customer VARCHAR, amount DECIMAL(10,2) ) WITH ( type = 'HIVE', format = 'csv', delimiter = '|', location = 's3://amzn-s3-demo-bucket/tables/ct_orders_csv/' )

The statement returns no rows.

The following SQL statement adds two rows to the CSV table.

INSERT INTO sales_db.ct_orders_csv VALUES (1, 'alice', 120.50), (2, 'bob', 75.00)

The statement returns no rows.

The following SQL query reads the rows of the CSV table.

SELECT order_id, customer, amount FROM sales_db.ct_orders_csv ORDER BY order_id

The query returns the following results.

order_id customer amount

1

alice

120.50

2

bob

75.00

The following SQL query returns the field delimiter that AWS Glue records for the CSV table.

SELECT serde_parameters['field.delim'] AS field_delimiter FROM glue_get_table_response('sales_db.ct_orders_csv')

The query returns the following result.

field_delimiter

|

The following SQL statement creates a table that stores its data in JSON files.

CREATE TABLE sales_db.ct_orders_json ( order_id INTEGER, customer VARCHAR, amount DECIMAL(10,2) ) WITH ( type = 'HIVE', format = 'json', location = 's3://amzn-s3-demo-bucket/tables/ct_orders_json/' )

The statement returns no rows.

The following SQL statement copies the rows of the CSV table into the JSON table.

INSERT INTO sales_db.ct_orders_json SELECT order_id, customer, amount FROM sales_db.ct_orders_csv

The statement returns no rows.

The following SQL query reads the rows of the JSON table.

SELECT order_id, customer, amount FROM sales_db.ct_orders_json ORDER BY order_id

The query returns the following results.

order_id customer amount

1

alice

120.50

2

bob

75.00

The following SQL query returns the SerDe that AWS Glue records for the Parquet, CSV, and JSON tables.

SELECT table_name, serde_library FROM glue_get_table_response( 'sales_db.ct_orders_parquet' ) UNION ALL SELECT table_name, serde_library FROM glue_get_table_response('sales_db.ct_orders_csv') UNION ALL SELECT table_name, serde_library FROM glue_get_table_response('sales_db.ct_orders_json') ORDER BY table_name

The query returns the following results.

table_name serde_library

ct_orders_csv

org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe

ct_orders_json

org.apache.hive.hcatalog.data.JsonSerDe

ct_orders_parquet

org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe

Considerations

You cannot create ORC tables, bucketed tables, or Delta Lake or Apache Hudi tables.

Differences from Athena engine version 3

When DuckDB writes the DOUBLE and REAL values NaN, positive infinity, and negative infinity to a Hive CSV table, it writes them as nan, inf, and -inf. In contrast, Athena engine version 3 writes NaN, Infinity, and -Infinity.

Partition a Hive table

To partition a Hive table, list the partition columns in the column list with their data types, and name them again, without types, in PARTITIONED BY.

When you add rows, DuckDB writes the rows of each partition to a directory of its own under the table location, such as region=east/. It also adds each new partition to the AWS Glue Data Catalog. To list the partitions of a table, use the glue_partitions function.

A table can have more than one partition column, such as PARTITIONED BY (region, order_date). Each combination of values of the partition columns gets its own partition. A partition column can be a DATE column.

Syntax

CREATE TABLE [ [ catalog. ]database. ]table_name ( column_name data_type [, ...] ) PARTITIONED BY ( partition_column [, ...] ) WITH ( type = 'HIVE', location = 's3_location' [, ...] )

Examples

The following SQL statement creates a Hive table that is partitioned by region.

CREATE TABLE sales_db.ct_orders_by_region ( order_id INTEGER, customer VARCHAR, amount DECIMAL(10,2), region VARCHAR ) PARTITIONED BY (region) WITH ( type = 'HIVE', format = 'parquet', location = 's3://amzn-s3-demo-bucket/tables/ct_orders_by_region/' )

The statement returns no rows.

The following SQL statement adds three rows in two regions.

INSERT INTO sales_db.ct_orders_by_region VALUES (1, 'alice', 120.50, 'east'), (2, 'bob', 75.00, 'west'), (3, 'carol', 42.25, 'east')

The statement returns no rows.

The following SQL query reads the rows of the partitioned table.

SELECT order_id, customer, amount, region FROM sales_db.ct_orders_by_region ORDER BY order_id

The query returns the following results.

order_id customer amount region

1

alice

120.50

east

2

bob

75.00

west

3

carol

42.25

east

The following SQL query lists the partitions that the insert added, with the Amazon S3 location of each.

SELECT region, location FROM glue_partitions('sales_db.ct_orders_by_region') ORDER BY region

The query returns the following results.

region location

east

s3://amzn-s3-demo-bucket/tables/ct_orders_by_region/region=east

west

s3://amzn-s3-demo-bucket/tables/ct_orders_by_region/region=west

Considerations

Put PARTITIONED BY before the WITH clause.

Do not give the partition columns data types in PARTITIONED BY. A data type there is a syntax error.

DuckDB puts the partition columns after the other columns, whatever their place in the column list. List them last, so that the column order of the table matches your column list.

Differences from Athena engine version 3

In Athena engine version 3, you declare partition columns with their data types in PARTITIONED BY, apart from the other columns. In DuckDB, include the partition columns in the column list, and name them without types in PARTITIONED BY.

Create an Iceberg table

To create an Apache Iceberg table, set type = 'ICEBERG' and location in the WITH clause. The statement registers the table in the AWS Glue Data Catalog, and DuckDB writes the metadata files and the Parquet data files of the table under location.

Set the Iceberg format version of the table with the 'format-version' property, to '2' or '3'. Put the property name in single quotation marks, because it contains a hyphen. If you leave out 'format-version', the table uses format version 2.

An Iceberg table supports row-level changes, such as UPDATE and DELETE. A Hive table does not support them. For more information about these statements, see INSERT, UPDATE, and DELETE statements in DuckDB.

Syntax

CREATE TABLE [ [ catalog. ]database. ]table_name ( column_name data_type [, ...] ) [ PARTITIONED BY ( { partition_column | transform( column ) } [, ...] ) ] WITH ( type = 'ICEBERG', location = 's3_location' [, 'format-version' = { '2' | '3' } ] [, property_name = value [, ...] ] )

Examples

The following SQL statement creates an unpartitioned Iceberg table in format version 2.

CREATE TABLE sales_db.ct_orders_iceberg ( order_id INTEGER, customer VARCHAR, amount DECIMAL(10,2), order_date DATE ) WITH ( type = 'ICEBERG', location = 's3://amzn-s3-demo-bucket/tables/ct_orders_iceberg/', 'format-version' = '2' )

The statement returns no rows.

The following SQL statement adds two rows to the Iceberg table.

INSERT INTO sales_db.ct_orders_iceberg VALUES (1, 'alice', 120.50, DATE '2026-10-01'), (2, 'bob', 75.00, DATE '2026-10-01')

The statement returns no rows.

The following SQL query reads the rows of the Iceberg table.

SELECT order_id, customer, amount, order_date FROM sales_db.ct_orders_iceberg ORDER BY order_id

The query returns the following results.

order_id customer amount order_date

1

alice

120.50

2026-10-01

2

bob

75.00

2026-10-01

The following SQL query counts the snapshots of the Iceberg table after the insert. The table has one snapshot, which the insert made. A CREATE TABLE statement alone makes no snapshot.

SELECT count(*) AS snapshots FROM iceberg_snapshots('sales_db.ct_orders_iceberg')

The query returns the following result.

snapshots

1

The following SQL query lists the Iceberg table properties of the new table. The CREATE TABLE statement set only type, location, and 'format-version', so the table has the two properties that a new Iceberg table gets by default.

SELECT key, value FROM iceberg_table_properties( 'sales_db.ct_orders_iceberg' ) ORDER BY key

The query returns the following results.

key value

write.parquet.compression-codec

zstd

write.parquet.shred-variants

true

The following SQL statement creates an Iceberg table in format version 3.

CREATE TABLE sales_db.ct_orders_iceberg_v3 ( order_id INTEGER, customer VARCHAR, amount DECIMAL(10,2), order_date DATE ) WITH ( type = 'ICEBERG', location = 's3://amzn-s3-demo-bucket/tables/ct_orders_iceberg_v3/', 'format-version' = '3' )

The statement returns no rows.

The following SQL statement copies the rows of the format version 2 table into the format version 3 table.

INSERT INTO sales_db.ct_orders_iceberg_v3 SELECT order_id, customer, amount, order_date FROM sales_db.ct_orders_iceberg

The statement returns no rows.

The following SQL query reads the rows of the format version 3 table.

SELECT order_id, customer, amount FROM sales_db.ct_orders_iceberg_v3 ORDER BY order_id

The query returns the following results.

order_id customer amount

1

alice

120.50

2

bob

75.00

The following SQL query reads the format-version field of the table's metadata files in Amazon S3, which shows that the table uses format version 3.

SELECT DISTINCT "format-version" AS format_version FROM read_json( 's3://amzn-s3-demo-bucket/tables/ct_orders_iceberg_v3/metadata/*.metadata.json', columns := {'format-version': 'INTEGER'} )

The query returns the following result.

format_version

3

Considerations

DuckDB reads Iceberg tables in format version 1, but does not write to them. A CREATE TABLE statement with 'format-version' = '1' creates a table that does not accept INSERT INTO, so use format version 2 or 3 for a new table.

If location already holds the files of an Iceberg table that you dropped, a new Iceberg table at that location does not read them, and starts with no rows.

DuckDB does not support CREATE OR REPLACE TABLE for Iceberg tables. To replace a table, drop it with DROP TABLE, and then create it again.

Differences from Athena engine version 3

In Athena engine version 3, you create an Iceberg table with the LOCATION clause and TBLPROPERTIES ('table_type' = 'ICEBERG'). In DuckDB, set type = 'ICEBERG' and location in the WITH clause.

Partition an Iceberg table

To partition an Iceberg table, name the partition columns, or partition transforms of columns, in PARTITIONED BY, before the WITH clause. A column name alone partitions the table by the values of the column. A partition transform derives the partition value from a column. For example, day(order_date) partitions the table by the day of order_date.

The year, month, and day transforms take the year, month, or day of a DATE column. The hour transform takes the hour of a TIMESTAMP column.

To see the partition fields of an Iceberg table, with their transforms, use the iceberg_partition_specs function. A partition field has the name of its source column. When the field uses a transform, the name of the transform follows, after an underscore, as in order_date_day.

The bucket transform takes two arguments, the number of buckets and a column, as in bucket(4, order_id). It puts each row in a bucket by a hash of the column value. The truncate transform takes a width and a column, as in truncate(1, customer), and partitions by the value cut to that width. Their partition fields are named like order_id_bucket_4 and customer_trunc_1.

Syntax

PARTITIONED BY ( { partition_column | transform( column ) | bucket( n, column ) | truncate( n, column ) } [, ...] )

Examples

The following SQL statement creates an Iceberg table that is partitioned by region and by the day of order_date.

CREATE TABLE sales_db.ct_orders_partitioned ( order_id INTEGER, customer VARCHAR, region VARCHAR, amount DECIMAL(10,2), order_date DATE ) PARTITIONED BY (region, day(order_date)) WITH ( type = 'ICEBERG', location = 's3://amzn-s3-demo-bucket/tables/ct_orders_partitioned/', 'format-version' = '2' )

The statement returns no rows.

The following SQL statement copies the six orders of sales_parquet into the partitioned table.

INSERT INTO sales_db.ct_orders_partitioned SELECT order_id, customer, region, amount, order_date FROM sales_db.sales_parquet

The statement returns no rows.

The following SQL query reads the orders of one day from the partitioned table.

SELECT order_id, customer, region, order_date FROM sales_db.ct_orders_partitioned WHERE order_date = DATE '2026-10-03' ORDER BY order_id

The query returns the following results.

order_id customer region order_date

4

dave

north

2026-10-03

5

erin

west

2026-10-03

The following SQL query returns the partition fields of the table, with the transform and the source column of each. The column name region alone gives a field with the identity transform.

SELECT name, transform, source_column FROM iceberg_partition_specs( 'sales_db.ct_orders_partitioned' ) ORDER BY field_id

The query returns the following results.

name transform source_column

region

identity

region

order_date_day

day

order_date

Table properties in the WITH clause

The WITH clause holds the properties of the new table, as name = value pairs separated by commas. Write the values of type, location, format, and 'format-version' as string literals. A property name that contains a hyphen, such as 'format-version', goes in single quotation marks. You can quote any property name, as in 'type' = 'HIVE'.

For a Hive table, DuckDB stores other properties, such as team = 'support', as AWS Glue table parameters with the same name and value.

For an Iceberg table, DuckDB stores other properties as Iceberg table properties. When you set a property that a new Iceberg table gets by default, your value replaces the default. For example, 'write.parquet.compression-codec' = 'snappy' replaces the default value zstd. DuckDB also stores custom properties, such as team, as AWS Glue table parameters.

DuckDB uses the following properties.

type

The type of the table: 'HIVE' for a Hive table, or 'ICEBERG' for an Apache Iceberg table. The value is not case-sensitive. This parameter is required.

location

The Amazon S3 location of the table, such as 's3://amzn-s3-demo-bucket/tables/orders/'. DuckDB writes the data files of the table under this location. For an Iceberg table, it also writes the metadata files there. This parameter is required, except for tables in Amazon S3 Tables, which do not take a location.

format

The format of the data files of a Hive table: 'parquet' (the default), 'csv', 'json', or 'avro'. For an Iceberg table, DuckDB writes the data files in Parquet, and accepts format = 'parquet'.

format-version

The format version of an Iceberg table: '2' (the default) or '3'. Write the name in single quotation marks, as 'format-version'.

comment

A description of the table. For a Hive table, DuckDB stores it as the description of the table in the AWS Glue Data Catalog and as the comment AWS Glue table parameter. For an Iceberg table, DuckDB stores it as the comment Iceberg table property and as the comment AWS Glue table parameter. However, DuckDB does not set the description of an Iceberg table in the AWS Glue Data Catalog.

delimiter

The character that separates the fields of a CSV table. The default is a comma.

header

A flag that starts the data files of a CSV table with a line of column names. To turn it on, set header = true. Queries of the table skip that line.

quote

The character that encloses a quoted field of a CSV table.

escape

The character that escapes a quotation character inside a quoted field of a CSV table.

Examples

The following SQL statement creates a Hive table without a format property, so the table uses Parquet. The table has a description and a custom table parameter, team.

CREATE TABLE sales_db.ct_order_notes ( order_id INTEGER, note VARCHAR ) WITH ( type = 'HIVE', location = 's3://amzn-s3-demo-bucket/tables/ct_order_notes/', comment = 'Notes from the support team', team = 'support' )

The statement returns no rows.

The following SQL statement adds two rows to the table.

INSERT INTO sales_db.ct_order_notes VALUES (1, 'gift wrap'), (2, 'leave at the front desk')

The statement returns no rows.

The following SQL query reads the rows of the table.

SELECT order_id, note FROM sales_db.ct_order_notes ORDER BY order_id

The query returns the following results.

order_id note

1

gift wrap

2

leave at the front desk

The following SQL query returns the description, the comment and team table parameters, and the SerDe that AWS Glue records for the table. The description comes from the Description field of the response column, which holds the complete AWS Glue Table object.

SELECT CAST( response.Description AS VARCHAR ) AS description, parameters['comment'] AS comment_parameter, parameters['team'] AS team, serde_library FROM glue_get_table_response('sales_db.ct_order_notes')

The query returns the following result.

description comment_parameter team serde_library

Notes from the support team

Notes from the support team

support

org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe

Considerations

DuckDB does not apply the encryption_option and kms_key table properties. The data that it writes to Amazon S3 uses the default encryption of the bucket.

DuckDB does not accept the Athena engine version 3 property field_delimiter. Use delimiter instead.

Column types

Give each column a data type, such as INTEGER, DECIMAL(10,2), VARCHAR, DATE, or TIMESTAMP. A column can also have a nested type, such as VARCHAR[] for a list of strings, MAP(VARCHAR, VARCHAR), or STRUCT(city VARCHAR, zip VARCHAR). Other names of the same types also work, such as INT and STRING. For more information about data types, see Data types in DuckDB.

Hive tables in Parquet format and Iceberg tables can both store list, map, and struct columns.

The types that a table can store depend on the type of the table. For example, a Hive table cannot store INTERVAL values, and an Iceberg table cannot store UBIGINT or JSON values. For more information about the types that each table type can store, see SQL reference.

Examples

The following SQL statement creates a Hive table with columns of several types, including a list, a map, and a struct.

CREATE TABLE sales_db.ct_column_types ( id BIGINT, active BOOLEAN, price DECIMAL(10,2), ratio DOUBLE, created_on DATE, updated_at TIMESTAMP, tags VARCHAR[], attributes MAP(VARCHAR, VARCHAR), address STRUCT(city VARCHAR, zip VARCHAR) ) WITH ( type = 'HIVE', format = 'parquet', location = 's3://amzn-s3-demo-bucket/tables/ct_column_types/' )

The statement returns no rows.

The following SQL statement adds one row, with a literal value of each type.

INSERT INTO sales_db.ct_column_types VALUES ( 1, true, 19.99, 0.25, DATE '2026-10-01', TIMESTAMP '2026-10-01 12:30:00', ['red', 'blue'], MAP {'size': 'L'}, {'city': 'Seattle', 'zip': '98101'} )

The statement returns no rows.

The following SQL query reads the row back.

SELECT id, active, price, ratio, created_on, updated_at, tags, attributes, address FROM sales_db.ct_column_types

The query returns the following result.

id active price ratio created_on updated_at tags attributes address

1

true

19.99

0.25

2026-10-01

2026-10-01 12:30:00

[red, blue]

{size=L}

{'city': Seattle, 'zip': 98101}

The following SQL query returns the types of six of the columns, as the table stores them. Each type is the type that the CREATE TABLE statement gave the column.

SELECT typeof(id) AS id_type, typeof(price) AS price_type, typeof(updated_at) AS updated_at_type, typeof(tags) AS tags_type, typeof(attributes) AS attributes_type, typeof(address) AS address_type FROM sales_db.ct_column_types

The query returns the following result.

id_type price_type updated_at_type tags_type attributes_type address_type

BIGINT

DECIMAL(10,2)

TIMESTAMP

VARCHAR[]

MAP(VARCHAR, VARCHAR)

STRUCT(city VARCHAR, zip VARCHAR)

Considerations

A DECIMAL column without a precision and scale is DECIMAL(18,3). Give the precision and scale that your values need, as in DECIMAL(10,2).

Write nested types with DuckDB type names, such as VARCHAR[] and STRUCT(city VARCHAR). Hive type names with angle brackets, such as array<string>, are a syntax error.

A Hive table does not accept column constraints, such as NOT NULL. An Iceberg table in format version 2 does not accept a DEFAULT value for a column.

IF NOT EXISTS

With IF NOT EXISTS, CREATE TABLE does nothing when a table with the same name already exists in the database. The existing table, its columns, and its rows do not change, even when the statement lists other columns. Without IF NOT EXISTS, the statement fails when the table exists.

CREATE TABLE AS also takes IF NOT EXISTS. When the table exists, the statement adds no rows to it.

Syntax

CREATE TABLE IF NOT EXISTS [ [ catalog. ]database. ]table_name ( column_name data_type [, ...] ) [ PARTITIONED BY ( partition_column [, ...] ) ] WITH ( property_name = value [, ...] ) CREATE TABLE IF NOT EXISTS [ [ catalog. ]database. ]table_name [ PARTITIONED BY ( partition_column [, ...] ) ] WITH ( property_name = value [, ...] ) AS query

Examples

The following SQL statement creates ct_orders_parquet again, with IF NOT EXISTS. The table exists, so the statement does nothing.

CREATE TABLE IF NOT EXISTS sales_db.ct_orders_parquet ( order_id INTEGER, customer VARCHAR, amount DECIMAL(10,2), order_date DATE ) WITH ( type = 'HIVE', format = 'parquet', location = 's3://amzn-s3-demo-bucket/tables/ct_orders_parquet/' )

The statement returns no rows.

The following SQL query shows that the table still holds its two rows.

SELECT order_id, customer FROM sales_db.ct_orders_parquet ORDER BY order_id

The query returns the following results.

order_id customer

1

alice

2

bob

CREATE TABLE AS

CREATE TABLE AS, also called CTAS, creates a table and fills it with the rows of a query, in one statement. The columns of the new table take their names and data types from the query, so give every expression in the select list an alias.

The WITH clause takes the same properties as in CREATE TABLE, for Hive and Iceberg tables. To partition the new table, add PARTITIONED BY before the WITH clause, and name columns of the query in it.

The query can read tables or a VALUES list.

To create the table without filling it, add WITH NO DATA after the query.

Syntax

CREATE TABLE [ IF NOT EXISTS ] [ [ catalog. ]database. ]table_name [ PARTITIONED BY ( partition_column [, ...] ) ] WITH ( type = { 'HIVE' | 'ICEBERG' }, location = 's3_location' [, property_name = value [, ...] ] ) AS query [ WITH NO DATA ]

In the syntax, replace each placeholder as follows:

catalog

The catalog that holds the database, such as awsdatacatalog. This parameter is optional.

database

The database of the new table, such as sales_db. A table name without a database refers to the database of the query execution context. This parameter is optional.

table_name

The name of the new table.

partition_column

A column of the query to partition the new table by. This parameter is optional.

s3_location

The Amazon S3 location of the new table.

property_name

The name of another table property, such as format or 'format-version'. The WITH clause takes the same properties as in CREATE TABLE, including type and location. For more information about these properties, see Table properties in the WITH clause. This parameter is optional.

value

The value of the property.

query

The query whose rows fill the new table.

Examples

The following SQL statement creates a Hive table that holds the number of orders and the revenue of each region.

CREATE TABLE sales_db.ct_revenue_by_region WITH ( type = 'HIVE', format = 'parquet', location = 's3://amzn-s3-demo-bucket/tables/ct_revenue_by_region/' ) AS SELECT region, count(*) AS orders, sum(amount) AS revenue FROM sales_db.sales_parquet GROUP BY region

The statement returns no rows.

The following SQL query reads the new table. The east region has three orders, and its revenue leaves out order 6, whose amount is NULL.

SELECT region, orders, revenue FROM sales_db.ct_revenue_by_region ORDER BY region

The query returns the following results.

region orders revenue

east

3

162.75

north

1

310.00

west

2

93.75

The following SQL query returns the types that the columns of the new table took from the query. Here, count(*) returns a BIGINT, and sum of a DECIMAL(10,2) column returns a DECIMAL(38,2).

SELECT typeof(orders) AS orders_type, typeof(revenue) AS revenue_type FROM sales_db.ct_revenue_by_region LIMIT 1

The query returns the following result.

orders_type revenue_type

BIGINT

DECIMAL(38,2)

The following SQL statement creates a Hive table that is partitioned by region, from all six orders. The partition column is the last column of the query.

CREATE TABLE sales_db.ct_orders_by_region_ctas PARTITIONED BY (region) WITH ( type = 'HIVE', format = 'parquet', location = 's3://amzn-s3-demo-bucket/tables/ct_orders_by_region_ctas/' ) AS SELECT order_id, customer, amount, region FROM sales_db.sales_parquet

The statement returns no rows.

The following SQL query counts the orders in each region of the new table.

SELECT region, count(*) AS orders FROM sales_db.ct_orders_by_region_ctas GROUP BY region ORDER BY region

The query returns the following results.

region orders

east

3

north

1

west

2

The following SQL query lists the partitions that the CREATE TABLE AS statement added.

SELECT region FROM glue_partitions( 'sales_db.ct_orders_by_region_ctas' ) ORDER BY region

The query returns the following results.

region

east

north

west

The following SQL statement creates an Iceberg table from the orders with an amount of 75 or more.

CREATE TABLE sales_db.ct_large_orders WITH ( type = 'ICEBERG', location = 's3://amzn-s3-demo-bucket/tables/ct_large_orders/', 'format-version' = '2' ) AS SELECT order_id, customer, amount FROM sales_db.sales_parquet WHERE amount >= 75

The statement returns no rows.

The following SQL query reads the new Iceberg table.

SELECT order_id, customer, amount FROM sales_db.ct_large_orders ORDER BY order_id

The query returns the following results.

order_id customer amount

1

alice

120.50

2

bob

75.00

4

dave

310.00

The following SQL statement creates an Iceberg table in format version 3 from a VALUES list.

CREATE TABLE sales_db.ct_region_names WITH ( type = 'ICEBERG', location = 's3://amzn-s3-demo-bucket/tables/ct_region_names/', 'format-version' = '3' ) AS SELECT region, region_name FROM ( VALUES ('east', 'Eastern'), ('north', 'Northern'), ('west', 'Western') ) AS t(region, region_name)

The statement returns no rows.

The following SQL query reads the new table.

SELECT region, region_name FROM sales_db.ct_region_names ORDER BY region

The query returns the following results.

region region_name

east

Eastern

north

Northern

west

Western

Considerations

The table must be able to store the type of every column of the query. For example, an Iceberg table cannot store UBIGINT values, so cast such a column in the query, as in CAST(n AS BIGINT) AS n.

Do not list columns after the table name. A column list in CREATE TABLE AS is a syntax error.

A CREATE TABLE AS statement for a Hive table fails when location already holds files, for example the files of a table that you dropped. Delete the files, use another location, or add overwrite = true to the WITH clause. With overwrite = true, DuckDB deletes the data files at location before it writes the new table.

Differences from Athena engine version 3

In Athena engine version 3, a CTAS that creates a Hive table sets its Amazon S3 location with external_location. In DuckDB, set type = 'HIVE' and location in the WITH clause. You can also set format. Without format, the table uses Parquet.