We have the answers to your questions! - Don't miss our next open house about the data universe!

SQL INSERT INTO: Insert data with this SQL query

- Reading Time: 3 minutes
sql insert into

To insert data into your SQL table, you can use the SQL INSERT INTO query. But how does it work? That's what we're going to look at in this article.

What is an SQL INSERT INTO request?

SQL INSERT INTO is one of the most commonly used commands in the SQL language. And with good reason: this query allows you to integrate new records into your database. This can be one or more rows, depending on your needs.

Good to know: INSERT INTO is the command to use for all database management systems. Whether you’re using Oracle, Mysql, Transact-SQL, etc.

How to use the SQL query INSERT INTO?

INSERT INTO can be used to insert one or more rows into your table. But it’s also possible to use this command in more complex queries. In this article, we’ll take a look at some of these applications.

Insert a line

There are two ways of doing this.

1º You can insert a line by specifying all the columns. In this case, the syntax is as follows:

INSERT INTO table VALUES (‘value 1’, ‘value 2’, …)

If you choose this option, be sure to respect the order of the columns. The database management system interprets SQL queries according to the information you give it. So if you don’t need to record new values for certain columns, you’ll need to specify “NULL” (instead of value 1,2…).

The advantage is that you don’t need to write the name of each limit. This greatly reduces typing errors.

2º You can specify only those columns for which you wish to integrate new data. To do this, use the following syntax:

INSERT INTO table (column-name_1, column-name_2, …)
VALUES (‘value 1’, ‘value 2’, …)

Here, column order is no longer as important. Be careful, however, with the order of values. They must correspond to the columns to which they are assigned.

Insert several lines

Inserting several lines is often necessary to enrich your table.

To help you understand, here’s a concrete example of a Customer file to which you want to add the contact details of several customers.

Here’s the query:

INSERT INTO customer (first name, last name, city, email)
VALUES
(‘Justine’, ‘Martin’, ‘Paris’, ‘justinemartin@gmail.com’ ),
(‘Thomas’, ‘Durant’, ‘Bordeaux’, ‘tomtom@sfr.fr’),
(‘Marie’, ‘Leroy’, ‘Angers’, ‘marieleroy@laposte.net’),
(‘Vanessa’, ‘Savary’, ‘Marseille’, ‘vanessa13@gmail.com’)es

Use the INSERT INTO command to integrate the following data into your table.

First Name Last Name City Email
Justin Martin Paris justinemartin@gmail.com
Thomas Durant Bordeaux tomtom@sfr.fr
Marie Leroy Angers marieleroy@laposte.net
Vanessa Savary Marseille vanessa13@gmail.com

Copy data from another table

By combining several queries, you can also copy data from an A array to a table.

To do this, use the following syntax:

INSERT INTO my_table_1 SELECT column_1,column_2,column_2 FROM my_table_2 WHERE conditions

Let’s take an example:

You have two tables. The first corresponds to a “customer” file:

First Name Last Name City Email
Justin Martin Paris justinemartin@gmail.com
Thomas Durant Bordeaux tomtom@sfr.fr
Marie Leroy Angers marieleroy@laposte.net
Vanessa Savary Marseille vanessa13@gmail.com

And the second, to a “Prospecting” file:

ID Prénom Nom Ville Email
1 Jean Bernad Valenciennes jbernard@gmail.com
2 Thomas Castaing Nantes thomas.castaing@orange.fr
3 Antoine Dupond Paris Antwan@laposte.net
4 Virginie Pavard Lyon V.pavard@gmail.com

Having converted a prospect (Jean Bernard), you want to transfer him to your customer file.

Here’s the query:

INSERT INTO Customer SELECT (first name, last name, city, email) FROM prospect WHERE id = 1

And the result:

First Name Last Name City Email
Justin Martin Paris justinemartin@gmail.com
Thomas Durant Bordeaux tomtom@sfr.fr
Marie Leroy Angers marieleroy@laposte.net
Vanessa Savary Marseille vanessa13@gmail.com
Jean Bernard Valenciennes jbernard@gmail.com

Develop your SQL skills with DataScientest

Manipulating relational data doesn’t just mean adding new information. It’s also possible to delete, modify, update and so on. For all these actions, there are a multitude of SQL queries. If you want to know them all, data training is more than necessary. That’s why we’ve developed DataScientest. We offer a range of industry-specific training courses to help you develop your skills.

You are not available?

Leave us your e-mail, so that we can send you your new articles when they are published!
icon newsletter

DataNews

Get monthly insider insights from experts directly in your mailbox