Last Updated : 15 Jul, 2025
The PostgreSQL SELECT INTO statement allows users to create a new table directly from the result set of a query. This command is ideal for duplicating or organizing data from an existing table into a new one for further analysis. SELECT INTO does not return data to the client but saves it in a new table, allowing for streamlined data handling and improved query performance.
In this article, we will cover the SELECT INTO syntax, explore its key parameters, and walk through examples demonstrating its usage in PostgreSQL.
What is PostgreSQL SELECT INTO?In PostgreSQL, the SELECT INTO statement creates a new table and inserts data returned by a query into it. This newly created table inherits the column names and data types from the output of the query, which makes it easy to quickly organize or duplicate data for specific purposes.
Syntax
SELECTKey Terms
column_list
INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table_name
FROM
table_name
WHERE
condition;
TEMPORARY
, TEMP
, or UNLOGGED
to define the table type.INNER JOIN
, LEFT JOIN
, GROUP BY
, and HAVING
to refine the data selection.Now let’s look into some examples of SELECT INTO Statement in PostgreSQL to better understand the concept. For examples we will be using the sample database (ie, dvdrental).
Example 1: Creating a Permanent TableIn this example, we will use the 'film'
table from the 'dvdrental'
database to create a new table named 'film_r'
. This table will contain all films with a rating of 'R' and a rental duration of 5 days.
Query:
SELECT
film_id,
title,
rental_rate
INTO TABLE film_r
FROM
film
WHERE
rating = 'R'
AND rental_duration = 5
ORDER BY
title;
Now we can verify the created table using the below statement:
SELECT
*
FROM
film_r;
Output
PostgreSQL SELECT INTO Example1Explanation:
film_id
, title
, and rental_rate
columns from the film
table where the rating
is '
R
'
and the rental_duration
is 5 days.film_r
.film_r'
table, displaying the films that match the specified criteria.In this example, we create a temporary table named 'short_film'
that contains all films with a length of under 60 minutes.
Query:
SELECT
film_id,
title,
length
INTO TEMP TABLE short_film
FROM
film
WHERE
length < 60
ORDER BY
title;
Now we verify the table short_film using the below statement:
SELECT
*
FROM
short_film;
Output
PostgreSQL SELECT INTO Example2Explanation:
short_film'
table, showing the short films as specified.short_film
, which will only exist for the duration of the session.length
is less than 60 minutes.TEMPORARY
or TEMP
keyword creates a table that is automatically dropped at the end of the session.UNLOGGED
keyword creates a table that is not written to the write-ahead log (WAL). This makes inserts faster but at the cost of durability.SELECT INTO
statement does not copy constraints (e.g., primary keys, foreign keys, or UNIQUE constraints) from the source table to the new table. We need to add them manually if required.The PostgreSQL SELECT INTO statement is an efficient tool for creating a new table based on the results of a query, ideal for organizing and manipulating data quickly. For more strong requirements, the CREATE TABLE AS statement provides enhanced functionality and flexibility. Understanding the syntax of SELECT INTO and its options such as temporary tables and unlogged tables allows PostgreSQL users to optimize data workflows effectively.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4