SELECT INTO
Creates a table that uses the data computed from a query as its contents.
Syntax
[ WITH [ RECURSIVE ] <with_query> [, ...] ]
SELECT [ALL | DISTINCT [ON ( <expression> [, ...] )]]
* | <expression> [AS <output_name>] [, ...]
INTO [TABLE] <new_table>
[FROM <from_item> [, ...]]
[WHERE <condition>]
[GROUP BY <expression> [, ...]]
[HAVING <condition> [, ...]]
[WINDOW <window_name> AS ( <window_definition> ) [, ...]]
[{UNION | INTERSECT | EXCEPT} [ALL | DISTINCT ] <select>]
[ORDER BY <expression> [ASC | DESC | USING <operator>] [NULLS {FIRST | LAST}] [, ...]]
[LIMIT {<count> | ALL}]
[OFFSET <start> [ ROW | ROWS ] ]
[FETCH { FIRST | NEXT } [ <count> ] { ROW | ROWS } ONLY ]
Description
When you use SELECT INTO
to create a table, the data filled in the table is the result of a query. For this reason, the name and data type of each column in the table are the same as those of the counterpart column in the query result. However, Relyt does not recommend that you use SELECT INTO
to create tables.
The result of the query is not result to the client.
Parameters
<new_table>
: the name of the table to create. You can prefix the name with a schema name.
For information about other parameters, see section "Parameters" in SELECT.
Examples
Create a table named sales_2022
that contain the rows whose value in the year
column is 2022
from table sales
:
SELECT *
INTO sales_2022
FROM sales
WHERE year = 2022;
SQL standard compatibility
SELECT INTO
in Relyt creates a new table, while SELECT INTO
in the SQL standard selects values into scalar variables.