Quick revision
The short answer
A STREAM is an append-only sequence of events over a topic, and a TABLE holds the latest value per key, like KStream and KTable in Kafka Streams. A persistent query, such as CREATE TABLE ... AS SELECT, runs continuously on the servers and writes to a backing topic; a push query streams changing results to a client until it disconnects; a pull query returns the current value once. The statement in the question compiles into a Kafka Streams topology running inside the ksqlDB cluster, with its own repartition and changelog topics.
What to remember
In this answer, we saw that a ksqlDB STREAM is an append-only sequence of events and a TABLE keeps the latest value per key, like KStream and KTable. Persistent queries run continuously and write to backing topics, push queries stream changes to a connected client, and pull queries return the current value once. The example CREATE TABLE AS SELECT becomes a Kafka Streams topology inside the ksqlDB servers, with a repartition topic, a state store and changelog, and the ORDERS_PER_CUSTOMER backing topic. ksqlDB's source-available licence and product direction deserve a check before standardising on it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A STREAM is an append-only sequence of events over a topic, and a TABLE holds the latest value per key, like KStream and KTable in Kafka Streams. A persistent query, such as CREATE TABLE ... AS SELECT, runs continuously on the servers and writes to a backing topic; a push query streams changing results to a client until it disconnects; a pull query returns the current value once. The statement in the question compiles into a Kafka Streams topology running inside the ksqlDB cluster, with its own repartition and changelog topics.
Why this question is asked
ksqlDB looks like a database, which leads people to expect database behaviour: a query that returns and ends, a table stored somewhere central. In fact most of what it runs is continuous stream processing, and knowing that explains its scaling, its failure modes and its costs. Interviewers ask this concept question to check that we can tell its collection types and query types apart and know what really runs behind a CREATE TABLE AS SELECT. In this answer, we will compare streams and tables, go through the three kinds of query, look under the hood of the example statement, and mention licensing.
1Streams and Tables
A ksqlDB STREAM is a view of a topic as an append-only sequence of events: each record is a new fact, such as an order was placed. Nothing is updated or deleted. A TABLE is a view in which each key has one current value: a new record for a key replaces the old one, and a record with a null value deletes it. These are the same ideas as KStream and KTable in Kafka Streams, which is no coincidence, since ksqlDB is built on Kafka Streams.
Notice that a stream declares a KEY column while a table declares a PRIMARY KEY. A table needs a key because the key is what later records replace.
2Persistent, Push and Pull Queries
ksqlDB has three kinds of query, and they differ in how long they live and where their results go.
A persistent query is created with CREATE STREAM ... AS SELECT or CREATE TABLE ... AS SELECT. It runs on the servers continuously, surviving client disconnects and server restarts, until someone terminates it, and it writes its results to a new Kafka topic. A push query is a SELECT with EMIT CHANGES: the client stays connected and receives each new or changed result as it happens, and the query ends when the client disconnects. A pull query is a SELECT without EMIT CHANGES against a materialized table, typically with a key condition: it returns the current value once and ends, like a key lookup in a database.
| Query | Lives until | Results go to |
|---|---|---|
| Persistent (CREATE ... AS SELECT) | terminated | a new Kafka topic, plus state |
| Push (SELECT ... EMIT CHANGES) | client disconnects | the client, as a stream of changes |
| Pull (SELECT ... WHERE key = ...) | result returned | the client, once |
3What Runs When We Create the Table
Now the statement from the question. CREATE TABLE orders_per_customer AS SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id EMIT CHANGES starts a persistent query. ksqlDB compiles it into a Kafka Streams topology and runs it inside the ksqlDB servers, which are themselves a Kafka Streams application cluster.
- REST API and query engineparses SQL, plans the topology
- Persistent queryKafka Streams topology, one per CREATE ... AS SELECT
- Local state storesRocksDB; serve pull queries
- Kafkainput, repartition, changelog and backing topics
4 layers, from the top down. Step through them one at a time.
Because orders is keyed by order_id, the topology first re-keys records by customer_id and writes them to an internal repartition topic, so all orders for one customer reach the same task. It counts them in a local state store, backed by a changelog topic for recovery, and writes each updated count to the backing topic, named ORDERS_PER_CUSTOMER by default. The internal topics are named from the ksql.service.id and the query id, such as CTAS_ORDERS_PER_CUSTOMER_0. Since we did not alias COUNT(*), the output column gets a generated name such as KSQL_COL_0; in practice we write COUNT(*) AS order_count.
The materialized state is also what serves pull queries on this table. And because it is a Kafka Streams application, it scales the same way: tasks are spread across the ksqlDB servers, up to the number of input partitions, and a failed server's tasks move to others and restore their state from the changelog.
4Licensing and Product Status
One practical point belongs in any recommendation. ksqlDB is source-available under the Confluent Community License rather than Apache 2.0. We may run it ourselves, but we may not offer it as a service that competes with Confluent. That is fine for most internal use, but legal should confirm it. It is also worth checking the project's recent release activity and the vendor's direction before standardising on it, since many teams now choose Flink SQL for new streaming SQL work.
5Follow-up questions
Can a pull query run against a stream?
Recent ksqlDB versions allow pull queries that scan a stream's underlying topic, but they read the whole topic and are meant for occasional inspection. Pull queries are designed for materialized tables, where a key lookup is cheap.
What happens to the persistent query if a ksqlDB server dies?
Its tasks are reassigned to the remaining servers, which restore state from the changelog topics and continue from the committed offsets, just like any Kafka Streams application.
How do you stop a persistent query?
With TERMINATE and the query id, which you can find with SHOW QUERIES. Dropping the table afterwards removes the ksqlDB object, and the DELETE TOPIC option also removes the backing topic.
6Conclusion
In this answer, we saw that a ksqlDB STREAM is an append-only sequence of events and a TABLE keeps the latest value per key, like KStream and KTable. Persistent queries run continuously and write to backing topics, push queries stream changes to a connected client, and pull queries return the current value once. The example CREATE TABLE AS SELECT becomes a Kafka Streams topology inside the ksqlDB servers, with a repartition topic, a state store and changelog, and the ORDERS_PER_CUSTOMER backing topic. ksqlDB's source-available licence and product direction deserve a check before standardising on it.
Open with
A STREAM in ksqlDB is an append-only sequence of events over a topic, every record is a new fact. A TABLE holds the latest value per key, so new records update it and a null value deletes the key, just like KStream and KTable in Kafka Streams.
Then, if they want more
For queries, a persistent query is what CREATE TABLE AS SELECT or CREATE STREAM AS SELECT starts: it runs continuously on the servers until it is terminated, writes results to a new backing topic, ORDERS_PER_CUSTOMER by default here, and keeps materialized state.
A push query is a SELECT with EMIT CHANGES that streams changing results to the client until it disconnects. A pull query is a SELECT on a materialized table, like WHERE customer_id equals c1, that returns the current value once, like a key lookup.
When I run that CREATE TABLE statement, ksqlDB compiles it into a Kafka Streams topology inside the ksqlDB server cluster. Because orders is keyed by order id, it repartitions by customer_id through an internal topic, counts in a RocksDB store with a changelog topic, and writes counts to the backing topic.
The internal topics are named from ksql.service.id and the query id, and it scales with servers and partitions like any Streams app. I would also mention that ksqlDB is under the Confluent Community License, not Apache 2.0, so licensing and project activity need checking before standardising.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "In ksqlDB, explain the difference between a STREAM and a TABLE, and between a persistent...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/dedefd0c9b/