# Keep rows found in both tables

Join / setops

A variant of [Intersect or subtract tables](https://meridian.online/operations/intersect-or-subtract-tables).

Keep the rows that appear in both inputs, as a set or with duplicates.

Page: <https://meridian.online/operations/intersect-or-subtract-tables/keep-rows-found-in-both-tables>

## Status

Runs as a SQL step.

arcform carries it as a `sql:` step today.

## The DuckDB step

keep the rows found in both tables, as a set and with duplicates

```sql
CREATE TEMP TABLE a AS SELECT * FROM (VALUES
  (1, 'x'), (1, 'x'), (2, 'y'), (NULL, 'z'), (NULL, 'z'), (3, 'w')) t(k, v);
CREATE TEMP TABLE b AS SELECT * FROM (VALUES
  (1, 'x'), (1, 'x'), (1, 'x'), (NULL, 'z'), (2, 'y'), (4, 'u')) t(k, v);

-- a holds (1, x) twice and (NULL, z) twice; b holds (1, x) three times and (NULL, z) once; NULLs count as equal
CREATE TEMP TABLE as_set AS SELECT * FROM a INTERSECT SELECT * FROM b;
CREATE TEMP TABLE with_duplicates AS SELECT * FROM a INTERSECT ALL SELECT * FROM b;
SELECT * FROM as_set ORDER BY k NULLS LAST;
SELECT * FROM with_duplicates ORDER BY k NULLS LAST;
SELECT CASE WHEN ((SELECT list((k, v) ORDER BY k NULLS LAST) IS NOT DISTINCT FROM [(1, 'x'), (2, 'y'), (NULL, 'z')] FROM as_set)
  AND (SELECT list((k, v) ORDER BY k NULLS LAST) IS NOT DISTINCT FROM [(1, 'x'), (1, 'x'), (2, 'y'), (NULL, 'z')] FROM with_duplicates)) THEN 'as expected' ELSE error('the intersection should be 3 distinct rows as a set, and 4 rows with duplicates, where (1, x) stays twice (the smaller count) and (NULL, z) once') END AS verdict;
```

The statement builds its own data.

It needs nothing installed beyond DuckDB itself.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 95.7% of 324 rows (variant, version 3).

- Apache DataFusion: [`INTERSECT`](https://datafusion.apache.org/user-guide/sql/select.html) (read 2026-09-27)
- Apache DataFusion: [`INTERSECT`](https://datafusion.apache.org/user-guide/sql/select.html) (read 2026-09-27)
- Apache DataFusion: [`intersect`](https://docs.rs/datafusion/latest/datafusion/dataframe/struct.DataFrame.html) (read 2026-09-27)
- Apache DataFusion: [`intersect_distinct`](https://docs.rs/datafusion/latest/datafusion/dataframe/struct.DataFrame.html) (read 2026-09-27)
- dbt-utils: [`intersect`](https://docs.getdbt.com/reference/dbt-jinja-functions/cross-database-macros) (read 2026-09-27)
- Duckle: [`Intersect`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- Ibis: [`Intersection`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/relations.py) (read 2026-09-27)
