Wondering how to insert into table from SELECT query in SQL? You came to the right place!

A vanilla way of inserting data into a table is different than insert into table from SELECT statement. Here’s why.

The Basics of SELECT in SQL

Many developers that insert data into a table use the INSERT statement available in SQL. In their basic form, INSERT statements look like this (MySQL example):

INSERT INTO `table_name` (`column`, `column2`, …) VALUES ("Demo", "Demo", ...);

The structure of this INSERT statement is as follows:

  1. First, we specify the statement itself (INSERT INTO)
  2. Once the statement is specified, we specify the table that we want to insert data into (table_name)
  3. After the table name, we specify columns that we want to insert data into (if we are inserting data into all of the columns that exist within a table, we can skip this step)
  4. Finally, with the VALUES clause we insert specific values (records, rows)

This INSERT statement will insert specified data into two or more columns.

How to Insert Into Table From SELECT Statement?

To insert into table from SELECT statement, you need to utilize the INSERT INTO SQL statement like so:

INSERT INTO `table_name` (`col1`, `col2`) SELECT `col1`, `col2` FROM `demo_table`;

The core idea of the INSERT INTO table from SELECT statement is that you specify the table name that you want to insert your data into, then specify the columns you want to work with, and then specify the SELECT SQL statement just like you would specify it in an ordinary way, just don’t forget to with the columns that you specified after the INSERT statement.

Summary

To insert data into a table from a SELECT statement, define the INSERT INTO statement, the table name, column titles, then define a SELECT statement.

FAQ

How to Insert Into Table From SELECT Statement?

To insert into table from SELECT statement, specify an INSERT INTO statement, specify the columns you want to insert the data into, then specify the SELECT statement.

Why Should I Use a Data Breach Search Engine?

Data breach search engines like BreachDirectory.com will help you see if your data has been stolen in any data breach and also help you perform a wide variety of investigative activities on email addresses, usernames, Blockchain or IP addresses, or other data.

BreachDirectory

The BreachDirectory API will let you in on the stolen data in the data breach search engine through a JSON form.

Nirium

Recent Posts

Solve the “Enter Password to Unlock 30 30 Attempts Remaining” Error

Here’s what the “Enter password to unlock 30 30 attempts remaining” message means and what…

2 weeks ago

19 Billion Compromised Passwords: Everything You Need to Know

Users on the Web have come across 19 billion compromised passwords. Here’s everything you need…

2 weeks ago

The Biggest CS2 Market Crash Explained

5 Covert skins can now be exchanged for knives or gloves using the CS2 trade…

1 month ago

How to Overcome MySQL Database Corruption and Restore Data?

This blog will walk you through ways to overcome database corruption which can be the…

1 month ago

Beginners Guide to Analyse Database Data Using SQL

This blog will walk you through ways to analyse database systems using SQL. Tune in.

2 months ago