Syntax

更新时间:
复制 MD 格式

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, and SeLeCt are equivalent, and DateTime is the same as datetime.

  • 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 typeSyntax examplesNotes
String'Hello', 'It\'s a test'Single-quoted; use backslash to escape single quotes
Numeric42, 3.14, 0xFF, 1e10Integers, decimals, hex, and scientific notation
NULLNULLCase-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 sequenceInterpretation
\'Single quote
\\Backslash
\nNewline
\rCarriage return
\tTab
\bBackspace
\fForm feed
\0Null character
\xhhHexadecimal 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_table

The AS keyword is optional for table aliases in FROM clauses.

Alias scope rules:

ContextAlias availability
GROUP BY, ORDER BY, HAVINGAliases defined in SELECT are available
WHEREAliases defined in the same SELECT are not available
JOIN conditionsAliases 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 in WHERE and JOIN contexts. 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&param_id=42&param_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.