SELECT_INTO(7)

๐Ÿ’– SELECT INTO (7) โ€” bestie edition ๐Ÿ’–

okay so this is the PostgreSQL 18.6 docs page for SELECT INTO, section 7 of the manual (iykyk), and we're gonna get you fluent in it fr fr. grab a snack, this one's short but it has LORE. ๐Ÿ“


โœจ NAME โœจ

SELECT_INTO โ€” define a new table from the results of a query

like... you run a query and the results become a whole new table?? main character energy honestly ๐Ÿ‘‘


๐ŸŒธ 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 ] [...] ]

no cap, that's the whole shape of it. it's giving "SELECT but make it a table" ๐Ÿ’…


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

SELECT INTO creates a new table and fills it with data computed by a query. the tea: the data does not get returned to the client, unlike a normal SELECT. it just... quietly becomes a table. iconic behavior honestly.

the new table's columns get the names and data types associated with the output columns of the SELECT. so whatever your query spits out, that's your table's whole personality. โœจ


๐ŸŽ€ 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. (yes bestie, same page, go read it)
  • new_table โ€” the name (optionally schema-qualified) of the table to be created. name her well ๐Ÿ’–

all other parameters are described in detail under SELECT(7). like, every single one, we're not redoing that here.


๐Ÿ‘‘ NOTES ๐Ÿ‘‘

CREATE TABLE AS is functionally similar to SELECT INTO. and CREATE TABLE AS is the recommended syntax, because this form of SELECT INTO is not available in ECPG or PL/pgSQL โ€” they interpret the INTO clause differently. ๐Ÿ˜ญ plot twist. furthermore, CREATE TABLE AS offers a superset of the functionality provided by SELECT INTO. superset!! she ate and left no crumbs.

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 if necessary. therefore, the default table access method is chosen for the new table. see default_table_access_method for more information. ๐Ÿ’€

so like. it works. it's fine. but CREATE TABLE AS is the moment. just saying. ๐Ÿ’…

๐ŸŒˆ 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';

okay look at her go. one line, whole new table, slay. ๐ŸŽฌโœจ


๐Ÿงš COMPATIBILITY ๐Ÿงš

the SQL standard uses SELECT INTO to represent selecting values into scalar variables of a host program, rather than creating a new table. and that's genuinely the usage found in ECPG (see Chapter 34) and PL/pgSQL (see Chapter 41). so if you came from those worlds you're not wrong, you're just in a different room bestie.

the PostgreSQL usage of SELECT INTO to represent table creation is historical. some other SQL implementations also use SELECT INTO in this way (but most SQL implementations support CREATE TABLE AS instead). apart from such compatibility considerations, it is best to use CREATE TABLE AS for this purpose in new code. ๐Ÿซถ

lowkey the whole compatibility section is just the docs gently telling you "please use CREATE TABLE AS" three separate times. they're being so nice about it and yet. ๐Ÿ’€

๐Ÿ’œ SEE ALSO ๐Ÿ’œ


and that's the page!! you're basically a SELECT INTO expert now, no cap. ๐Ÿ’–โœจ

okay bye, this was PostgreSQL 18.6, 2026, SELECT INTO(7) โ€” signing off, mwah ๐Ÿ’…๐ŸŒธ๐Ÿฆ‹