---
metadata:
  - name: generator
    content: Diplodoc Platform v5.56.0
alternate:
  - https://ydb.tech/docs/en/dev/streaming-query/local-and-external-topics.md?version=main
  - href: en/dev/streaming-query/local-and-external-topics.md
    type: text/markdown
    title: Markdown version
  - href: ../../llms.txt
    type: text/markdown
    title: llms.txt
sourcePath: en/core/dev/streaming-query/local-and-external-topics.md
---
> **Documentation Index:** Fetch the complete configuration index at https://ydb.tech/docs/en/llms.txt

# Local and external topics in streaming queries

[Streaming queries](https://ydb.tech/docs/en/concepts/streaming-query/streaming-query.md?version=main) read events from [topics](https://ydb.tech/docs/en/concepts/datamodel/topic.md?version=main) and can write results back to topics. The source and destination of messages can be either a topic **in the same database** where the query is executed, or a topic **in another database** YDB.

All use cases of [streaming queries](https://ydb.tech/docs/en/concepts/streaming-query/streaming-query.md?version=main) work the same for local{#local-topics} and external{#external-topics} topics. The same query can simultaneously read a local topic, write to an external one, and vice versa.

## Local topics {#local-topics}

**Local topics**: topics created in the **same database** YDB as the [streaming query](https://ydb.tech/docs/en/concepts/streaming-query/streaming-query.md?version=main).

In the query text, they are referred to **by a short name** — just like a table in the current database:


```yql
SELECT * FROM input_topic WITH (FORMAT = json_each_row, SCHEMA = (...));
```


```yql
INSERT INTO output_topic SELECT ...;
```


## External topics {#external-topics}

**External topics** — topics located **in another database** YDB.

Access to them from a streaming query is performed only through a pre-created [external data source](https://ydb.tech/docs/en/concepts/datamodel/external_data_source.md?version=main) with the YDB source type. Creating the object is the [CREATE EXTERNAL DATA SOURCE](https://ydb.tech/docs/en/yql/reference/syntax/create-external-data-source.md?version=main) command; if authentication is required, secrets are used.

After creating a source, for example named `ext_source`, referring to topic `input_topic` in the external database is written as follows:


```yql
SELECT * FROM ext_source.input_topic WITH (FORMAT = json_each_row, SCHEMA = (...));
```


The name `ext_source` in the documentation is **conditional** — in your database, the source may be named differently; it is important that it matches in `CREATE EXTERNAL DATA SOURCE` and in the prefix before the topic name.

## See also

- [Typical streaming query patterns](https://ydb.tech/docs/en/dev/streaming-query/patterns.md?version=main) — ready-made YQL snippets
- [CREATE STREAMING QUERY](https://ydb.tech/docs/en/yql/reference/syntax/create-streaming-query.md?version=main) — creating a query
- [CREATE EXTERNAL DATA SOURCE](https://ydb.tech/docs/en/yql/reference/syntax/create-external-data-source.md?version=main) — declaring a source for an external database
- [External data source](https://ydb.tech/docs/en/concepts/datamodel/external_data_source.md?version=main) — concept
- [Topic](https://ydb.tech/docs/en/concepts/datamodel/topic.md?version=main) — data model
- [Data enrichment](https://ydb.tech/docs/en/dev/streaming-query/enrichment.md?version=main) — examples with reading from a topic and `JOIN`
- [Debug reading from a topic](https://ydb.tech/docs/en/recipes/streaming_queries/debug-read.md?version=main)
