Select Into

Construct a new table from the results of a SELECT.

Synopsis

SELECT [ ALL | DISTINCT [ ON ( distinct_expression [, ...] ) ] ]
    target_expression [ AS output_name ] [, ...]
    [ INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table ]
    [ FROM from_item [ { , | CROSS JOIN } ...] ]
    [ WHERE condition ]
    [ GROUP BY aggregate_expression [, ...] ]
    [ HAVING aggregate_condition [, ...] ]
    [ { UNION | INTERSECT | EXCEPT [ALL] } select ]
    [ ORDER BY order_expression [ ASC | DESC | USING operator ] [, ...] ]
    [ FOR UPDATE [ OF update_table [, ...] ] ]
    [ LIMIT { ALL | count } [ { OFFSET | , } start ] ]
from_item ::= { [ ONLY ] table_name [ * ]
                  [ [ AS ] from_alias [ ( column_alias_list ) ] ] |
                ( select ) [ [ AS ] alias [ ( column_alias_list ) ] ] |
                from_item [ NATURAL ] join_type from_item
                  [ ON ( join_condition ) | USING ( join_column_list ) ]
               }

join_type ::= [ INNER |
                LEFT  [ OUTER ] |
                RIGHT [ OUTER ] |
                FULL  [ OUTER ]
              ] JOIN

Parameters

Most SELECT INTO parameters are the same as for the SELECT command. The following two are the only parameters unique to SELECT INTO:

TEMPORARY, TEMP

The TEMPORARY (or TEMP) keyword indicates that the table is for temporary use; it will be destroyed when the session has ended.

new_table

The name of the new table created to hold the resulting rows of the query. This table will be created automatically and must not already exist before you execute this command.

Results

Refer to the reference entry titled “CREATE TABLE” and the reference entry titled “SELECT” for a list of possible ...

Get Practical PostgreSQL now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.