SELECT_INTO(7pg13)

πŸ“¦ version 13.23, in opensuse-leap 16.0

✨ 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 πŸŽ€

  • TEMPORARY or TEMP β€” if specified, the table is created as a temporary table. refer to CREATE TABLE (CREATE_TABLE(7)) for details πŸ‘‘
  • UNLOGGED β€” if specified, the table is created as an unlogged table. refer to CREATE 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