# Query a database for each input row

Acquire / database

A variant of [Read from a database](https://meridian.online/operations/read-from-a-database).

Run a query once per input row, or with keys taken from an input table, and return the matching database rows.

Page: <https://meridian.online/operations/read-from-a-database/query-a-database-for-each-input-row>

## Status

Runs as a SQL step.

arcform carries it as a `sql:` step today, with the `sqlite_scanner` extension.

## The DuckDB step

run a query for each input row and look up the rows for a table of keys

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
INSTALL sqlite; LOAD sqlite;
ATTACH '<your_scratch_dir>/store.sqlite' AS store (TYPE sqlite);
CREATE TABLE store.orders AS SELECT order_id, customer, amount::DOUBLE AS amount FROM orders;
DETACH store;
ATTACH '<your_scratch_dir>/store.sqlite' AS store (TYPE sqlite, READ_ONLY);
CREATE TEMP TABLE keys AS SELECT * FROM (VALUES ('Alice Ng'), ('Dev Patel'), ('Nobody')) t(customer);
-- the matching rows of the database, for keys taken from an input table
CREATE TEMP TABLE matching AS
SELECT s.order_id, s.customer, s.amount FROM store.orders s WHERE s.customer IN (SELECT customer FROM keys);
-- a query run once for each input row, with that row's key in it
CREATE TEMP TABLE per_row AS
SELECT k.customer, r.n, r.total
FROM keys k, LATERAL (SELECT count(*) AS n, sum(amount) AS total FROM store.orders s WHERE s.customer = k.customer) r;
SELECT * FROM matching ORDER BY order_id; SELECT * FROM per_row ORDER BY customer;
SELECT CASE WHEN ((SELECT list(order_id ORDER BY order_id) FROM matching) = [1, 6, 8, 10] AND (SELECT list(n ORDER BY customer) FROM per_row) = [2, 2, 0] AND (SELECT total FROM per_row WHERE customer = 'Alice Ng') = 340 AND (SELECT total FROM per_row WHERE customer = 'Nobody') IS NULL) THEN 'as expected' ELSE error('the keys did not return orders 1, 6, 8 and 10, or the per-row query did not count 2, 2 and 0 orders') END AS verdict;
```

Replace `<your_scratch_dir>` with a directory of your own that you can write to before running the statement.

The statement writes `store.sqlite` itself, in `<your_scratch_dir>`.

It needs the `sqlite_scanner` extension.

## What other tools call it

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

- EasyMorph: [`Select matching database rows`](https://help.easymorph.com/doku.php?id=transformations:selectdbrowsbykeys) (read 2026-09-27)
- KNIME Analytics Platform: [`DB Looper`](https://api.hub.knime.com/nodes/*ZAffXysEzEonM415?details=full) (read 2026-09-27)
- KNIME Analytics Platform: [`DB Parameterized Query Reader`](https://api.hub.knime.com/nodes/*XGaHanhyHTSgL1A1?details=full) (read 2026-09-27)
- Talend: [`Database lookup input components (family)`](https://help.qlik.com/talend/en-US/components/8.0/About) (read 2026-09-27)
