Joins
A join combines each row of one source with the rows of another source that meet the join condition. The [ INNER ] JOIN form returns only the pairs of rows that match. LEFT [ OUTER ] JOIN also returns each row of the left source that has no match, with NULL in the columns of the right source. RIGHT [ OUTER ] JOIN does the same for the right source, and FULL [ OUTER ] JOIN for both. CROSS JOIN pairs every row of one source with every row of the other and takes no condition.
Give the condition with ON condition, or with USING (columns) when both sources have columns of the same name that must be equal. With USING, the result has one column for each join column, and with a full outer join that column holds the value from whichever side has one. NATURAL JOIN joins on every column name that the two sources share.
DuckDB also supports SEMI, ANTI, LATERAL, ASOF, and POSITIONAL joins. For more information about these joins, see SEMI and ANTI joins, LATERAL joins, ASOF joins, and POSITIONAL joins.
Syntax
from_item [ INNER | LEFT [ OUTER ] | RIGHT
[ OUTER ] | FULL [ OUTER ] ] JOIN from_item
{ ON condition | USING ( column [, ...] ) }
from_item NATURAL [ INNER | LEFT [ OUTER ] | RIGHT
[ OUTER ] | FULL [ OUTER ] ] JOIN from_item
from_item CROSS JOIN from_item
Examples
For a description of the sample tables that the examples on this page use, see SELECT statement in DuckDB.
The following SQL query joins each order to the manager of its region. The north region has no manager, so its order is left out.
SELECT s.order_id, s.customer, m.manager FROM sales_db.sales_parquet AS s JOIN (VALUES ('east', 'maria'), ('west', 'wei') ) AS m(region, manager) ON s.region = m.region ORDER BY s.order_id
The query returns the following results.
| order_id | customer | manager |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The following SQL query uses a left outer join, so the order in the north region is kept with a NULL manager.
SELECT s.order_id, s.customer, m.manager FROM sales_db.sales_parquet AS s LEFT JOIN (VALUES ('east', 'maria'), ('west', 'wei') ) AS m(region, manager) ON s.region = m.region ORDER BY s.order_id
The query returns the following results.
| order_id | customer | manager |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The following SQL query uses a full outer join with USING. The region column holds the value from whichever side has one, including the south region, which has a manager and no orders.
SELECT region, s.order_id, m.manager FROM ( SELECT * FROM sales_db.sales_parquet WHERE order_id <= 2 ) AS s FULL OUTER JOIN (VALUES ('east', 'maria'), ('south', 'li') ) AS m(region, manager) USING (region) ORDER BY region
The query returns the following results.
| region | order_id | manager |
|---|---|---|
|
|
|
|
|
|
|
|
|
The following SQL query pairs every size with every color.
SELECT s.size, c.color FROM (VALUES ('S'), ('M')) AS s(size) CROSS JOIN (VALUES ('red'), ('blue')) AS c(color) ORDER BY s.size, c.color
The query returns the following results.
| size | color |
|---|---|
|
|
|
|
|
|
|
|