1SELECT INTO(7) PostgreSQL 16.1 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 is functionally similar to SELECT INTO. CREATE TABLE
47 AS is the recommended syntax, since this form of SELECT INTO is not
48 available in ECPG or PL/pgSQL, because they interpret the INTO clause
49 differently. Furthermore, CREATE TABLE AS offers a superset of the
50 functionality provided by SELECT INTO.
51
52 In contrast to CREATE TABLE AS, SELECT INTO does not allow specifying
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 if necessary. Therefore, the default table access method is chosen for
56 the new table. See default_table_access_method for more information.
57
59 Create a new table films_recent consisting of only recent entries from
60 the table films:
61
62 SELECT * INTO films_recent FROM films WHERE date_prod >= '2002-01-01';
63
65 The SQL standard uses SELECT INTO to represent selecting values into
66 scalar variables of a host program, rather than creating a new table.
67 This indeed is the usage found in ECPG (see Chapter 36) and PL/pgSQL
68 (see Chapter 43). The PostgreSQL usage of SELECT INTO to represent
69 table creation is historical. Some other SQL implementations also use
70 SELECT INTO in this way (but most SQL implementations support CREATE
71 TABLE AS instead). Apart from such compatibility considerations, it is
72 best to use CREATE TABLE AS for this purpose in new code.
73
75 CREATE TABLE AS (CREATE_TABLE_AS(7))
76
77
78
79PostgreSQL 16.1 2023 SELECT INTO(7)