How to Use Data Types in ClickHouse

Galaxy Glossary

How do data types work in ClickHouse?

Data types decide how ClickHouse stores, compresses, and computes column values, directly impacting speed and accuracy.

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

What are the core ClickHouse data type categories?

ClickHouse supplies Numeric, String, FixedString(N), Date/Time, UUID, Array, Tuple, Map, Enum, and Nullable(T) wrappers. Choosing the smallest adequate type cuts disk usage, RAM, and CPU.

How do I choose numeric types for IDs and money?

Store surrogate keys such as Customers.id in UInt64.Hold currency in Decimal(12,2) to keep cents exact and avoid floating-point rounding.

Example: Storing order totals precisely

CREATE TABLE Orders (
id UInt64,
customer_id UInt64,
order_date DateTime,
total_amount Decimal(12,2)
) ENGINE = MergeTree
ORDER BY id;

How do I store dates and times accurately?

Use Date for whole days, DateTime for second resolution, or DateTime64(3) for millisecond events like payment callbacks.

When should I enable LowCardinality?

Wrap String or Enum columns with few distinct values—e.g., Customers.country—in LowCardinality to get dictionary compression and faster group-bys.

Best practices for e-commerce schemas

  • Keep primary keys as UInt64.
  • Use Decimal for monetary fields.
  • Add Nullable only when missing values are expected.
  • Prefer Enum8/Enum16 for small, fixed value sets like order status.
  • Benchmark LowCardinality on high-selectivity columns before production use.

Common pitfalls and how to avoid them

Avoid Float64 for totals; rounding errors accumulate.Define Decimal scale and precision correctly or ClickHouse silently truncates values.

.

Why How to Use Data Types in ClickHouse is important

How to Use Data Types in ClickHouse Example Usage


SELECT
    c.name,
    round(sum(oi.quantity * p.price), 2) AS lifetime_value
FROM Customers c
JOIN Orders o      ON o.customer_id = c.id
JOIN OrderItems oi ON oi.order_id   = o.id
JOIN Products p    ON p.id          = oi.product_id
GROUP BY c.name
ORDER BY lifetime_value DESC;

How to Use Data Types in ClickHouse Syntax


-- Generic table definition with data types
CREATE TABLE table_name (
    column_name DataType [NULL | NOT NULL] [DEFAULT expr] [CODEC(codec)]
    -- add more columns here
) ENGINE = MergeTree
ORDER BY primary_key_expr
[PARTITION BY expr]
[TTL expr];

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

CREATE TABLE Products (
    id UInt64,
    name String,
    price Decimal(10,2),
    stock UInt32
) ENGINE = MergeTree
ORDER BY id;

CREATE TABLE OrderItems (
    id UInt64,
    order_id UInt64,
    product_id UInt64,
    quantity UInt16
) ENGINE = MergeTree
ORDER BY id;

Common Mistakes

Frequently Asked Questions (FAQs)

Does ClickHouse have a BOOLEAN type?

Yes. Boolean is an alias for UInt8; values are stored as 0 or 1.

Can I change a columns data type?

Use ALTER TABLE ... MODIFY COLUMN, but only to types ClickHouse can convert in place (e.g., UInt32 → UInt64). Otherwise create a new table and migrate.

When do I need Decimal128?

Decimal128 offers up to 38 digits. Use it when your amounts exceed ~1026 or you require nano-currency precision.

Want to learn about other SQL terms?

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