Skip to main content
SQL Lookup stage showing database enrichment with parameterized queries
The SQL Lookup stage enriches documents by querying external SQL databases. It executes parameterized queries using document fields as inputs, joining external structured data with your search results.
Stage Category: APPLY (Enriches documents)Transformation: N documents → N documents (with SQL data added)

When to Use

When NOT to Use

Parameters

Supported Databases

A connection to any other provider fails with Connection does not support SQL queries. For Snowflake or another warehouse, use the API Call stage against an endpoint that runs the query. See Join Warehouse Metrics onto Results.

Configuration Examples

Query Syntax

Parameter Placeholders

Use $1, $2, and so on for placeholders. The stage sorts the keys of parameters in alphabetical order and binds their values to the placeholders in that order. $1 takes the first key alphabetically.
With parameters set to {"user_id": "...", "status": "active"}, $1 is status and $2 is user_id. The database binds the values, so they are not concatenated into the query text.

Template Variables

Output Schema

Single Row (default)

Multiple Rows

No Results

Security

SQL queries are parameterized to prevent injection attacks. Never concatenate user input directly into query strings.

Performance

For high-volume lookups, ensure your database has appropriate indexes on the queried columns. Consider caching frequently accessed data.

Common Pipeline Patterns

Search + SQL Enrichment

Join Warehouse Metrics onto Results

A creative library often carries an ID such as creative_id that keys a metrics table. The table holds spend, ROAS, and thumb-stop rate. Two stages join that table onto search results. Where the table lives picks the stage. Both stages run after the search stage and add fields to each returned document. They do not change which documents were retrieved or how they scored. A search cannot rank by a joined value. To rank by ROAS, ingest it as document data.

From PostgreSQL

Read creative_id from the document metadata and bind it to $1.
Each result gains a warehouse_metrics object. A creative with no row gets null, because on_no_results defaults to null.

From Another Warehouse

Put an HTTP endpoint in front of the warehouse. The endpoint takes the ID in its path and returns one JSON object. The stage reads the token from an organization secret. See API Call.
The endpoint needs a public address and must return JSON. The stage sends one request per document, one after another. A retriever that returns 20 documents sends 20 requests.

Error Handling