ApsaraDB for ClickHouse is fully compatible with ClickHouse SQL syntax. This page covers the key rules for writing valid queries: keywords, identifiers, literals, comments, aliases, and query parameters. For the complete upstream specification, see ClickHouse SQL syntax reference.
Key syntax rules
Keywords
ClickHouse keywords fall into two categories by case sensitivity:
Case-insensitive: SQL standard keywords and keywords that match popular databases such as MySQL or PostgreSQL. For example,
SELECT,select, andSeLeCtare equivalent, andDateTimeis the same asdatetime.Case-sensitive: All other keywords, including function names.
To check whether a data type name is case-sensitive, query the system.data_type_families table.
Keywords are not reserved, so they can appear as identifiers without quoting.
Identifiers
Identifiers are names for databases, tables, columns, and aliases. An identifier can be unquoted or quoted with backticks or double quotes.
Unquoted identifiers must start with a letter or underscore and can contain only letters, digits, and underscores.
Quoted identifiers allow any characters, including spaces and reserved words. Use double quotes ("identifier") or backticks (`identifier`) when an identifier contains special characters or conflicts with a keyword.
Literals
ClickHouse supports the following literal types:
| Literal type | Syntax examples | Notes |
|---|---|---|
| String | 'Hello', 'It\'s a test' | Single-quoted; use backslash to escape single quotes |
| Numeric | 42, 3.14, 0xFF, 1e10 | Integers, decimals, hex, and scientific notation |
| NULL | NULL | Case-insensitive |
| Heredoc | $tag$content$tag$ | No escaping needed inside a heredoc block |
String escape sequences
Inside single-quoted strings, the following escape sequences are supported:
| Escape sequence | Interpretation |
|---|---|
\' | Single quote |
\\ | Backslash |
\n | Newline |
\r | Carriage return |
\t | Tab |
\b | Backspace |
\f | Form feed |
\0 | Null character |
\xhh | Hexadecimal byte value |
Comments
ClickHouse supports two comment styles:
-- SQL-style single-line comment
/* C-style
multi-line comment */Expression aliases
Assign an alias to any expression using AS:
SELECT column_name * 2 AS double FROM some_tableThe AS keyword is optional for table aliases in FROM clauses.
Alias scope rules:
| Context | Alias availability |
|---|---|
GROUP BY, ORDER BY, HAVING | Aliases defined in SELECT are available |
WHERE | Aliases defined in the same SELECT are not available |
JOIN conditions | Aliases defined in the same query level are not available |
If an alias name conflicts with a column name, ClickHouse resolves the reference by preferring the column name inWHEREandJOINcontexts. Use distinct alias names to avoid ambiguity.
Query parameters
Define a query parameter with a {name:type} placeholder, then supply its value at query time. This lets you reuse the same query template with different inputs without rewriting or re-parsing the query.
Example template:
SELECT * FROM my_table WHERE id = {id:UInt32} AND name = {name:String}Supply parameter values in one of three ways:
SQL command:
SET param_id=42;
SET param_name='Alice';
SELECT * FROM my_table WHERE id = {id:UInt32} AND name = {name:String};clickhouse-client:
clickhouse-client \
--param_id='42' \
--param_name='Alice' \
--query "SELECT * FROM my_table WHERE id = {id:UInt32} AND name = {name:String}"HTTP interface:
curl "http://localhost:8123/?query=SELECT+*+FROM+my_table+WHERE+id+%3D+%7Bid%3AUInt32%7D+AND+name+%3D+%7Bname%3AString%7D¶m_id=42¶m_name=Alice"Full syntax reference
ApsaraDB for ClickHouse follows the upstream ClickHouse SQL syntax specification. For complete reference documentation — including all literal formats, operator precedence, and advanced syntax rules — see the ClickHouse SQL syntax reference.