How to CREATE TABLE in ClickHouse

Galaxy Glossary

How do I use CREATE TABLE in ClickHouse?

CREATE TABLE defines a new table in ClickHouse with chosen columns, data types, engine, and index settings.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why use CREATE TABLE in ClickHouse?

CREATE TABLE lets you design storage for fast analytics—choosing column data types, primary key, partition keys, and an engine such as MergeTree for high-speed inserts and queries.

What is the basic CREATE TABLE syntax?

Start with the table name, list columns with types, pick an engine, then add ORDER BY and PARTITION BY clauses for optimal performance.

How do I create a simple table?

CREATE TABLE Customers
(
id UInt64,
name String,
email String,
created_at DateTime
) ENGINE = MergeTree()
ORDER BY id;

How do I add partitions for time-based pruning?

CREATE TABLE Orders
(
id UInt64,
customer_id UInt64,
order_date Date,
total_amount Decimal(10,2)
) ENGINE = MergeTree()
PARTITION BY toYYYYMM(order_date)
ORDER BY (customer_id, id);

Can I prevent errors if a table already exists?

Yes—append IF NOT EXISTS to skip creation when the table is present.

How do I set column compression or TTL?

CREATE TABLE OrderItems IF NOT EXISTS
(
id UInt64,
order_id UInt64,
product_id UInt64,
quantity UInt8
) ENGINE = MergeTree()
ORDER BY id
SETTINGS compression = 'ZSTD', ttl = toDateTime(order_id) + INTERVAL 30 DAY;

Which engine should I choose?

Use MergeTree for most workloads. ReplicatedMergeTree adds high availability, while ReplacingMergeTree deduplicates rows by a version column.

How do I list tables after creation?

SHOW TABLES FROM default;

Best practices for CREATE TABLE

  • Pick ORDER BY keys that match your most common filtering columns.
  • Partition by a low-cardinality date key for balanced shard sizes.
  • Store decimals as Decimal or Int64 cents—avoid Float for money.

What are common mistakes?

See below for troubleshooting tips and fixes.

Why How to CREATE TABLE in ClickHouse is important

How to CREATE TABLE in ClickHouse Example Usage


-- Create a product table with stock tracking
CREATE TABLE Products (
    id UInt64,
    name String,
    price Decimal(8,2),
    stock Int32
) ENGINE = MergeTree()
ORDER BY id;

How to CREATE TABLE in ClickHouse Syntax


CREATE TABLE [IF NOT EXISTS] [db.]table_name
(
    column_name1 type [DEFAULT expr] [CODEC(codec)],
    column_name2 type,
    ...
) ENGINE = engine_name()
[PARTITION BY expr]
[ORDER BY expr]
[PRIMARY KEY expr]
[SAMPLE BY expr]
[SETTINGS key = value, ...];
-- Example for ecommerce Orders
CREATE TABLE Orders (
    id UInt64,
    customer_id UInt64,
    order_date Date,
    total_amount Decimal(10,2)
) ENGINE = MergeTree()
PARTITION BY toYYYYMM(order_date)
ORDER BY (customer_id, id);

Common Mistakes

Frequently Asked Questions (FAQs)

Is IF NOT EXISTS supported in ClickHouse?

Yes. It silently skips creation when the table already exists, avoiding errors in automated scripts.

Can I change the ENGINE after creation?

No. You must create a new table with the desired engine and INSERT SELECT data into it.

How do I drop a ClickHouse table?

Run DROP TABLE [IF EXISTS] table_name [ON CLUSTER cluster]; this removes data immediately unless SYNC option is used.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.