Defines a new table from the results of a query.
[ WITH [ RECURSIVE ] <with_query> [, ...] ]
SELECT [ALL | DISTINCT [ON ( <expression> [, ...] )]]
* | <expression> [AS <output_name>] [, ...]
INTO [TEMPORARY | TEMP | UNLOGGED ] [TABLE] <new_table>
[FROM <from_item> [, ...]]
[WHERE <condition>]
[GROUP BY <expression> [, ...]]
[HAVING <condition> [, ...]]
[{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 ]
[FOR {UPDATE | SHARE} [OF <table_name> [, ...]] [NOWAIT]
[...]]
SELECT INTO
creates a new table and fills it with data computed by a query. The data is not returned to the client, as it is with a normal SELECT
. The new table's columns have the names and data types associated with the output columns of the SELECT
.
The majority of parameters for SELECT INTO
are the same as SELECT.
Create a new table films_recent
consisting of only recent entries from the table films
:
SELECT * INTO films_recent FROM films WHERE date_prod >=
'2016-01-01';
The SQL standard uses SELECT INTO
to represent selecting values into scalar variables of a host program, rather than creating a new table. The Greenplum Database usage of SELECT INTO
to represent table creation is historical. It is best to use CREATE TABLE AS for this purpose in new applications.
Parent topic: SQL Commands