Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Inserting Multiple Rows | Populating a Database
Introduction to SQL

bookInserting Multiple Rows

Oftentimes we need to insert more than one row into a table, and re-writing the insert statements many times can be tedious and inefficient. Luckily, SQL provides us with a slightly shorter syntax which basically compresses multiple INSERT statements into a single query.

Following is the general syntax for inserting multiple rows using a single insert statement:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES 
    (value1_1, value1_2, value1_3, ...),
    (value2_1, value2_2, value2_3, ...),
    (value3_1, value3_2, value3_3, ...),
    ...;

The following example shows the syntax for inserting multiple rows into a table called students which has three columns first_name, second_name and age:

1234567
INSERT INTO students (first_name, second_name, age) VALUES ('Alice', 'Smith', 20), ('Bob', 'Johnson', 22), ('Charlie', 'Brown', 19); SELECT * FROM students;
copy

Tip:

This syntax has a shorter version as well, in which we don't specify the column names:

INSERT INTO table_name
VALUES 
    (value1_1, value1_2, value1_3, ...),
    (value2_1, value2_2, value2_3, ...),
    (value3_1, value3_2, value3_3, ...),
    …;

In this case the query will look like this:

INSERT INTO students
VALUES
    ('Alice', 'Smith', 20),
    ('Bob', 'Johnson', 22),
    ('Charlie', 'Brown', 19);

SELECT * FROM students;

Demonstration:

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 1.72

bookInserting Multiple Rows

Swipe to show menu

Oftentimes we need to insert more than one row into a table, and re-writing the insert statements many times can be tedious and inefficient. Luckily, SQL provides us with a slightly shorter syntax which basically compresses multiple INSERT statements into a single query.

Following is the general syntax for inserting multiple rows using a single insert statement:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES 
    (value1_1, value1_2, value1_3, ...),
    (value2_1, value2_2, value2_3, ...),
    (value3_1, value3_2, value3_3, ...),
    ...;

The following example shows the syntax for inserting multiple rows into a table called students which has three columns first_name, second_name and age:

1234567
INSERT INTO students (first_name, second_name, age) VALUES ('Alice', 'Smith', 20), ('Bob', 'Johnson', 22), ('Charlie', 'Brown', 19); SELECT * FROM students;
copy

Tip:

This syntax has a shorter version as well, in which we don't specify the column names:

INSERT INTO table_name
VALUES 
    (value1_1, value1_2, value1_3, ...),
    (value2_1, value2_2, value2_3, ...),
    (value3_1, value3_2, value3_3, ...),
    …;

In this case the query will look like this:

INSERT INTO students
VALUES
    ('Alice', 'Smith', 20),
    ('Bob', 'Johnson', 22),
    ('Charlie', 'Brown', 19);

SELECT * FROM students;

Demonstration:

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 6
some-alt