View a markdown version of this page

SEMI and ANTI joins - Amazon Athena

SEMI and ANTI joins

Use SEMI JOIN to keep the rows of the left source that the join condition pairs with one or more rows of the right source. Use ANTI JOIN to keep the rows of the left source that the condition pairs with no row of the right source. Both return only the columns of the left source, and each left row at most once, however many rows of the right source match it.

A semi join gives the same rows as WHERE EXISTS (...), and an anti join the same rows as WHERE NOT EXISTS (...).

Syntax

from_item { SEMI | ANTI } JOIN from_item { ON condition | USING ( column [, ...] ) }

Examples

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

The following SQL query returns the orders in sales_parquet that also appear in orders_iceberg.

SELECT s.order_id, s.customer FROM sales_db.sales_parquet AS s SEMI JOIN sales_db.orders_iceberg AS o ON s.order_id = o.order_id ORDER BY s.order_id

The query returns the following results.

order_id customer

1

alice

2

bob

3

carol

The following SQL query returns the orders in sales_parquet that do not appear in orders_iceberg.

SELECT s.order_id, s.customer FROM sales_db.sales_parquet AS s ANTI JOIN sales_db.orders_iceberg AS o ON s.order_id = o.order_id ORDER BY s.order_id

The query returns the following results.

order_id customer

4

dave

5

erin

6

frank