Introduction
Syntax
Learn the key elements of ClickHouse SQL syntax.
ClickHouse SQL syntax follows standard SQL conventions with some unique features. This guide covers the essential elements of ClickHouse SQL syntax.
Key syntax elements
Spaces and comments
- Spaces between syntactical constructions are optional but recommended for readability.
- SQL-style comments: Start with
--
,#!
, or#
and continue to the end of the line. - C-style comments: Enclosed between
/*
and*/
, can be multiline.
Keywords and identifiers
- Keywords are case-insensitive for SQL standard terms (e.g.,
SELECT
,select
,SeLeCt
). - Other keywords, including function names, are case-sensitive.
- Identifiers (e.g., table names, column names) can be quoted or non-quoted.
- Non-quoted: Must match
^[a-zA-Z_][0-9a-zA-Z_]*$
- Quoted: Use double quotes or backticks (e.g.,
"id"
,`id`
)
- Non-quoted: Must match
Literals
- Numeric: Integers, decimals, exponential notation, hex, and binary (e.g.,
1
,0.1
,1e100
,0xc0fe
,0b1101
) - String: Enclosed in single quotes (e.g.,
'Hello, world'
) - Array: Square brackets (e.g.,
[1, 2, 3]
) - Tuple: Round brackets (e.g.,
(1, 'Hello', 2)
) - NULL: Represents a missing value
Functions and operators
- Function calls:
function_name(arg1, arg2, ...)
- Operators are converted to their corresponding functions during parsing
Expressions and aliases
- Expressions can include functions, identifiers, literals, operators, and subqueries.
- Aliases:
expression AS alias_name
Examples
Basic SELECT query
Using functions and operators
Complex expression with alias
Frequently asked questions
Was this page helpful?