View a markdown version of this page

FROM clause - Amazon Athena

FROM clause

The FROM clause lists the sources of the rows that a query reads. A source can be a table or view, a quoted Amazon S3 file path, a table function such as read_parquet or generate_series, a subquery in parentheses, or a VALUES list. Combine sources with joins. A comma between two sources is a cross join, which pairs every row of one with every row of the other.

A one-part table name refers to a table in the database of the query execution context. A two-part name is database.table, and a three-part name is catalog.database.table. Quote an identifier with double quotes.

Give a source an alias with AS alias. To rename its columns too, list the new names after the alias, as in AS t(id, buyer).

Syntax

FROM from_item [, ...] from_item: { [ catalog. ] [ database. ] table | 's3_path' | table_function ( argument [, ...] ) | ( select ) | ( VALUES ( expression [, ...] ) [, ...] ) } [ [ AS ] alias [ ( column_alias [, ...] ) ] ]

Examples

For a description of the sample tables that the examples on this page use, see SELECT statement in DuckDB.

The following SQL query gives the table an alias and new column names, and refers to them in the rest of the query.

SELECT t.id, t.buyer FROM sales_db.sales_parquet AS t(id, buyer) WHERE t.id <= 2 ORDER BY t.id

The query returns the following results.

id buyer

1

alice

2

bob

The following SQL query names the table with a three-part name that includes the catalog.

SELECT count(*) AS orders FROM awsdatacatalog.sales_db.sales_parquet

The query returns the following result.

orders

6

The following SQL query reads rows from the table function generate_series, which returns the integers from 1 through 3.

SELECT n FROM generate_series(1, 3) AS t(n) ORDER BY n

The query returns the following results.

n

1

2

3

The following SQL query reads the Parquet files of the sales_parquet table directly from Amazon S3 with read_parquet.

SELECT count(*) AS orders FROM read_parquet( 's3://amzn-s3-demo-bucket/tables/sales_parquet/*' )

The query returns the following result.

orders

6

The following SQL query reads the Parquet files of the sales_parquet table by their quoted Amazon S3 path, with a wildcard in place of the file names.

SELECT order_id, customer FROM 's3://amzn-s3-demo-bucket/tables/sales_parquet/*.parquet' WHERE region = 'west' ORDER BY order_id

The query returns the following results.

order_id customer

2

bob

5

erin

The following SQL query lists two tables separated by a comma, which pairs each of the six rows of one with each of the three rows of the other.

SELECT count(*) AS pairs FROM sales_db.sales_parquet, sales_db.orders_iceberg

The query returns the following result.

pairs

18

Considerations

When the query execution context does not set a catalog, a one-part or two-part name refers to the AWS Glue Data Catalog, AwsDataCatalog. When the context does not set a database, a one-part name refers to a table in the default database.

DuckDB reads a quoted Amazon S3 path in place of a table name as a file, as in FROM 's3://amzn-s3-demo-bucket/data/orders.parquet'. The path can contain a wildcard, such as *, and must end in .parquet, .csv, or .json. If the path ends in a bare *, DuckDB looks it up as a table name instead.

As with the reader functions, such as read_parquet, read_csv, and read_json, these reads do not go through the AWS Glue Data Catalog. For more information about the permissions that these reads use, see Reading files by Amazon S3 path.

DuckDB reads a quoted name without the s3:// prefix, such as FROM 'sales_parquet', as a table name. A quoted file name without the prefix, such as 'orders.parquet', fails, because DuckDB reads files only from Amazon S3.

To set reader options, such as the file format, a header row, or the column types, call the reader function instead of quoting the path.