1SELECT INTO(7) PostgreSQL 12.2 Documentation SELECT INTO(7)
2
3
4
6 SELECT_INTO - define a new table from the results of a query
7
9 [ WITH [ RECURSIVE ] with_query [, ...] ]
10 SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
11 * | expression [ [ AS ] output_name ] [, ...]
12 INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table
13 [ FROM from_item [, ...] ]
14 [ WHERE condition ]
15 [ GROUP BY expression [, ...] ]
16 [ HAVING condition [, ...] ]
17 [ WINDOW window_name AS ( window_definition ) [, ...] ]
18 [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
19 [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
20 [ LIMIT { count | ALL } ]
21 [ OFFSET start [ ROW | ROWS ] ]
22 [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
23 [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]
24
26 SELECT INTO creates a new table and fills it with data computed by a
27 query. The data is not returned to the client, as it is with a normal
28 SELECT. The new table's columns have the names and data types
29 associated with the output columns of the SELECT.
30
32 TEMPORARY or TEMP
33 If specified, the table is created as a temporary table. Refer to
34 CREATE TABLE (CREATE_TABLE(7)) for details.
35
36 UNLOGGED
37 If specified, the table is created as an unlogged table. Refer to
38 CREATE TABLE (CREATE_TABLE(7)) for details.
39
40 new_table
41 The name (optionally schema-qualified) of the table to be created.
42
43 All other parameters are described in detail under SELECT(7).
44
46 CREATE TABLE AS (CREATE_TABLE_AS(7)) is functionally similar to SELECT
47 INTO. CREATE TABLE AS is the recommended syntax, since this form of
48 SELECT INTO is not available in ECPG or PL/pgSQL, because they
49 interpret the INTO clause differently. Furthermore, CREATE TABLE AS
50 offers a superset of the functionality provided by SELECT INTO.
51
52 In contrast to CREATE TABLE AS, SELECT INTO does not allow to specify
53 properties like a table's access method with USING method or the
54 table's tablespace with TABLESPACE tablespace_name. Use CREATE TABLE AS
55 (CREATE_TABLE_AS(7)) if necessary. Therefore, the default table access
56 method is chosen for the new table. See default_table_access_method for
57 more information.
58
60 Create a new table films_recent consisting of only recent entries from
61 the table films:
62
63 SELECT * INTO films_recent FROM films WHERE date_prod >= '2002-01-01';
64
66 The SQL standard uses SELECT INTO to represent selecting values into
67 scalar variables of a host program, rather than creating a new table.
68 This indeed is the usage found in ECPG (see Chapter 35) and PL/pgSQL
69 (see Chapter 42). The PostgreSQL usage of SELECT INTO to represent
70 table creation is historical. It is best to use CREATE TABLE AS for
71 this purpose in new code.
72
74 CREATE TABLE AS (CREATE_TABLE_AS(7))
75
76
77
78PostgreSQL 12.2 2020 SELECT INTO(7)