> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Resolving UNKNOWN_DATABASE or UNKNOWN_TABLE errors in subqueries and joins with remote and remoteSecure

> A subquery or JOIN in a query that reads from remote or remoteSecure runs on the remote server, where your local table does not exist. GLOBAL IN and GLOBAL JOIN send the local data along.

When the `FROM` clause of a query reads from the [`remote` or `remoteSecure`](https://clickhouse.com/docs/reference/functions/table-functions/remote) table function, ClickHouse sends the query to the remote server together with its `IN` subqueries and joined tables, and the remote server executes them. A table that exists only on the server where you start the query is unknown to the remote server, so the query fails with `UNKNOWN_DATABASE` or `UNKNOWN_TABLE`. Write `GLOBAL IN` or `GLOBAL JOIN` instead: the local server then runs the subquery itself and sends its result to the remote server together with the query.

## Example

The remote server has a table of orders:

```sql theme={null}
CREATE DATABASE shop;

CREATE TABLE shop.orders
(
    order_id UInt64,
    customer_id UInt32,
    amount Decimal(10, 2)
)
ENGINE = MergeTree
ORDER BY order_id;

INSERT INTO shop.orders VALUES (1, 10, 25.00), (2, 20, 13.50), (3, 10, 8.75), (4, 30, 99.99);
```

The local server, where you run the queries, has a list of customers that the remote server does not have:

```sql theme={null}
CREATE DATABASE crm;

CREATE TABLE crm.vip_customers
(
    customer_id UInt32,
    name String
)
ENGINE = MergeTree
ORDER BY customer_id;

INSERT INTO crm.vip_customers VALUES (10, 'Alice'), (30, 'Bob');
```

Filtering the remote table by the local one fails:

```sql theme={null}
SELECT order_id, customer_id, amount
FROM remoteSecure('remote.example.com:9440', shop.orders, 'default', 'password')
WHERE customer_id IN (SELECT customer_id FROM crm.vip_customers)
ORDER BY order_id;
```

```text theme={null}
Code: 81. DB::Exception: Received from remote.example.com:9440. DB::Exception: Database crm does not exist. (UNKNOWN_DATABASE)
```

`Received from` shows that the remote server raised the error: it received the subquery as part of the query and has no database `crm`. If the remote server has a database with that name but not the table, the error is `UNKNOWN_TABLE` (`Unknown table expression identifier 'crm.vip_customers'`). A regular `JOIN` with `crm.vip_customers` fails in the same way.

## Use `GLOBAL IN`

```sql theme={null}
SELECT order_id, customer_id, amount
FROM remoteSecure('remote.example.com:9440', shop.orders, 'default', 'password')
WHERE customer_id GLOBAL IN (SELECT customer_id FROM crm.vip_customers)
ORDER BY order_id;
```

```text theme={null}
   ┌─order_id─┬─customer_id─┬─amount─┐
1. │        1 │          10 │     25 │
2. │        3 │          10 │   8.75 │
3. │        4 │          30 │  99.99 │
   └──────────┴─────────────┴────────┘
```

## Use `GLOBAL JOIN`

```sql theme={null}
SELECT o.order_id, v.name, o.amount
FROM remoteSecure('remote.example.com:9440', shop.orders, 'default', 'password') AS o
GLOBAL INNER JOIN crm.vip_customers AS v ON o.customer_id = v.customer_id
ORDER BY o.order_id;
```

```text theme={null}
   ┌─order_id─┬─name──┬─amount─┐
1. │        1 │ Alice │     25 │
2. │        3 │ Alice │   8.75 │
3. │        4 │ Bob   │  99.99 │
   └──────────┴───────┴────────┘
```

Keep the local table on the right side of an `INNER` or `LEFT` join: for these join kinds the right side is the one that the local server calculates and sends.

## How `GLOBAL` works and what it costs

With `GLOBAL IN` or `GLOBAL JOIN`, the server that received your query first runs the subquery (or reads the joined table), puts the result into a temporary table in memory, and sends this temporary table to the remote server with the query. The remote server uses the transmitted data in place of the table it does not have.

The data travels over the network with every query, so keep it small:

* Select only the columns you need and filter the rows in the subquery.
* The result is not deduplicated. Add `DISTINCT` to the subquery of a `GLOBAL IN` when the column has many repeated values.
* [`max_rows_in_set`](https://clickhouse.com/docs/reference/settings/session-settings/max-rows#max_rows_in_set) and [`max_bytes_in_set`](https://clickhouse.com/docs/reference/settings/session-settings/max-bytes#max_bytes_in_set) limit how much data a `GLOBAL IN` subquery may produce.

If the table is large and you need it often, create a copy of it on the remote server and use a regular `IN` or `JOIN`.

## Related settings

* [`prefer_global_in_and_join`](https://clickhouse.com/docs/reference/settings/session-settings/prefer#prefer_global_in_and_join) set to `1` replaces `IN` and `JOIN` with `GLOBAL IN` and `GLOBAL JOIN` in such queries. Use it when you cannot change the query text.
* [`distributed_product_mode`](https://clickhouse.com/docs/reference/settings/session-settings/distributed#distributed_product_mode) does not help here. It controls subqueries that read `Distributed` tables and is not used for the `remote` table function.

The same rules apply to queries that read from a [`Distributed`](https://clickhouse.com/docs/reference/engines/table-engines/special/distributed) table: a table used in a regular `IN` subquery or `JOIN` must exist on every shard.

## Related pages

* [Distributed subqueries](https://clickhouse.com/docs/reference/statements/in.md#distributed-subqueries)
* [Distributed `JOIN`](https://clickhouse.com/docs/reference/statements/select/join#distributed-join)
* [`remote`, `remoteSecure` table functions](https://clickhouse.com/docs/reference/functions/table-functions/remote)
