Back to Clickhouse

view

docs/en/sql-reference/table-functions/view.md

26.4.1.1-new1.4 KB
Original Source

view Table Function

Turns a subquery into a table. The function implements views (see CREATE VIEW). The resulting table does not store data, but only stores the specified SELECT query. When reading from the table, ClickHouse executes the query and deletes all unnecessary columns from the result.

Syntax {#syntax}

sql
view(subquery)

Arguments {#arguments}

  • subquerySELECT query.

Returned value {#returned_value}

  • A table.

Examples {#examples}

Input table:

text
┌─id─┬─name─────┬─days─┐
│  1 │ January  │   31 │
│  2 │ February │   29 │
│  3 │ March    │   31 │
│  4 │ April    │   30 │
└────┴──────────┴──────┘

Query:

sql
SELECT * FROM view(SELECT name FROM months);

Result:

text
┌─name─────┐
│ January  │
│ February │
│ March    │
│ April    │
└──────────┘

You can use the view function as a parameter of the remote and cluster table functions:

sql
SELECT * FROM remote(`127.0.0.1`, view(SELECT a, b, c FROM table_name));
sql
SELECT * FROM cluster(`cluster_name`, view(SELECT a, b, c FROM table_name));