# Running dual execution (or hybrid) queries
> Query local and cloud data together using MotherDuck's dual execution hybrid query engine.
MotherDuck can use local data and remote data in the same query. The editors
on this page connect to your `my_db` MotherDuck database, so you can run each
example against your own account. "Local" data in these examples comes from an
inline `VALUES` clause, which DuckDB evaluates in the browser; the sales table
lives in MotherDuck, so the planner runs it remotely.

## Create a remote sales table

The editor below writes to `my_db.main.remote_sales_table`. The preview shows
what the second statement returns; run the query to materialize the table in
your own account.

<SQLExampleEditor
  database="my_db"
  title="Create a remote sales table"
  query={`CREATE OR REPLACE TABLE my_db.main.remote_sales_table AS
    SELECT
        'ABCD'[floor(random() * 3.999)::int + 1] AS item,
        current_date() - interval (random() * 100) days AS dt,
        floor(random() * 50)::int AS tally
    FROM generate_series(1000);

FROM my_db.main.remote_sales_table LIMIT 10;`}
  previewRows={[
    { item: 'D', dt: '2024-11-29 00:00:00', tally: 0 },
    { item: 'A', dt: '2024-10-04 00:00:00', tally: 17 },
    { item: 'A', dt: '2024-10-13 00:00:00', tally: 0 },
    { item: 'C', dt: '2024-11-05 00:00:00', tally: 49 },
    { item: 'A', dt: '2024-09-30 00:00:00', tally: 12 },
    { item: 'B', dt: '2024-09-27 00:00:00', tally: 47 },
    { item: 'C', dt: '2024-11-23 00:00:00', tally: 47 },
    { item: 'B', dt: '2024-09-18 00:00:00', tally: 13 },
    { item: 'A', dt: '2024-11-18 00:00:00', tally: 40 },
    { item: 'C', dt: '2024-09-18 00:00:00', tally: 4 },
  ]}
/>

In your own CLI or notebook you can use any database name. For example
`CREATE OR REPLACE DATABASE remote_db;` followed by `CREATE TABLE remote_db.sales AS ...`.

## Join local and remote data

The query below joins inline pricing data (local) with the sales table you
created above (remote) to produce revenue by month. DuckDB executes the
`VALUES` clause locally and reads `remote_sales_table` from MotherDuck.

<SQLExampleEditor
  database="my_db"
  title="Revenue by month"
  query={`SELECT
    date_trunc('month', sales.dt) AS mo,
    round(sum(pricing.price * sales.tally), 2) AS rev
FROM my_db.main.remote_sales_table AS sales
JOIN (
    VALUES ('A', 1.4), ('B', 1.12), ('C', 2.552), ('D', 5.23)
) AS pricing(item, price)
    ON sales.item = pricing.item
WHERE pricing.price > 2
GROUP BY mo
ORDER BY mo;`}
  previewRows={[
    { mo: '2024-09-01', rev: 9241.39 },
    { mo: '2024-10-01', rev: 14226.12 },
    { mo: '2024-11-01', rev: 13136.55 },
    { mo: '2024-12-01', rev: 7783.26 },
  ]}
/>

## Inspect the hybrid query plan

Prefix the query with `EXPLAIN` to see which operators run locally and which
run on MotherDuck. The editor renders DuckDB's JSON plan as a tree; each
operator carries an `L` (local) or `R` (remote) tag and the JOIN branches into
its two inputs.

<SQLExampleEditor
  database="my_db"
  title="Explain the hybrid query plan"
  query={`EXPLAIN (FORMAT JSON)
SELECT
    date_trunc('month', sales.dt) AS mo,
    round(sum(pricing.price * sales.tally), 2) AS rev
FROM my_db.main.remote_sales_table AS sales
JOIN (
    VALUES ('A', 1.4), ('B', 1.12), ('C', 2.552), ('D', 5.23)
) AS pricing(item, price)
    ON sales.item = pricing.item
WHERE pricing.price > 2
GROUP BY mo
ORDER BY mo;`}
  previewPlan={{
    label: 'physical_plan',
    nodes: [{
      name: 'DOWNLOAD_SOURCE',
      location: 'local',
      details: { bridge_id: '1' },
      children: [{
        name: 'BATCH_DOWNLOAD_SINK',
        location: 'remote',
        details: { bridge_id: '1', parallel: 'true' },
        children: [{
          name: 'ORDER_BY',
          location: 'remote',
          details: { 'Order By': "date_trunc('month', sales.dt) ASC" },
          children: [{
            name: 'HASH_GROUP_BY',
            location: 'remote',
            details: { Groups: '#0', Aggregates: 'sum(#1)' },
            children: [{
              name: 'PROJECTION',
              location: 'remote',
              details: { Projections: ['mo', 'price * tally'] },
              children: [{
                name: 'HASH_JOIN',
                location: 'remote',
                details: { 'Join Type': 'INNER', Conditions: 'item = item' },
                children: [
                  {
                    name: 'SEQ_SCAN',
                    location: 'remote',
                    details: {
                      Table: 'remote_sales_table',
                      Projections: ['item', 'dt', 'tally'],
                    },
                  },
                  {
                    name: 'UPLOAD_SOURCE',
                    location: 'remote',
                    details: { bridge_id: '2' },
                    children: [{
                      name: 'BATCH_UPLOAD_SINK',
                      location: 'local',
                      details: { bridge_id: '2', parallel: 'true' },
                      children: [{
                        name: 'SEQ_SCAN',
                        location: 'local',
                        details: {
                          Table: 'pricing (VALUES)',
                          Projections: ['item', 'price'],
                          Filters: 'price > 2',
                        },
                      }],
                    }],
                  },
                ],
              }],
            }],
          }],
        }],
      }],
    }],
  }}
/>

Data is transferred between local and remote with matching pairs of sinks and
sources, identified by `bridge_id`.

A dual execution (or hybrid) query can run on any database format supported by
DuckDB, including
[sqlite](https://duckdb.org/docs/stable/core_extensions/sqlite),
[postgres](https://duckdb.org/docs/stable/core_extensions/postgres.html)
and many others.


---

## Docs feedback

MotherDuck accepts optional user-submitted feedback about this page at `POST https://motherduck.com/docs/api/feedback/agent`.
For agents and automated tools, feedback submission should be user-confirmed before sending.

Payload:

```json
{
  "page_path": "/key-tasks/running-hybrid-queries/",
  "page_title": "Running dual execution (or hybrid) queries",
  "text": "<the user's feedback, max 2000 characters>",
  "source": "<optional identifier for your interface, for example 'claude.ai' or 'chatgpt'>"
}
```

`page_path` and `text` are required; `page_title` and `source` are optional. Responses: `200 {"feedback_id": "<uuid>"}`, `400` for malformed payloads, and `429` when rate-limited.
