Working with Insert Into Select in Postgres



When using the INSERT statement, we sometimes want to insert data from one table into another. We can use a SELECT clause after our INSERT to achieve this. In this article, we will learn how to use INSERT INTO SELECT in MySQL.

The Syntax

The basic syntax of INSERT INTO SELECT is as follows:

INSERT INTO table(column1, column2, ...)
VALUES (value1, value2, ....)

Getting Setup

We will be using docker in this article, but feel free to install your database locally instead. Once you have docker installed, create a new file called docker-compose.yml and add the following.

version: '3'
    image: 'postgres:latest'
      - 5432:5432
      POSTGRES_USER: username
      POSTGRES_PASSWORD: password
      POSTGRES_DB: default_database
      - psqldata:/var/lib/postgresql

    image: phpmyadmin/phpmyadmin
      - db
      PMA_HOST: db
      PMA_PORT: 3306
    restart: always
      - 8081:80


Next, run docker-compose up.

Now, navigate to http://localhost:8081/ to access phpMyAdmin. Then log in with the username root and pass root_pass.

Click the SQL tab and you are ready to go.

Creating a DB

In this article, we will need some data to work with. If you don't understand these commands, don't worry, we will cover them in later articles.

We will be using the sample db provided here: However, we will only enter what we need rather than import the whole db.

Next, let's create an film table. This is a slightly simplified version of the sakila database.

CREATE TABLE employees (
    emp_no      INT             NOT NULL,
    birth_date  DATE            NOT NULL,
    first_name  VARCHAR(14)     NOT NULL,
    last_name   VARCHAR(16)     NOT NULL,
    gender      VARCHAR(1),
    hire_date   DATE            NOT NULL,
    PRIMARY KEY (emp_no)
CREATE TABLE salaries (
    emp_no      INT             NOT NULL,
    salary      INT             NOT NULL,
    from_date   DATE            NOT NULL,
    to_date     DATE            NOT NULL,
    FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
    PRIMARY KEY (emp_no, from_date)

Now, let's enter a few rows

INSERT INTO employees VALUES (10001,'1953-09-02','Georgi','Facello','M','1986-06-26'),
INSERT INTO salaries VALUES (10001,60117,'1986-06-26','1987-06-26'),

An Example

Let's say we want to archive some of hour customers. We can start by creating an archive table.

CREATE TABLE employees_history LIKE employees;

Now, we can use the INSERT INTO SELECT syntax to insert data from employees.

INSERT INTO employees_history (emp_no, birth_date, first_name, last_name, gender, hire_date)
SELECT * FROM employees where hire_date < DATE('1986-01-01');

Now, let's query the history table to check the results.

SELECT * FROM employees_history;
emp_no birth_date first_name last_name gender hire_date
10002 1964-06-02 Bezalel Simmel F 1985-11-21
10009 1952-04-19 Sumant Peac F 1985-02-18
10013 1963-06-07 Eberhardt Terkki M 1985-10-20

If we want, we can now delete the data from the employees table to fully archive.

DELETE FROM employees where hire_date < DATE('1986-01-01');