Below is the normal use case for "INSERT INTO" - where you insert values manually. It is followed by the table name and an optional list of columns that should be filled with the data. Then follows the "VALUES" keyword and the actual values you want to insert.
In order to copy data from an existing table to a new one, you can use the "INSERT INTO SELECT DISTINCT" pattern. After "INSERT INTO", you specify the target table's name - organizations in the below case.
Then you select the columns that should be copied over from the source table – unviversity_professors in this case. You use the "DISTINCT" keyword to only copy over distinct organizations.
INSERTINTO organizations
SELECTDISTINCT organization,
organization_sector
FROM university_professors;
This selects all distinct values in table table_a – nothing new for you.
-- Insert unique professors into the new tableINSERTINTO professors
SELECTDISTINCT firstname, lastname, university_shortname
FROM university_professors;-- Doublecheck the contents of professorsSELECT*FROM professors;
Take this part and append it to the first, so it inserts all distinct rows from table_a into table_b.
One last thing: It is important that you run all of the code at the same time once you have filled out the blanks.
-- Insert unique affiliations into the new tableINSERTINTO affiliations
SELECTDISTINCT firstname, lastname,function, organization
FROM university_professors;-- Doublecheck the contents of affiliationsSELECT*FROM affiliations;
If you are looking to get started with SQL, we’ve got you covered. In this SQL tutorial, we will introduce you to SQL queries - a powerful tool that enables us to work with the data stored in a database.
Syntax
Below is the normal use case for "INSERT INTO" - where you insert values manually. It is followed by the table name and an optional list of columns that should be filled with the data. Then follows the "VALUES" keyword and the actual values you want to insert.
INSERT DISTINCT Records INTO New Tables
In order to copy data from an existing table to a new one, you can use the "INSERT INTO SELECT DISTINCT" pattern. After "INSERT INTO", you specify the target table's name -
organizationsin the below case.Then you select the columns that should be copied over from the source table –
unviversity_professorsin this case. You use the "DISTINCT" keyword to only copy over distinct organizations.The above output shows that only 1287 records are inserted into the "organizations" table.
On the other hand, if you use "INSERT INTO SELECT", without the "DISTINCT" keyword, duplicate records would be copied over as well.
Migrating Data Into a New Table
Let's migrate the data into new tables. You'll use the following pattern:
It can be broken up into two parts:
First part:
This selects all distinct values in table
table_a– nothing new for you.Second part:
Take this part and append it to the first, so it inserts all distinct rows from
table_aintotable_b.One last thing: It is important that you run all of the code at the same time once you have filled out the blanks.
Try it for yourself.
To learn more about relational databases in SQL, please see this video from our course Introduction to Relational Databases in SQL.
This content is taken from DataCamp’s Introduction to Relational Databases in SQL course by Timo Grossenbacher.