3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "SELECT INTO" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 SELECT_INTO \- define a new table from the results of a query
35 [ WITH [ RECURSIVE ] \fIwith_query\fR [, \&.\&.\&.] ]
36 SELECT [ ALL | DISTINCT [ ON ( \fIexpression\fR [, \&.\&.\&.] ) ] ]
37 [ { * | \fIexpression\fR [ [ AS ] \fIoutput_name\fR ] } [, \&.\&.\&.] ]
38 INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] \fInew_table\fR
39 [ FROM \fIfrom_item\fR [, \&.\&.\&.] ]
40 [ WHERE \fIcondition\fR ]
41 [ GROUP BY \fIexpression\fR [, \&.\&.\&.] ]
42 [ HAVING \fIcondition\fR ]
43 [ WINDOW \fIwindow_name\fR AS ( \fIwindow_definition\fR ) [, \&.\&.\&.] ]
44 [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] \fIselect\fR ]
45 [ ORDER BY \fIexpression\fR [ ASC | DESC | USING \fIoperator\fR ] [ NULLS { FIRST | LAST } ] [, \&.\&.\&.] ]
46 [ LIMIT { \fIcount\fR | ALL } ]
47 [ OFFSET \fIstart\fR [ ROW | ROWS ] ]
48 [ FETCH { FIRST | NEXT } [ \fIcount\fR ] { ROW | ROWS } ONLY ]
49 [ FOR { UPDATE | SHARE } [ OF \fItable_name\fR [, \&.\&.\&.] ] [ NOWAIT ] [\&.\&.\&.] ]
54 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
55 \fBSELECT\fR\&. The new table\*(Aqs columns have the names and data types associated with the output columns of the
61 If specified, the table is created as a temporary table\&. Refer to
62 CREATE TABLE (\fBCREATE_TABLE\fR(7))
68 If specified, the table is created as an unlogged table\&. Refer to
69 CREATE TABLE (\fBCREATE_TABLE\fR(7))
75 The name (optionally schema\-qualified) of the table to be created\&.
78 All other parameters are described in detail under
83 is functionally similar to
86 is the recommended syntax, since this form of
91 PL/pgSQL, because they interpret the
93 clause differently\&. Furthermore,
95 offers a superset of the functionality provided by
99 \fBCREATE TABLE AS\fR,
101 does not allow specifying properties like a table\*(Aqs access method with
103 or the table\*(Aqs tablespace with
104 TABLESPACE \fItablespace_name\fR\&. Use
105 \fBCREATE TABLE AS\fR
106 if necessary\&. Therefore, the default table access method is chosen for the new table\&. See
107 default_table_access_method
108 for more information\&.
113 consisting of only recent entries from the table
120 SELECT * INTO films_recent FROM films WHERE date_prod >= \*(Aq2002\-01\-01\*(Aq;
127 The SQL standard uses
129 to represent selecting values into scalar variables of a host program, rather than creating a new table\&. This indeed is the usage found in
135 Chapter\ \&41)\&. The
139 to represent table creation is historical\&. Some other SQL implementations also use
141 in this way (but most SQL implementations support
142 \fBCREATE TABLE AS\fR
143 instead)\&. Apart from such compatibility considerations, it is best to use
144 \fBCREATE TABLE AS\fR
145 for this purpose in new code\&.
147 CREATE TABLE AS (\fBCREATE_TABLE_AS\fR(7))