Skip to content
ClickHouse Docs
ClickHouse DocsClickHouse Docs

Table Functions

Table functions are methods for constructing tables.

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.

Querybash
echo "1, 2, 3" > example.csv
Responsetext
./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:

Querysql
SELECT * FROM generateSeries(1,5);
Responseresponse
┌─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:

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

For example:

Querysql
CREATE TABLE series AS generateSeries(1, 5);
SELECT * FROM series;
Responseresponse
┌─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:

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