πŸ•·οΈ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 89 (from laksa118)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

πŸ“„
INDEXABLE
βœ…
CRAWLED
3 days ago
πŸ€–
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.1 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://clickhouse.com/docs/sql-reference/statements/select
Last Crawled2026-04-13 23:04:23 (3 days ago)
First Indexed2025-02-21 00:41:22 (1 year ago)
HTTP Status Code200
Meta TitleSELECT Query | ClickHouse Docs
Meta DescriptionDocumentation for SELECT Query
Meta Canonicalnull
Boilerpipe Text
SELECT queries perform data retrieval. By default, the requested data is returned to the client, while in conjunction with INSERT INTO it can be forwarded to a different table. Syntax ​ [ WITH expr_list ( subquery ) ] SELECT [ DISTINCT [ ON ( column1 , column2 , . . . ) ] ] expr_list [ FROM [ db . ] table | ( subquery ) | table_function ] [ FINAL ] [ SAMPLE sample_coeff ] [ ARRAY JOIN . . . ] [ GLOBAL ] [ ANY | ALL | ASOF ] [ INNER | LEFT | RIGHT | FULL | CROSS ] [ OUTER | SEMI | ANTI ] JOIN ( subquery ) | table [ ( alias1 [ , alias2 . . . ] ) ] ( ON < expr_list > ) | ( USING < column_list > ) [ PREWHERE expr ] [ WHERE expr ] [ GROUP BY expr_list ] [ WITH ROLLUP | WITH CUBE ] [ WITH TOTALS ] [ HAVING expr ] [ WINDOW window_expr_list ] [ QUALIFY expr ] [ ORDER BY expr_list ] [ WITH FILL ] [ FROM expr ] [ TO expr ] [ STEP expr ] [ INTERPOLATE [ ( expr_list ) ] ] [ LIMIT [ offset_value , ] n BY columns ] [ LIMIT [ n , ] m ] [ WITH TIES ] [ SETTINGS . . . ] [ UNION . . . ] [ INTO OUTFILE filename [ TRUNCATE ] [ COMPRESSION type [ LEVEL level ] ] ] [ FORMAT format ] All clauses are optional, except for the required list of expressions immediately after SELECT which is covered in more detail below . Specifics of each optional clause are covered in separate sections, which are listed in the same order as they are executed: WITH clause SELECT clause DISTINCT clause FROM clause SAMPLE clause JOIN clause PREWHERE clause WHERE clause WINDOW clause GROUP BY clause LIMIT BY clause HAVING clause QUALIFY clause LIMIT clause OFFSET clause UNION clause INTERSECT clause EXCEPT clause INTO OUTFILE clause FORMAT clause SELECT Clause ​ Expressions specified in the SELECT clause are calculated after all the operations in the clauses described above are finished. These expressions work as if they apply to separate rows in the result. If expressions in the SELECT clause contain aggregate functions, then ClickHouse processes aggregate functions and expressions used as their arguments during the GROUP BY aggregation. If you want to include all columns in the result, use the asterisk ( * ) symbol. For example, SELECT * FROM ... . Dynamic column selection ​ Dynamic column selection (also known as a COLUMNS expression) allows you to match some columns in a result with a re2 regular expression. COLUMNS ( 'regexp' ) For example, consider the table: CREATE TABLE default . col_names ( aa Int8 , ab Int8 , bc Int8 ) ENGINE = TinyLog The following query selects data from all the columns containing the a symbol in their name. SELECT COLUMNS ( 'a' ) FROM col_names β”Œβ”€aa─┬─ab─┐ β”‚ 1 β”‚ 1 β”‚ β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜ The selected columns are returned not in the alphabetical order. You can use multiple COLUMNS expressions in a query and apply functions to them. For example: SELECT COLUMNS ( 'a' ) , COLUMNS ( 'c' ) , toTypeName ( COLUMNS ( 'c' ) ) FROM col_names β”Œβ”€aa─┬─ab─┬─bc─┬─toTypeName(bc)─┐ β”‚ 1 β”‚ 1 β”‚ 1 β”‚ Int8 β”‚ β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Each column returned by the COLUMNS expression is passed to the function as a separate argument. Also you can pass other arguments to the function if it supports them. Be careful when using functions. If a function does not support the number of arguments you have passed to it, ClickHouse throws an exception. For example: SELECT COLUMNS ( 'a' ) + COLUMNS ( 'c' ) FROM col_names Received exception from server (version 19.14.1): Code: 42. DB::Exception: Received from localhost:9000. DB::Exception: Number of arguments for function plus does not match: passed 3, should be 2. In this example, COLUMNS('a') returns two columns: aa and ab . COLUMNS('c') returns the bc column. The + operator can't apply to 3 arguments, so ClickHouse throws an exception with the relevant message. Columns that matched the COLUMNS expression can have different data types. If COLUMNS does not match any columns and is the only expression in SELECT , ClickHouse throws an exception. Asterisk ​ You can put an asterisk in any part of a query instead of an expression. When the query is analyzed, the asterisk is expanded to a list of all table columns (excluding the MATERIALIZED and ALIAS columns). There are only a few cases when using an asterisk is justified: When creating a table dump. For tables containing just a few columns, such as system tables. For getting information about what columns are in a table. In this case, set LIMIT 1 . But it is better to use the DESC TABLE query. When there is strong filtration on a small number of columns using PREWHERE . In subqueries (since columns that aren't needed for the external query are excluded from subqueries). In all other cases, we do not recommend using the asterisk, since it only gives you the drawbacks of a columnar DBMS instead of the advantages. In other words using the asterisk is not recommended. Extreme Values ​ In addition to results, you can also get minimum and maximum values for the results columns. To do this, set the extremes setting to 1. Minimums and maximums are calculated for numeric types, dates, and dates with times. For other columns, the default values are output. An extra two rows are calculated – the minimums and maximums, respectively. These extra two rows are output in XML , JSON* , TabSeparated* , CSV* , Vertical , Template and Pretty* formats , separate from the other rows. They are not output for other formats. In JSON* and XML formats, the extreme values are output in a separate 'extremes' field. In TabSeparated* , CSV* and Vertical formats, the row comes after the main result, and after 'totals' if present. It is preceded by an empty row (after the other data). In Pretty* formats, the row is output as a separate table after the main result, and after totals if present. In Template format the extreme values are output according to specified template. Extreme values are calculated for rows before LIMIT , but after LIMIT BY . However, when using LIMIT offset, size , the rows before offset are included in extremes . In stream requests, the result may also include a small number of rows that passed through LIMIT . Notes ​ You can use synonyms ( AS aliases) in any part of a query. The GROUP BY , ORDER BY , and LIMIT BY clauses can support positional arguments. To enable this, switch on the enable_positional_arguments setting. Then, for example, ORDER BY 1,2 will be sorting rows in the table on the first and then the second column. Implementation Details ​ If the query omits the DISTINCT , GROUP BY and ORDER BY clauses and the IN and JOIN subqueries, the query will be completely stream processed, using O(1) amount of RAM. Otherwise, the query might consume a lot of RAM if the appropriate restrictions are not specified: max_memory_usage max_rows_to_group_by max_rows_to_sort max_rows_in_distinct max_bytes_in_distinct max_rows_in_set max_bytes_in_set max_rows_in_join max_bytes_in_join max_bytes_before_external_sort max_bytes_ratio_before_external_sort max_bytes_before_external_group_by max_bytes_ratio_before_external_group_by For more information, see the section "Settings". It is possible to use external sorting (saving temporary tables to a disk) and external aggregation. SELECT modifiers ​ You can use the following modifiers in SELECT queries. Modifier Description APPLY Allows you to invoke some function for each row returned by an outer table expression of a query. EXCEPT Specifies the names of one or more columns to exclude from the result. All matching column names are omitted from the output. REPLACE Specifies one or more expression aliases . Each alias must match a column name from the SELECT * statement. In the output column list, the column that matches the alias is replaced by the expression in that REPLACE . This modifier does not change the names or order of columns. However, it can change the value and the value type. Modifier Combinations ​ You can use each modifier separately or combine them. Examples: Using the same modifier multiple times. SELECT COLUMNS ( '[jk]' ) APPLY ( toString ) APPLY ( length ) APPLY ( max ) FROM columns_transformers ; β”Œβ”€max(length(toString(j)))─┬─max(length(toString(k)))─┐ β”‚ 2 β”‚ 3 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Using multiple modifiers in a single query. SELECT * REPLACE ( i + 1 AS i ) EXCEPT ( j ) APPLY ( sum ) from columns_transformers ; β”Œβ”€sum(plus(i, 1))─┬─sum(k)─┐ β”‚ 222 β”‚ 347 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜ SETTINGS in SELECT Query ​ You can specify the necessary settings right in the SELECT query. The setting value is applied only to this query and is reset to default or previous value after the query is executed. Other ways to make settings see here . For boolean settings set to true, you can use a shorthand syntax by omitting the value assignment. When only the setting name is specified, it is automatically set to 1 (true). Example SELECT * FROM some_table SETTINGS optimize_read_in_order = 1 , cast_keep_nullable = 1 ;
Markdown
[Skip to main content](https://clickhouse.com/docs/sql-reference/statements/select#__docusaurus_skipToContent_fallback) [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](https://clickhouse.com/) - [Products](https://clickhouse.com/docs/sql-reference/statements/select) - [ClickHouse Cloud Best way to use ClickHouse. Available on AWS, GCP, and Azure.](https://clickhouse.com/cloud) - [BYOC (Bring Your Own Cloud) The fully managed ClickHouse Cloud service, Can be deployed in your AWS account.](https://clickhouse.com/cloud/bring-your-own-cloud) - [ClickHouse Set up a database with open-source ClickHouse. ClickHouse](https://clickhouse.com/clickhouse) - [Discover more than 100 integrations.](https://clickhouse.com/integrations) [Discover more than 100 integrations.](https://clickhouse.com/integrations) - [Use cases](https://clickhouse.com/docs/sql-reference/statements/select) - [Real-time analytics](https://clickhouse.com/use-cases/real-time-analytics) - [Machine Learning & Generative AI](https://clickhouse.com/use-cases/machine-learning-and-data-science) - [Business Intelligence](https://clickhouse.com/use-cases/data-warehousing) - [Logs, Events, Traces](https://clickhouse.com/use-cases/observability) - [All use cases](https://clickhouse.com/use-cases) [All use cases](https://clickhouse.com/use-cases) - [Documentation](https://clickhouse.com/docs) - [Resources](https://clickhouse.com/docs/sql-reference/statements/select) - [User stories](https://clickhouse.com/user-stories) - [Blog](https://clickhouse.com/blog) - [Events](https://clickhouse.com/company/events) - [Learning and certification](https://clickhouse.com/learn) - [Comparison](https://clickhouse.com/docs/sql-reference/statements/select) - [BigQuery](https://clickhouse.com/comparison/bigquery) - [PostgreSQL](https://clickhouse.com/comparison/postgresql) - [Redshift](https://clickhouse.com/comparison/redshift) - [Rockset](https://clickhouse.com/comparison/rockset) - [Snowflake](https://clickhouse.com/comparison/snowflake) - [Video](https://clickhouse.com/videos) - [Demo](https://clickhouse.com/demos) - [Pricing](https://clickhouse.com/pricing) - [Contact](https://clickhouse.com/company/contact?loc=nav) [46\.9k](https://github.com/ClickHouse/ClickHouse?utm_source=clickhouse&utm_medium=website&utm_campaign=website-nav) [Search`Ctrl``K`](https://clickhouse.com/docs/search) [Sign in](https://console.clickhouse.cloud/signIn?loc=docs-nav-signIn-cta&glxid=9b44e39d-c50a-4388-8882-41a3391300c1&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect&utm_ga=GA1.1.13172778.1776121465) [Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=9b44e39d-c50a-4388-8882-41a3391300c1&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect&utm_ga=GA1.1.13172778.1776121465) [Get started](https://clickhouse.com/docs/introduction-clickhouse) [Cloud](https://clickhouse.com/docs/cloud/overview) [Manage data](https://clickhouse.com/docs/updating-data) [Server admin](https://clickhouse.com/docs/guides/manage-and-deploy-index) [Reference](https://clickhouse.com/docs/sql-reference) [Integrations](https://clickhouse.com/docs/integrations) [ClickStack](https://clickhouse.com/docs/use-cases/observability/clickstack/overview) [chDB](https://clickhouse.com/docs/chdb) [About](https://clickhouse.com/docs/about) [Knowledge Base](https://clickhouse.com/docs/knowledgebase) [English](https://clickhouse.com/docs/sql-reference/statements/select) - [English](https://clickhouse.com/docs/sql-reference/statements/select) - [ζ—₯本θͺž](https://clickhouse.com/docs/jp/sql-reference/statements/select) - [δΈ­ζ–‡](https://clickhouse.com/docs/zh/sql-reference/statements/select) - [Русский](https://clickhouse.com/docs/ru/sql-reference/statements/select) - [ν•œκ΅­μ–΄](https://clickhouse.com/docs/ko/sql-reference/statements/select) [Skip to main content](https://clickhouse.com/docs/sql-reference/statements/select#__docusaurus_skipToContent_fallback) [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](https://clickhouse.com/) - [Products](https://clickhouse.com/docs/sql-reference/statements/select) - [ClickHouse Cloud Best way to use ClickHouse. Available on AWS, GCP, and Azure.](https://clickhouse.com/cloud) - [BYOC (Bring Your Own Cloud) The fully managed ClickHouse Cloud service, Can be deployed in your AWS account.](https://clickhouse.com/cloud/bring-your-own-cloud) - [ClickHouse Set up a database with open-source ClickHouse. ClickHouse](https://clickhouse.com/clickhouse) - [Discover more than 100 integrations.](https://clickhouse.com/integrations) [Discover more than 100 integrations.](https://clickhouse.com/integrations) - [Use cases](https://clickhouse.com/docs/sql-reference/statements/select) - [Real-time analytics](https://clickhouse.com/use-cases/real-time-analytics) - [Machine Learning & Generative AI](https://clickhouse.com/use-cases/machine-learning-and-data-science) - [Business Intelligence](https://clickhouse.com/use-cases/data-warehousing) - [Logs, Events, Traces](https://clickhouse.com/use-cases/observability) - [All use cases](https://clickhouse.com/use-cases) [All use cases](https://clickhouse.com/use-cases) - [Documentation](https://clickhouse.com/docs) - [Resources](https://clickhouse.com/docs/sql-reference/statements/select) - [User stories](https://clickhouse.com/user-stories) - [Blog](https://clickhouse.com/blog) - [Events](https://clickhouse.com/company/events) - [Learning and certification](https://clickhouse.com/learn) - [Comparison](https://clickhouse.com/docs/sql-reference/statements/select) - [BigQuery](https://clickhouse.com/comparison/bigquery) - [PostgreSQL](https://clickhouse.com/comparison/postgresql) - [Redshift](https://clickhouse.com/comparison/redshift) - [Rockset](https://clickhouse.com/comparison/rockset) - [Snowflake](https://clickhouse.com/comparison/snowflake) - [Video](https://clickhouse.com/videos) - [Demo](https://clickhouse.com/demos) - [Pricing](https://clickhouse.com/pricing) - [Contact](https://clickhouse.com/company/contact?loc=nav) [46\.9k](https://github.com/ClickHouse/ClickHouse?utm_source=clickhouse&utm_medium=website&utm_campaign=website-nav) [Search`Ctrl``K`](https://clickhouse.com/docs/search) [Sign in](https://console.clickhouse.cloud/signIn?loc=docs-nav-signIn-cta&glxid=9b44e39d-c50a-4388-8882-41a3391300c1&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect&utm_ga=GA1.1.13172778.1776121465) [Get started](https://console.clickhouse.cloud/signUp?loc=docs-nav-signUp-cta&glxid=9b44e39d-c50a-4388-8882-41a3391300c1&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect&utm_ga=GA1.1.13172778.1776121465) [Get started](https://clickhouse.com/docs/introduction-clickhouse) [Cloud](https://clickhouse.com/docs/cloud/overview) [Manage data](https://clickhouse.com/docs/updating-data) [Server admin](https://clickhouse.com/docs/guides/manage-and-deploy-index) [Reference](https://clickhouse.com/docs/sql-reference) [Integrations](https://clickhouse.com/docs/integrations) [ClickStack](https://clickhouse.com/docs/use-cases/observability/clickstack/overview) [chDB](https://clickhouse.com/docs/chdb) [About](https://clickhouse.com/docs/about) [Knowledge Base](https://clickhouse.com/docs/knowledgebase) [English](https://clickhouse.com/docs/sql-reference/statements/select) - [English](https://clickhouse.com/docs/sql-reference/statements/select) - [ζ—₯本θͺž](https://clickhouse.com/docs/jp/sql-reference/statements/select) - [δΈ­ζ–‡](https://clickhouse.com/docs/zh/sql-reference/statements/select) - [Русский](https://clickhouse.com/docs/ru/sql-reference/statements/select) - [ν•œκ΅­μ–΄](https://clickhouse.com/docs/ko/sql-reference/statements/select) [Search`Ctrl``K`](https://clickhouse.com/docs/search) - [Introduction](https://clickhouse.com/docs/sql-reference) - [Syntax](https://clickhouse.com/docs/sql-reference/syntax) - [Input and Output Formats](https://clickhouse.com/docs/sql-reference/formats) - [Data types](https://clickhouse.com/docs/sql-reference/data-types) - [Statements](https://clickhouse.com/docs/sql-reference/statements) - [SELECT](https://clickhouse.com/docs/sql-reference/statements/select) - [ALL](https://clickhouse.com/docs/sql-reference/statements/select/all) - [APPLY](https://clickhouse.com/docs/sql-reference/statements/select/apply-modifier) - [ARRAY JOIN](https://clickhouse.com/docs/sql-reference/statements/select/array-join) - [DISTINCT](https://clickhouse.com/docs/sql-reference/statements/select/distinct) - [EXCEPT](https://clickhouse.com/docs/sql-reference/statements/select/except) - [EXCEPT](https://clickhouse.com/docs/sql-reference/statements/select/except-modifier) - [FORMAT](https://clickhouse.com/docs/sql-reference/statements/select/format) - [FROM](https://clickhouse.com/docs/sql-reference/statements/select/from) - [GROUP BY](https://clickhouse.com/docs/sql-reference/statements/select/group-by) - [HAVING](https://clickhouse.com/docs/sql-reference/statements/select/having) - [INTERSECT](https://clickhouse.com/docs/sql-reference/statements/select/intersect) - [INTO OUTFILE](https://clickhouse.com/docs/sql-reference/statements/select/into-outfile) - [JOIN](https://clickhouse.com/docs/sql-reference/statements/select/join) - [LIMIT BY](https://clickhouse.com/docs/sql-reference/statements/select/limit-by) - [LIMIT](https://clickhouse.com/docs/sql-reference/statements/select/limit) - [OFFSET](https://clickhouse.com/docs/sql-reference/statements/select/offset) - [ORDER BY](https://clickhouse.com/docs/sql-reference/statements/select/order-by) - [PREWHERE](https://clickhouse.com/docs/sql-reference/statements/select/prewhere) - [QUALIFY](https://clickhouse.com/docs/sql-reference/statements/select/qualify) - [REPLACE](https://clickhouse.com/docs/sql-reference/statements/select/replace-modifier) - [SAMPLE](https://clickhouse.com/docs/sql-reference/statements/select/sample) - [UNION](https://clickhouse.com/docs/sql-reference/statements/select/union) - [WHERE](https://clickhouse.com/docs/sql-reference/statements/select/where) - [WITH](https://clickhouse.com/docs/sql-reference/statements/select/with) - [INSERT INTO](https://clickhouse.com/docs/sql-reference/statements/insert-into) - [CREATE](https://clickhouse.com/docs/sql-reference/statements/create) - [ALTER](https://clickhouse.com/docs/sql-reference/statements/alter) - [DELETE](https://clickhouse.com/docs/sql-reference/statements/delete) - [SYSTEM](https://clickhouse.com/docs/sql-reference/statements/system) - [SHOW](https://clickhouse.com/docs/sql-reference/statements/show) - [GRANT](https://clickhouse.com/docs/sql-reference/statements/grant) - [EXPLAIN](https://clickhouse.com/docs/sql-reference/statements/explain) - [REVOKE](https://clickhouse.com/docs/sql-reference/statements/revoke) - [UPDATE](https://clickhouse.com/docs/sql-reference/statements/update) - [ATTACH](https://clickhouse.com/docs/sql-reference/statements/attach) - [CHECK TABLE](https://clickhouse.com/docs/sql-reference/statements/check-table) - [DESCRIBE TABLE](https://clickhouse.com/docs/sql-reference/statements/describe-table) - [DETACH](https://clickhouse.com/docs/sql-reference/statements/detach) - [DROP](https://clickhouse.com/docs/sql-reference/statements/drop) - [EXISTS](https://clickhouse.com/docs/sql-reference/statements/exists) - [KILL](https://clickhouse.com/docs/sql-reference/statements/kill) - [OPTIMIZE](https://clickhouse.com/docs/sql-reference/statements/optimize) - [RENAME](https://clickhouse.com/docs/sql-reference/statements/rename) - [EXCHANGE](https://clickhouse.com/docs/sql-reference/statements/exchange) - [SET](https://clickhouse.com/docs/sql-reference/statements/set) - [SET ROLE](https://clickhouse.com/docs/sql-reference/statements/set-role) - [TRUNCATE](https://clickhouse.com/docs/sql-reference/statements/truncate) - [EXECUTE AS](https://clickhouse.com/docs/sql-reference/statements/execute_as) - [PARALLEL WITH](https://clickhouse.com/docs/sql-reference/statements/parallel_with) - [USE](https://clickhouse.com/docs/sql-reference/statements/use) - [WATCH](https://clickhouse.com/docs/sql-reference/statements/watch) - [MOVE](https://clickhouse.com/docs/sql-reference/statements/move) - [CHECK GRANT](https://clickhouse.com/docs/sql-reference/statements/check-grant) - [UNDROP](https://clickhouse.com/docs/sql-reference/statements/undrop) - [Operators](https://clickhouse.com/docs/sql-reference/operators) - [Engines](https://clickhouse.com/docs/engines) - [Database Engines](https://clickhouse.com/docs/engines/database-engines) - [Table Engines](https://clickhouse.com/docs/engines/table-engines) - [Functions](https://clickhouse.com/docs/sql-reference/functions) - [Regular functions](https://clickhouse.com/docs/sql-reference/functions/regular-functions) - [Aggregate functions](https://clickhouse.com/docs/sql-reference/aggregate-functions) - [Table functions](https://clickhouse.com/docs/sql-reference/table-functions) - [Window functions](https://clickhouse.com/docs/sql-reference/window-functions) - [Formats](https://clickhouse.com/docs/interfaces/formats) - [Data Lakes](https://clickhouse.com/docs/sql-reference/datalakes) - [Introduction](https://clickhouse.com/docs/sql-reference) - [Statements](https://clickhouse.com/docs/sql-reference/statements) - SELECT [Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/statements/select/index.md) # SELECT Query `SELECT` queries perform data retrieval. By default, the requested data is returned to the client, while in conjunction with [INSERT INTO](https://clickhouse.com/docs/sql-reference/statements/insert-into) it can be forwarded to a different table. ## Syntax[​](https://clickhouse.com/docs/sql-reference/statements/select#syntax "Direct link to Syntax") ``` [WITH expr_list(subquery)] SELECT [DISTINCT [ON (column1, column2, ...)]] expr_list [FROM [db.]table | (subquery) | table_function] [FINAL] [SAMPLE sample_coeff] [ARRAY JOIN ...] [GLOBAL] [ANY|ALL|ASOF] [INNER|LEFT|RIGHT|FULL|CROSS] [OUTER|SEMI|ANTI] JOIN (subquery)|table [(alias1 [, alias2 ...])] (ON <expr_list>)|(USING <column_list>) [PREWHERE expr] [WHERE expr] [GROUP BY expr_list] [WITH ROLLUP|WITH CUBE] [WITH TOTALS] [HAVING expr] [WINDOW window_expr_list] [QUALIFY expr] [ORDER BY expr_list] [WITH FILL] [FROM expr] [TO expr] [STEP expr] [INTERPOLATE [(expr_list)]] [LIMIT [offset_value, ]n BY columns] [LIMIT [n, ]m] [WITH TIES] [SETTINGS ...] [UNION ...] [INTO OUTFILE filename [TRUNCATE] [COMPRESSION type [LEVEL level]] ] [FORMAT format] ``` All clauses are optional, except for the required list of expressions immediately after `SELECT` which is covered in more detail [below](https://clickhouse.com/docs/sql-reference/statements/select#select-clause). Specifics of each optional clause are covered in separate sections, which are listed in the same order as they are executed: - [WITH clause](https://clickhouse.com/docs/sql-reference/statements/select/with) - [SELECT clause](https://clickhouse.com/docs/sql-reference/statements/select#select-clause) - [DISTINCT clause](https://clickhouse.com/docs/sql-reference/statements/select/distinct) - [FROM clause](https://clickhouse.com/docs/sql-reference/statements/select/from) - [SAMPLE clause](https://clickhouse.com/docs/sql-reference/statements/select/sample) - [JOIN clause](https://clickhouse.com/docs/sql-reference/statements/select/join) - [PREWHERE clause](https://clickhouse.com/docs/sql-reference/statements/select/prewhere) - [WHERE clause](https://clickhouse.com/docs/sql-reference/statements/select/where) - [WINDOW clause](https://clickhouse.com/docs/sql-reference/window-functions) - [GROUP BY clause](https://clickhouse.com/docs/sql-reference/statements/select/group-by) - [LIMIT BY clause](https://clickhouse.com/docs/sql-reference/statements/select/limit-by) - [HAVING clause](https://clickhouse.com/docs/sql-reference/statements/select/having) - [QUALIFY clause](https://clickhouse.com/docs/sql-reference/statements/select/qualify) - [LIMIT clause](https://clickhouse.com/docs/sql-reference/statements/select/limit) - [OFFSET clause](https://clickhouse.com/docs/sql-reference/statements/select/offset) - [UNION clause](https://clickhouse.com/docs/sql-reference/statements/select/union) - [INTERSECT clause](https://clickhouse.com/docs/sql-reference/statements/select/intersect) - [EXCEPT clause](https://clickhouse.com/docs/sql-reference/statements/select/except) - [INTO OUTFILE clause](https://clickhouse.com/docs/sql-reference/statements/select/into-outfile) - [FORMAT clause](https://clickhouse.com/docs/sql-reference/statements/select/format) ## SELECT Clause[​](https://clickhouse.com/docs/sql-reference/statements/select#select-clause "Direct link to SELECT Clause") [Expressions](https://clickhouse.com/docs/sql-reference/syntax#expressions) specified in the `SELECT` clause are calculated after all the operations in the clauses described above are finished. These expressions work as if they apply to separate rows in the result. If expressions in the `SELECT` clause contain aggregate functions, then ClickHouse processes aggregate functions and expressions used as their arguments during the [GROUP BY](https://clickhouse.com/docs/sql-reference/statements/select/group-by) aggregation. If you want to include all columns in the result, use the asterisk (`*`) symbol. For example, `SELECT * FROM ...`. ### Dynamic column selection[​](https://clickhouse.com/docs/sql-reference/statements/select#dynamic-column-selection "Direct link to Dynamic column selection") Dynamic column selection (also known as a COLUMNS expression) allows you to match some columns in a result with a [re2](https://en.wikipedia.org/wiki/RE2_\(software\)) regular expression. ``` COLUMNS('regexp') ``` For example, consider the table: ``` CREATE TABLE default.col_names (aa Int8, ab Int8, bc Int8) ENGINE = TinyLog ``` The following query selects data from all the columns containing the `a` symbol in their name. ``` SELECT COLUMNS('a') FROM col_names ``` ``` β”Œβ”€aa─┬─ab─┐ β”‚ 1 β”‚ 1 β”‚ β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜ ``` The selected columns are returned not in the alphabetical order. You can use multiple `COLUMNS` expressions in a query and apply functions to them. For example: ``` SELECT COLUMNS('a'), COLUMNS('c'), toTypeName(COLUMNS('c')) FROM col_names ``` ``` β”Œβ”€aa─┬─ab─┬─bc─┬─toTypeName(bc)─┐ β”‚ 1 β”‚ 1 β”‚ 1 β”‚ Int8 β”‚ β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` Each column returned by the `COLUMNS` expression is passed to the function as a separate argument. Also you can pass other arguments to the function if it supports them. Be careful when using functions. If a function does not support the number of arguments you have passed to it, ClickHouse throws an exception. For example: ``` SELECT COLUMNS('a') + COLUMNS('c') FROM col_names ``` ``` Received exception from server (version 19.14.1): Code: 42. DB::Exception: Received from localhost:9000. DB::Exception: Number of arguments for function plus does not match: passed 3, should be 2. ``` In this example, `COLUMNS('a')` returns two columns: `aa` and `ab`. `COLUMNS('c')` returns the `bc` column. The `+` operator can't apply to 3 arguments, so ClickHouse throws an exception with the relevant message. Columns that matched the `COLUMNS` expression can have different data types. If `COLUMNS` does not match any columns and is the only expression in `SELECT`, ClickHouse throws an exception. ### Asterisk[​](https://clickhouse.com/docs/sql-reference/statements/select#asterisk "Direct link to Asterisk") You can put an asterisk in any part of a query instead of an expression. When the query is analyzed, the asterisk is expanded to a list of all table columns (excluding the `MATERIALIZED` and `ALIAS` columns). There are only a few cases when using an asterisk is justified: - When creating a table dump. - For tables containing just a few columns, such as system tables. - For getting information about what columns are in a table. In this case, set `LIMIT 1`. But it is better to use the `DESC TABLE` query. - When there is strong filtration on a small number of columns using `PREWHERE`. - In subqueries (since columns that aren't needed for the external query are excluded from subqueries). In all other cases, we do not recommend using the asterisk, since it only gives you the drawbacks of a columnar DBMS instead of the advantages. In other words using the asterisk is not recommended. ### Extreme Values[​](https://clickhouse.com/docs/sql-reference/statements/select#extreme-values "Direct link to Extreme Values") In addition to results, you can also get minimum and maximum values for the results columns. To do this, set the **extremes** setting to 1. Minimums and maximums are calculated for numeric types, dates, and dates with times. For other columns, the default values are output. An extra two rows are calculated – the minimums and maximums, respectively. These extra two rows are output in `XML`, `JSON*`, `TabSeparated*`, `CSV*`, `Vertical`, `Template` and `Pretty*` [formats](https://clickhouse.com/docs/interfaces/formats), separate from the other rows. They are not output for other formats. In `JSON*` and `XML` formats, the extreme values are output in a separate 'extremes' field. In `TabSeparated*`, `CSV*` and `Vertical` formats, the row comes after the main result, and after 'totals' if present. It is preceded by an empty row (after the other data). In `Pretty*` formats, the row is output as a separate table after the main result, and after `totals` if present. In `Template` format the extreme values are output according to specified template. Extreme values are calculated for rows before `LIMIT`, but after `LIMIT BY`. However, when using `LIMIT offset, size`, the rows before `offset` are included in `extremes`. In stream requests, the result may also include a small number of rows that passed through `LIMIT`. ### Notes[​](https://clickhouse.com/docs/sql-reference/statements/select#notes "Direct link to Notes") You can use synonyms (`AS` aliases) in any part of a query. The `GROUP BY`, `ORDER BY`, and `LIMIT BY` clauses can support positional arguments. To enable this, switch on the [enable\_positional\_arguments](https://clickhouse.com/docs/operations/settings/settings#enable_positional_arguments) setting. Then, for example, `ORDER BY 1,2` will be sorting rows in the table on the first and then the second column. ## Implementation Details[​](https://clickhouse.com/docs/sql-reference/statements/select#implementation-details "Direct link to Implementation Details") If the query omits the `DISTINCT`, `GROUP BY` and `ORDER BY` clauses and the `IN` and `JOIN` subqueries, the query will be completely stream processed, using O(1) amount of RAM. Otherwise, the query might consume a lot of RAM if the appropriate restrictions are not specified: - `max_memory_usage` - `max_rows_to_group_by` - `max_rows_to_sort` - `max_rows_in_distinct` - `max_bytes_in_distinct` - `max_rows_in_set` - `max_bytes_in_set` - `max_rows_in_join` - `max_bytes_in_join` - `max_bytes_before_external_sort` - `max_bytes_ratio_before_external_sort` - `max_bytes_before_external_group_by` - `max_bytes_ratio_before_external_group_by` For more information, see the section "Settings". It is possible to use external sorting (saving temporary tables to a disk) and external aggregation. ## SELECT modifiers[​](https://clickhouse.com/docs/sql-reference/statements/select#select-modifiers "Direct link to SELECT modifiers") You can use the following modifiers in `SELECT` queries. | Modifier | Description | |---|---| | [`APPLY`](https://clickhouse.com/docs/sql-reference/statements/select/apply-modifier) | Allows you to invoke some function for each row returned by an outer table expression of a query. | | [`EXCEPT`](https://clickhouse.com/docs/sql-reference/statements/select/except-modifier) | Specifies the names of one or more columns to exclude from the result. All matching column names are omitted from the output. | | [`REPLACE`](https://clickhouse.com/docs/sql-reference/statements/select/replace-modifier) | Specifies one or more [expression aliases](https://clickhouse.com/docs/sql-reference/syntax#expression-aliases). Each alias must match a column name from the `SELECT *` statement. In the output column list, the column that matches the alias is replaced by the expression in that `REPLACE`. This modifier does not change the names or order of columns. However, it can change the value and the value type. | ### Modifier Combinations[​](https://clickhouse.com/docs/sql-reference/statements/select#modifier-combinations "Direct link to Modifier Combinations") You can use each modifier separately or combine them. **Examples:** Using the same modifier multiple times. ``` SELECT COLUMNS('[jk]') APPLY(toString) APPLY(length) APPLY(max) FROM columns_transformers; ``` ``` β”Œβ”€max(length(toString(j)))─┬─max(length(toString(k)))─┐ β”‚ 2 β”‚ 3 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` Using multiple modifiers in a single query. ``` SELECT * REPLACE(i + 1 AS i) EXCEPT (j) APPLY(sum) from columns_transformers; ``` ``` β”Œβ”€sum(plus(i, 1))─┬─sum(k)─┐ β”‚ 222 β”‚ 347 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ## SETTINGS in SELECT Query[​](https://clickhouse.com/docs/sql-reference/statements/select#settings-in-select-query "Direct link to SETTINGS in SELECT Query") You can specify the necessary settings right in the `SELECT` query. The setting value is applied only to this query and is reset to default or previous value after the query is executed. Other ways to make settings see [here](https://clickhouse.com/docs/operations/settings/overview). For boolean settings set to true, you can use a shorthand syntax by omitting the value assignment. When only the setting name is specified, it is automatically set to `1` (true). **Example** ``` SELECT * FROM some_table SETTINGS optimize_read_in_order=1, cast_keep_nullable=1; ``` [Previous List of statements](https://clickhouse.com/docs/sql-reference/statements) [Next ALL](https://clickhouse.com/docs/sql-reference/statements/select/all) - [Syntax](https://clickhouse.com/docs/sql-reference/statements/select#syntax) - [SELECT Clause](https://clickhouse.com/docs/sql-reference/statements/select#select-clause) - [Dynamic column selection](https://clickhouse.com/docs/sql-reference/statements/select#dynamic-column-selection) - [Asterisk](https://clickhouse.com/docs/sql-reference/statements/select#asterisk) - [Extreme Values](https://clickhouse.com/docs/sql-reference/statements/select#extreme-values) - [Notes](https://clickhouse.com/docs/sql-reference/statements/select#notes) - [Implementation Details](https://clickhouse.com/docs/sql-reference/statements/select#implementation-details) - [SELECT modifiers](https://clickhouse.com/docs/sql-reference/statements/select#select-modifiers) - [Modifier Combinations](https://clickhouse.com/docs/sql-reference/statements/select#modifier-combinations) - [SETTINGS in SELECT Query](https://clickhouse.com/docs/sql-reference/statements/select#settings-in-select-query) Was this page helpful? ###### Try ClickHouse Cloud for FREE Separation of storage and compute, automatic scaling, built-in SQL console, and lots more. \$300 in free credits when signing up. [Try it for Free](https://console.clickhouse.cloud/signUp?loc=doc-card-banner&glxid=9b44e39d-c50a-4388-8882-41a3391300c1&pagePath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect&origPath=%2Fdocs%2Fsql-reference%2Fstatements%2Fselect&utm_ga=GA1.1.13172778.1776121465) Β© 2016–2026 ClickHouse, Inc. [Trademark](https://clickhouse.com/legal/trademark-policy)Β·[Privacy](https://clickhouse.com/legal/privacy-policy)Β·[Security](https://trust.clickhouse.com/)Β·[Terms of Service](https://clickhouse.com/legal/agreements/terms-of-service) ![](https://static.scarf.sh/a.png?x-pxid=e6377503-591b-4886-9398-e69c7fee0b91) Β© 2016–2026 ClickHouse, Inc. [Trademark](https://clickhouse.com/legal/trademark-policy)Β·[Privacy](https://clickhouse.com/legal/privacy-policy)Β·[Security](https://trust.clickhouse.com/)Β·[Terms of Service](https://clickhouse.com/legal/agreements/terms-of-service) ![](https://static.scarf.sh/a.png?x-pxid=e6377503-591b-4886-9398-e69c7fee0b91) [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](https://clickhouse.com/) EN - Get startedβ–Ό - Cloudβ–Ό - Manage dataβ–Ό - Server adminβ–Ό - Referenceβ–Ό - Integrationsβ–Ό - ClickStackβ–Ό - chDBβ–Ό - Aboutβ–Ό [![ClickHouse](https://clickhouse.com/docs/img/ch_logo_docs.svg)](https://clickhouse.com/) EN main-menu - Introductionβ–Ό - [Syntax](https://clickhouse.com/docs/sql-reference/syntax) - [Input and Output Formats](https://clickhouse.com/docs/sql-reference/formats) - Data typesβ–Ό - Statementsβ–Ό - Operatorsβ–Ό - Enginesβ–Ό - Functionsβ–Ό - Formatsβ–Ό - [Data Lakes](https://clickhouse.com/docs/sql-reference/datalakes)
Readable Markdown
`SELECT` queries perform data retrieval. By default, the requested data is returned to the client, while in conjunction with [INSERT INTO](https://clickhouse.com/docs/sql-reference/statements/insert-into) it can be forwarded to a different table. ## Syntax[​](https://clickhouse.com/docs/sql-reference/statements/select#syntax "Direct link to Syntax") ``` [WITH expr_list(subquery)] SELECT [DISTINCT [ON (column1, column2, ...)]] expr_list [FROM [db.]table | (subquery) | table_function] [FINAL] [SAMPLE sample_coeff] [ARRAY JOIN ...] [GLOBAL] [ANY|ALL|ASOF] [INNER|LEFT|RIGHT|FULL|CROSS] [OUTER|SEMI|ANTI] JOIN (subquery)|table [(alias1 [, alias2 ...])] (ON <expr_list>)|(USING <column_list>) [PREWHERE expr] [WHERE expr] [GROUP BY expr_list] [WITH ROLLUP|WITH CUBE] [WITH TOTALS] [HAVING expr] [WINDOW window_expr_list] [QUALIFY expr] [ORDER BY expr_list] [WITH FILL] [FROM expr] [TO expr] [STEP expr] [INTERPOLATE [(expr_list)]] [LIMIT [offset_value, ]n BY columns] [LIMIT [n, ]m] [WITH TIES] [SETTINGS ...] [UNION ...] [INTO OUTFILE filename [TRUNCATE] [COMPRESSION type [LEVEL level]] ] [FORMAT format] ``` All clauses are optional, except for the required list of expressions immediately after `SELECT` which is covered in more detail [below](https://clickhouse.com/docs/sql-reference/statements/select#select-clause). Specifics of each optional clause are covered in separate sections, which are listed in the same order as they are executed: - [WITH clause](https://clickhouse.com/docs/sql-reference/statements/select/with) - [SELECT clause](https://clickhouse.com/docs/sql-reference/statements/select#select-clause) - [DISTINCT clause](https://clickhouse.com/docs/sql-reference/statements/select/distinct) - [FROM clause](https://clickhouse.com/docs/sql-reference/statements/select/from) - [SAMPLE clause](https://clickhouse.com/docs/sql-reference/statements/select/sample) - [JOIN clause](https://clickhouse.com/docs/sql-reference/statements/select/join) - [PREWHERE clause](https://clickhouse.com/docs/sql-reference/statements/select/prewhere) - [WHERE clause](https://clickhouse.com/docs/sql-reference/statements/select/where) - [WINDOW clause](https://clickhouse.com/docs/sql-reference/window-functions) - [GROUP BY clause](https://clickhouse.com/docs/sql-reference/statements/select/group-by) - [LIMIT BY clause](https://clickhouse.com/docs/sql-reference/statements/select/limit-by) - [HAVING clause](https://clickhouse.com/docs/sql-reference/statements/select/having) - [QUALIFY clause](https://clickhouse.com/docs/sql-reference/statements/select/qualify) - [LIMIT clause](https://clickhouse.com/docs/sql-reference/statements/select/limit) - [OFFSET clause](https://clickhouse.com/docs/sql-reference/statements/select/offset) - [UNION clause](https://clickhouse.com/docs/sql-reference/statements/select/union) - [INTERSECT clause](https://clickhouse.com/docs/sql-reference/statements/select/intersect) - [EXCEPT clause](https://clickhouse.com/docs/sql-reference/statements/select/except) - [INTO OUTFILE clause](https://clickhouse.com/docs/sql-reference/statements/select/into-outfile) - [FORMAT clause](https://clickhouse.com/docs/sql-reference/statements/select/format) ## SELECT Clause[​](https://clickhouse.com/docs/sql-reference/statements/select#select-clause "Direct link to SELECT Clause") [Expressions](https://clickhouse.com/docs/sql-reference/syntax#expressions) specified in the `SELECT` clause are calculated after all the operations in the clauses described above are finished. These expressions work as if they apply to separate rows in the result. If expressions in the `SELECT` clause contain aggregate functions, then ClickHouse processes aggregate functions and expressions used as their arguments during the [GROUP BY](https://clickhouse.com/docs/sql-reference/statements/select/group-by) aggregation. If you want to include all columns in the result, use the asterisk (`*`) symbol. For example, `SELECT * FROM ...`. ### Dynamic column selection[​](https://clickhouse.com/docs/sql-reference/statements/select#dynamic-column-selection "Direct link to Dynamic column selection") Dynamic column selection (also known as a COLUMNS expression) allows you to match some columns in a result with a [re2](https://en.wikipedia.org/wiki/RE2_\(software\)) regular expression. ``` COLUMNS('regexp') ``` For example, consider the table: ``` CREATE TABLE default.col_names (aa Int8, ab Int8, bc Int8) ENGINE = TinyLog ``` The following query selects data from all the columns containing the `a` symbol in their name. ``` SELECT COLUMNS('a') FROM col_names ``` ``` β”Œβ”€aa─┬─ab─┐ β”‚ 1 β”‚ 1 β”‚ β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜ ``` The selected columns are returned not in the alphabetical order. You can use multiple `COLUMNS` expressions in a query and apply functions to them. For example: ``` SELECT COLUMNS('a'), COLUMNS('c'), toTypeName(COLUMNS('c')) FROM col_names ``` ``` β”Œβ”€aa─┬─ab─┬─bc─┬─toTypeName(bc)─┐ β”‚ 1 β”‚ 1 β”‚ 1 β”‚ Int8 β”‚ β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` Each column returned by the `COLUMNS` expression is passed to the function as a separate argument. Also you can pass other arguments to the function if it supports them. Be careful when using functions. If a function does not support the number of arguments you have passed to it, ClickHouse throws an exception. For example: ``` SELECT COLUMNS('a') + COLUMNS('c') FROM col_names ``` ``` Received exception from server (version 19.14.1): Code: 42. DB::Exception: Received from localhost:9000. DB::Exception: Number of arguments for function plus does not match: passed 3, should be 2. ``` In this example, `COLUMNS('a')` returns two columns: `aa` and `ab`. `COLUMNS('c')` returns the `bc` column. The `+` operator can't apply to 3 arguments, so ClickHouse throws an exception with the relevant message. Columns that matched the `COLUMNS` expression can have different data types. If `COLUMNS` does not match any columns and is the only expression in `SELECT`, ClickHouse throws an exception. ### Asterisk[​](https://clickhouse.com/docs/sql-reference/statements/select#asterisk "Direct link to Asterisk") You can put an asterisk in any part of a query instead of an expression. When the query is analyzed, the asterisk is expanded to a list of all table columns (excluding the `MATERIALIZED` and `ALIAS` columns). There are only a few cases when using an asterisk is justified: - When creating a table dump. - For tables containing just a few columns, such as system tables. - For getting information about what columns are in a table. In this case, set `LIMIT 1`. But it is better to use the `DESC TABLE` query. - When there is strong filtration on a small number of columns using `PREWHERE`. - In subqueries (since columns that aren't needed for the external query are excluded from subqueries). In all other cases, we do not recommend using the asterisk, since it only gives you the drawbacks of a columnar DBMS instead of the advantages. In other words using the asterisk is not recommended. ### Extreme Values[​](https://clickhouse.com/docs/sql-reference/statements/select#extreme-values "Direct link to Extreme Values") In addition to results, you can also get minimum and maximum values for the results columns. To do this, set the **extremes** setting to 1. Minimums and maximums are calculated for numeric types, dates, and dates with times. For other columns, the default values are output. An extra two rows are calculated – the minimums and maximums, respectively. These extra two rows are output in `XML`, `JSON*`, `TabSeparated*`, `CSV*`, `Vertical`, `Template` and `Pretty*` [formats](https://clickhouse.com/docs/interfaces/formats), separate from the other rows. They are not output for other formats. In `JSON*` and `XML` formats, the extreme values are output in a separate 'extremes' field. In `TabSeparated*`, `CSV*` and `Vertical` formats, the row comes after the main result, and after 'totals' if present. It is preceded by an empty row (after the other data). In `Pretty*` formats, the row is output as a separate table after the main result, and after `totals` if present. In `Template` format the extreme values are output according to specified template. Extreme values are calculated for rows before `LIMIT`, but after `LIMIT BY`. However, when using `LIMIT offset, size`, the rows before `offset` are included in `extremes`. In stream requests, the result may also include a small number of rows that passed through `LIMIT`. ### Notes[​](https://clickhouse.com/docs/sql-reference/statements/select#notes "Direct link to Notes") You can use synonyms (`AS` aliases) in any part of a query. The `GROUP BY`, `ORDER BY`, and `LIMIT BY` clauses can support positional arguments. To enable this, switch on the [enable\_positional\_arguments](https://clickhouse.com/docs/operations/settings/settings#enable_positional_arguments) setting. Then, for example, `ORDER BY 1,2` will be sorting rows in the table on the first and then the second column. ## Implementation Details[​](https://clickhouse.com/docs/sql-reference/statements/select#implementation-details "Direct link to Implementation Details") If the query omits the `DISTINCT`, `GROUP BY` and `ORDER BY` clauses and the `IN` and `JOIN` subqueries, the query will be completely stream processed, using O(1) amount of RAM. Otherwise, the query might consume a lot of RAM if the appropriate restrictions are not specified: - `max_memory_usage` - `max_rows_to_group_by` - `max_rows_to_sort` - `max_rows_in_distinct` - `max_bytes_in_distinct` - `max_rows_in_set` - `max_bytes_in_set` - `max_rows_in_join` - `max_bytes_in_join` - `max_bytes_before_external_sort` - `max_bytes_ratio_before_external_sort` - `max_bytes_before_external_group_by` - `max_bytes_ratio_before_external_group_by` For more information, see the section "Settings". It is possible to use external sorting (saving temporary tables to a disk) and external aggregation. ## SELECT modifiers[​](https://clickhouse.com/docs/sql-reference/statements/select#select-modifiers "Direct link to SELECT modifiers") You can use the following modifiers in `SELECT` queries. | Modifier | Description | |---|---| | [`APPLY`](https://clickhouse.com/docs/sql-reference/statements/select/apply-modifier) | Allows you to invoke some function for each row returned by an outer table expression of a query. | | [`EXCEPT`](https://clickhouse.com/docs/sql-reference/statements/select/except-modifier) | Specifies the names of one or more columns to exclude from the result. All matching column names are omitted from the output. | | [`REPLACE`](https://clickhouse.com/docs/sql-reference/statements/select/replace-modifier) | Specifies one or more [expression aliases](https://clickhouse.com/docs/sql-reference/syntax#expression-aliases). Each alias must match a column name from the `SELECT *` statement. In the output column list, the column that matches the alias is replaced by the expression in that `REPLACE`. This modifier does not change the names or order of columns. However, it can change the value and the value type. | ### Modifier Combinations[​](https://clickhouse.com/docs/sql-reference/statements/select#modifier-combinations "Direct link to Modifier Combinations") You can use each modifier separately or combine them. **Examples:** Using the same modifier multiple times. ``` SELECT COLUMNS('[jk]') APPLY(toString) APPLY(length) APPLY(max) FROM columns_transformers; ``` ``` β”Œβ”€max(length(toString(j)))─┬─max(length(toString(k)))─┐ β”‚ 2 β”‚ 3 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` Using multiple modifiers in a single query. ``` SELECT * REPLACE(i + 1 AS i) EXCEPT (j) APPLY(sum) from columns_transformers; ``` ``` β”Œβ”€sum(plus(i, 1))─┬─sum(k)─┐ β”‚ 222 β”‚ 347 β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ## SETTINGS in SELECT Query[​](https://clickhouse.com/docs/sql-reference/statements/select#settings-in-select-query "Direct link to SETTINGS in SELECT Query") You can specify the necessary settings right in the `SELECT` query. The setting value is applied only to this query and is reset to default or previous value after the query is executed. Other ways to make settings see [here](https://clickhouse.com/docs/operations/settings/overview). For boolean settings set to true, you can use a shorthand syntax by omitting the value assignment. When only the setting name is specified, it is automatically set to `1` (true). **Example** ``` SELECT * FROM some_table SETTINGS optimize_read_in_order=1, cast_keep_nullable=1; ```
Shard89 (laksa)
Root Hash12633450985039531489
Unparsed URLcom,clickhouse!/docs/sql-reference/statements/select s443