Back to Clickhouse

Table Functions

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

26.4.1.1-new2.0 KB
Original Source

Table Functions

Table functions are methods for constructing tables.

<!--AUTOGENERATED_START--> <!--AUTOGENERATED_END-->

Usage {#usage}

Table functions can be used in the FROM clause of a SELECT query. For example, you can SELECT data from a file on your local machine using the file table function.

bash
echo "1, 2, 3" > example.csv
text
./clickhouse client
:) SELECT * FROM file('example.csv')
┌─c1─┬─c2─┬─c3─┐
│  1 │  2 │  3 │
└────┴────┴────┘

You can also use table functions for creating a temporary table that is available only in the current query. For example:

sql
SELECT * FROM generateSeries(1,5);
response
┌─generate_series─┐
│               1 │
│               2 │
│               3 │
│               4 │
│               5 │
└─────────────────┘

The table is deleted when the query finishes.

Table functions can be used as a way to create tables, using the following syntax:

sql
CREATE TABLE [IF NOT EXISTS] [db.]table_name AS table_function()

For example:

sql
CREATE TABLE series AS generateSeries(1, 5);
SELECT * FROM series;
response
┌─generate_series─┐
│               1 │
│               2 │
│               3 │
│               4 │
│               5 │
└─────────────────┘

Finally, table functions can be used to INSERT data into a table. For example, we could write out the contents of the table we created in the previous example to a file on disk using the file table function again:

sql
INSERT INTO FUNCTION file('numbers.csv', 'CSV') SELECT * FROM series;
bash
cat numbers.csv
1
2
3
4
5

:::note You can't use table functions if the allow_ddl setting is disabled. :::