SELECT_INTO(7pg13)
β¨ NAME β¨
okay bestie, welcome to SELECT_INTO(7) from the PostgreSQL 13.23 Documentation β this is the page for SELECT_INTO, and itβs literally about defining a new table from the results of a query. main character energy, no cap π
π¦ SYNOPSIS π¦
[ 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 ]
[ WINDOW window_name AS ( window_definition ) [, ...] ]
[ { 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 ] [...] ]
πΈ DESCRIPTION πΈ
so SELECT INTO creates a new table and fills it with data computed by a query. the data is not returned to the client, like it is with a normal SELECT β it goes straight into the new table instead. the new tableβs columns have the names and data types associated with the output columns of the SELECT. slay π
π PARAMETERS π
TEMPORARYorTEMPβ if specified, the table is created as a temporary table. refer toCREATE TABLE(CREATE_TABLE(7)) for details πUNLOGGEDβ if specified, the table is created as an unlogged table. refer toCREATE TABLE(CREATE_TABLE(7)) for details β¨new_tableβ the name (optionally schema-qualified) of the table to be created π«Ά
all other parameters are described in detail under SELECT(7). iykyk π
π₯ NOTES π₯
CREATE TABLE AS (CREATE_TABLE_AS(7)) is functionally similar to SELECT INTO. but bestie, CREATE TABLE AS is the recommended syntax, since this form of SELECT INTO is not available in ECPG or PL/pgSQL, because they interpret the INTO clause differently. furthermore, CREATE TABLE AS offers a superset of the functionality provided by SELECT INTO. itβs giving βuse the upgraded version,β fr fr π
in contrast to CREATE TABLE AS, SELECT INTO does not allow specifying properties like a tableβs access method with USING method or the tableβs tablespace with TABLESPACE tablespace_name. use CREATE TABLE AS (CREATE_TABLE_AS(7)) if necessary. therefore, the default table access method is chosen for the new table. see default_table_access_method for more information π
lowkey important: those missing properties are a real caveat, not a vibe. donβt forget them π
π EXAMPLES π
create a new table films_recent consisting of only recent entries from the table films:
SELECT * INTO films_recent FROM films WHERE date_prod >= '2002-01-01';
π COMPATIBILITY π
the SQL standard uses SELECT INTO to represent selecting values into scalar variables of a host program, rather than creating a new table. this indeed is the usage found in ECPG (see Chapter 35) and PL/pgSQL (see Chapter 42). the PostgreSQL usage of SELECT INTO to represent table creation is historical. it is best to use CREATE TABLE AS for this purpose in new code. periodt π
π«Ά SEE ALSO π«Ά
CREATE TABLE AS(CREATE_TABLE_AS(7)) β thatβs the recommended one, bestie π
made with π from PostgreSQL 13.23, dated 2025 β and thatβs the whole vibe. slay, bye β¨π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms