PostgreSQL SELECT INTO (original) (raw)

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

SELECT
column_list
INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table_name
FROM
table_name
WHERE
condition;

Key Terms

Examples of PostgreSQL SELECT INTO

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 Table

In 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-Example1

PostgreSQL SELECT INTO Example1

**Explanation:

**Example 2: Creating a Temporary Table

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-Example2

PostgreSQL SELECT INTO Example2

**Explanation:

Important Points About SELECT INTO Statement in PostgreSQL

**Conclusion

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.