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

# Federated Queries

A *federated query* reads the tables in an external
[catalog](https://docs.kinetica.com/content/sql/ddl/create-catalog.md#sql-create-catalog) by name, without first creating
[external tables](https://docs.kinetica.com/content/sql/ddl/create-external-table.md#sql-create-ext-table) over them.  Tables in a
*catalog* can be queried alone, joined with tables in the database, or joined
with tables in other *catalogs*.

```sql title="Federated Query Table Names" theme={null}
<JDBC catalog>.[<database>.]<schema>.<table>
<Iceberg catalog>.<namespace>[.<namespace>...].<table>
<Delta Lake catalog>.[<Unity Catalog catalog>.]<schema>.<table>
```

The table name rules differ by type of *catalog*:

* **JDBC** - Both `<catalog>.<database>.<schema>.<table>` &
  `<catalog>.<schema>.<table>` are accepted.  For a source with no schemas,
  such as *MySQL*, the name is `<catalog>.<database>.<table>`.
* **Apache Iceberg** - The namespace may have any number of levels.  A quoted
  part containing dots is several levels, so `lake."sales.eu".orders` is the
  same as `lake.sales.eu.orders`.
* **Delta Lake** - The name includes the *Unity Catalog* catalog, unless the
  *catalog* was created with a `DATABASE` option naming it.

The parts of the name after the *catalog* are sent to the external source as
written:  a quoted part stays quoted, and an unquoted part is left for the
source to case-convert.  While a *catalog* is the session's
[default catalog](https://docs.kinetica.com/content/sql/ddl/set-catalog.md#sql-set-catalog), the *catalog* can be left out
of the name.

> [!NOTE]
>
> Querying a *catalog's* tables requires `READ` access on the
> *catalog* and `CONNECT` access on its *data source*, and, for a data lake
> *catalog*, `READ` access on its *credential*; see
> [GRANT Catalog Permission](https://docs.kinetica.com/content/sql/security/grant-catalog-permission.md#sql-security-priv-mgmt-catalog-grant).

## Examples

To query a table in a JDBC *catalog*:

```sql Federated Query Example theme={null}
SELECT order_id, customer_id, amount, status
FROM jdbc_cat.example_fed.orders
ORDER BY order_id
```

To name the source's database in the table name:

```sql Federated Query with Database Example theme={null}
SELECT COUNT(*) AS order_count
FROM jdbc_cat.KINETICA.example_fed.orders
```

To filter & aggregate a table in a JDBC *catalog*:

```sql Federated Aggregation Example theme={null}
SELECT status, COUNT(*) AS order_count, SUM(amount) AS total_amount
FROM jdbc_cat.example_fed.orders
WHERE amount > 20
GROUP BY status
ORDER BY status
```

To join a table in a JDBC *catalog* with a table in the database:

```sql Federated/Local JOIN Example theme={null}
SELECT c.name, c.city, SUM(o.amount) AS total_amount
FROM jdbc_cat.example_fed.orders o
JOIN example.fed_customer c ON c.customer_id = o.customer_id
GROUP BY c.name, c.city
ORDER BY c.name
```

```Federated/Local JOIN Output theme={null}
+--------+-----------+----------------+
| name   | city      |   total_amount |
+--------+-----------+----------------+
| Ann    | Austin    |         195.25 |
| Bob    | Boston    |          64.49 |
| Cy     | Chicago   |         310.00 |
+--------+-----------+----------------+
```

To copy a table in a *catalog* into a new table in the database:

```sql Federated CREATE TABLE ... AS Example theme={null}
CREATE TABLE example.fed_orders_copy AS
SELECT *
FROM jdbc_cat.example_fed.orders
```

To insert records from a table in a *catalog* into an existing table in the
database:

```sql Federated INSERT INTO ... SELECT Example theme={null}
INSERT INTO example.fed_open_orders
SELECT order_id, amount
FROM jdbc_cat.example_fed.orders
WHERE status = 'open'
```

To create a [logical view](https://docs.kinetica.com/content/sql/ddl/create-view.md#sql-create-view) over a table in a
*catalog*:

```sql Federated View Example theme={null}
CREATE VIEW example.fed_shipped_orders AS
SELECT order_id, amount
FROM jdbc_cat.example_fed.orders
WHERE status = 'shipped'
```

## Performance

* All of the tables a statement reads from a *catalog* are described by its
  external source in one call per *catalog*, while the statement is planned.
* Each table is loaded once per statement, even when it is referenced more than
  once, as in a self-join.
* Only the columns a statement reads are loaded, and filters are passed to the
  external source; for a data lake table, filters are used to skip data files.
* A part of a statement that reads only the tables of one JDBC *data source* is
  sent to that source as a single query, so its joins, aggregates, and limits
  are performed by the source.

## Limitations

* Tables in a *catalog* are read-only; `INSERT`, `UPDATE`, `DELETE` &
  `MERGE` statements that write to them are rejected:

  ```sql Federated UPDATE Example theme={null}
  UPDATE jdbc_cat.example_fed.orders
  SET status = 'shipped'
  WHERE order_id = 2
  ```

  ```Federated UPDATE Output theme={null}
  [GPUdb]executeSql: SqlEngine: Writing to federated table 'jdbc_cat.example_fed.orders' is not supported (<REF>)
  ```

* A [materialized view](https://docs.kinetica.com/content/sql/ddl/create-materialized-view.md#sql-create-materialized-view) over a table
  in a *catalog* is not supported; use `CREATE TABLE ... AS` to copy the table
  into the database instead:

  ```sql Federated Materialized View Example theme={null}
  CREATE MATERIALIZED VIEW example.fed_orders_mv AS
  SELECT *
  FROM jdbc_cat.example_fed.orders
  ```

  ```Federated Materialized View Output theme={null}
  [GPUdb]executeSql: SqlEngine: Materialized views over federated tables are not supported; use CREATE TABLE ... AS SELECT to copy them (<REF>)
  ```

* The query plans of statements that read tables in a *catalog* are not cached,
  and *Apache Iceberg* time travel is not supported.

* The `CURRENT_CATALOG` function always returns `KINETICA`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.