# Compare two tables

Shape / inspect

A variant of [Check data quality](https://meridian.online/operations/check-data-quality).

Check that a table matches another or an expected result, row for row, by row count or by a summary figure.

Page: <https://meridian.online/operations/check-data-quality/compare-two-tables>

## Status

Runs as a SQL step.

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

## The DuckDB step

compare two tables row for row, by row count and by a summary figure

It reads the `orders` table the proofs share. Create it first:

```sql
CREATE TABLE orders AS SELECT * FROM (VALUES
  (1,  'Alice Ng',      'north', DATE '2026-01-03', 120.00, 'widget', 'a|b',   '{"channel":"web","items":[1,2]}'),
  (2,  'alice ng',      'north', DATE '2026-01-05',  80.50, 'gadget', 'b',     '{"channel":"shop","items":[3]}'),
  (3,  'Bob  Okafor',   'south', DATE '2026-01-05', 300.00, 'widget', 'a|c',   '{"channel":"web","items":[4,5,6]}'),
  (4,  'Carla Díaz',    'east',  DATE '2026-01-09',  NULL,  'gizmo',  NULL,    '{"channel":"web","items":[]}'),
  (5,  'Bob Okafor',    'south', DATE '2026-01-12',  45.25, 'gadget', 'c',     '{"channel":"phone","items":[7]}'),
  (6,  'Dev Patel',     'west',  DATE '2026-01-12', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (7,  'Carla Diaz',    'east',  DATE '2026-01-20',  99.99, 'gizmo',  'b|c',   '{"channel":"shop","items":[10]}'),
  (8,  'Dev Patel',     'west',  DATE '2026-02-02', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (9,  'Eve Larsen',    NULL,    DATE '2026-02-03',  15.00, 'gadget', NULL,    '{"channel":"web","items":[11]}'),
  (10, 'Alice Ng',      'north', DATE '2026-02-10', 220.00, 'gizmo',  'a|b|c', '{"channel":"phone","items":[12,13]}')
) t(order_id, customer, region, order_date, amount, product, tags, meta);
```

Then run the statement:

```sql
-- expected: order 3 has a different amount, order 10 is missing and order 11 is extra, so it also holds 10 rows
CREATE TEMP TABLE expected AS
SELECT order_id, customer, amount FROM orders WHERE order_id <> 3 AND order_id <> 10
UNION ALL SELECT 3, 'Bob  Okafor', 301.00
UNION ALL SELECT 11, 'Gus Hall', 12.00;
CREATE TEMP TABLE actual AS SELECT order_id, customer, amount FROM orders;
CREATE TEMP TABLE only_in_actual AS SELECT * FROM actual EXCEPT ALL SELECT * FROM expected;
CREATE TEMP TABLE only_in_expected AS SELECT * FROM expected EXCEPT ALL SELECT * FROM actual;
CREATE TEMP TABLE same_as_itself AS SELECT * FROM actual EXCEPT ALL SELECT * FROM actual;
CREATE TEMP TABLE figures AS
SELECT (SELECT count(*) FROM actual) AS actual_rows, (SELECT count(*) FROM expected) AS expected_rows,
       (SELECT sum(amount) FROM actual) AS actual_total, (SELECT sum(amount) FROM expected) AS expected_total;
SELECT * FROM only_in_actual ORDER BY order_id;
SELECT * FROM only_in_expected ORDER BY order_id;
SELECT * FROM figures;
SELECT CASE WHEN ((SELECT list(order_id ORDER BY order_id) FROM only_in_actual) = [3, 10] AND (SELECT list(order_id ORDER BY order_id) FROM only_in_expected) = [3, 11] AND (SELECT count(*) FROM same_as_itself) = 0 AND (SELECT actual_rows = expected_rows FROM figures) AND (SELECT actual_total <> expected_total FROM figures) AND (SELECT expected_total - actual_total FROM figures) = -207.00) THEN 'as expected' ELSE error('the two tables should differ by orders 3 and 10 one way and 3 and 11 the other while holding the same number of rows, with different totals') END AS verdict;
```

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).

- Alteryx Designer: [`Expect Equal`](https://help.alteryx.com/current/en/designer/tools/developer/expect-equal-tool.html) (read 2026-09-27)
- Bruin: [`Unit Tests`](https://getbruin.com/docs/bruin/quality/unit-tests.html) (read 2026-09-27)
- dbt-utils: [`cardinality_equality`](https://raw.githubusercontent.com/dbt-labs/dbt-utils/main/README.md) (read 2026-09-27)
- dbt-utils: [`equal_rowcount`](https://raw.githubusercontent.com/dbt-labs/dbt-utils/main/README.md) (read 2026-09-27)
- dbt-utils: [`equality`](https://raw.githubusercontent.com/dbt-labs/dbt-utils/main/README.md) (read 2026-09-27)
- dbt-utils: [`fewer_rows_than`](https://raw.githubusercontent.com/dbt-labs/dbt-utils/main/README.md) (read 2026-09-27)
- AWS Glue: [`AggregateMatch`](https://docs.aws.amazon.com/glue/latest/dg/dqdl-rule-types.html) (read 2026-09-27)
- AWS Glue: [`RowCountMatch`](https://docs.aws.amazon.com/glue/latest/dg/dqdl-rule-types.html) (read 2026-09-27)
- KNIME Analytics Platform: [`KNIME Testing Framework UI (extension)`](https://api.hub.knime.com/extensions/org.knime.features.testing.application) (read 2026-09-27)
- Polars: [`polars.testing asserts (family)`](https://docs.pola.rs/api/python/stable/reference/testing.html) (read 2026-09-27)
- SQLMesh: [`Unit tests`](https://github.com/SQLMesh/sqlmesh/blob/23b237cea7218cc8f338b89c14f0957394a0003b/docs/concepts/tests.md) (read 2026-09-27)
