PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - SELECT INTO

In PostgreSQL, the SELECT INTO statement is used to create a new table and fill it with data from a SELECT query. It is essentially a shorthand form of creating a table and then inserting data into it from a select statement.

Basic Syntax:

SELECT column1, column2, ...
INTO new_table
FROM existing_table
WHERE condition;
  • column1, column2, ...: Columns that you want to select and insert into the new table.

  • new_table: The name of the new table you want to create.

  • existing_table: The name of the table from which you're pulling data.

  • WHERE condition: Optional condition to filter the rows that you want to insert into the new table.

Examples:

  1. Create a new table with all columns and rows from an existing table:

    SELECT *
    INTO employees_backup
    FROM employees;
    

    This would create a new table called employees_backup and copy all rows and columns from the employees table into it.

  2. Create a new table with specific columns:

    SELECT first_name, last_name
    INTO employee_names
    FROM employees;
    

    This would create a new table called employee_names and insert the first_name and last_name columns from the employees table.

  3. Create a new table with rows based on a condition:

    SELECT *
    INTO hr_employees
    FROM employees
    WHERE department = 'HR';
    

    This would create a new table called hr_employees and copy all rows from the employees table where the department is 'HR'.

Notes:

  • The SELECT INTO statement creates a new table and does not return any data, unlike the regular SELECT statement.

  • The new table will not inherit the constraints, indexes, defaults, or other properties of the original table. If you need those, you'll have to define them manually on the new table.

  • The SELECT INTO command in PostgreSQL is equivalent to the CREATE TABLE AS command.

  • In some other databases, SELECT INTO has a different meaning. For example, in Microsoft SQL Server, it's used to insert data into an existing table, not create a new table.

For tasks where you need a temporary copy of the data or a subset of the data in a separate table, SELECT INTO provides a quick and straightforward method. However, always ensure you have the necessary permissions to create new tables and have taken into account the storage implications of duplicating data.

  1. How to use SELECT INTO in PostgreSQL: The SELECT INTO statement in PostgreSQL is used to create a new table based on the result of a query.

    SELECT column1, column2 INTO new_table FROM your_table;
    
  2. Creating a new table with SELECT INTO in PostgreSQL:

    SELECT * INTO new_table FROM your_table;
    
  3. SELECT INTO with specific columns in PostgreSQL:

    SELECT column1, column2 INTO new_table FROM your_table;
    
  4. Specifying data types with SELECT INTO in PostgreSQL:

    SELECT column1::integer, column2::text INTO new_table FROM your_table;
    
  5. SELECT INTO and temporary tables in PostgreSQL:

    SELECT column1, column2 INTO TEMPORARY TABLE temp_table FROM your_table;
    
  6. Handling NULL values with SELECT INTO in PostgreSQL:

    SELECT column1, COALESCE(column2, 'default_value') INTO new_table FROM your_table;
    
  7. SELECT INTO and table constraints in PostgreSQL:

    SELECT column1, column2 INTO new_table FROM your_table WHERE condition;
    
  8. Copying data from one table to another with SELECT INTO in PostgreSQL:

    SELECT * INTO new_table FROM existing_table;
    
  9. Creating a backup table with SELECT INTO in PostgreSQL:

    SELECT * INTO backup_table FROM original_table;
    
  10. SELECT INTO and WHERE clause in PostgreSQL:

    SELECT column1, column2 INTO new_table FROM your_table WHERE column1 > 100;
    
  11. Using SELECT INTO with ORDER BY in PostgreSQL:

    SELECT column1, column2 INTO new_table FROM your_table ORDER BY column1;
    
  12. SELECT INTO and LIMIT/OFFSET in PostgreSQL:

    SELECT column1, column2 INTO new_table FROM your_table LIMIT 10 OFFSET 5;
    
  13. SELECT INTO and JOIN operations in PostgreSQL:

    SELECT t1.column1, t2.column2 INTO new_table
    FROM table1 t1
    INNER JOIN table2 t2 ON t1.id = t2.id;
    
  14. SELECT INTO and subqueries in PostgreSQL:

    SELECT column1, (SELECT AVG(column2) FROM another_table) INTO new_table
    FROM your_table;
    
  15. Renaming columns with SELECT INTO in PostgreSQL:

    SELECT column1 AS new_column1, column2 AS new_column2 INTO new_table FROM your_table;