SQL INSERT Multiple Rows
Syntax
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...), (value1, value2, value3, ...), (value1, value2, value3, ...), ...
Example
Suppose we have a table named "employees" with columns "id", "name", "age", and "salary". We can insert multiple rows into the table using the following SQL syntax:
INSERT INTO employees (id, name, age, salary)
VALUES (1, 'John', 25, 30000),
(2, 'Jane', 30, 40000),
(3, 'Jim', 35, 50000),
(4, 'Janet', 40, 60000);
Output
After executing the SQL query, the "employees" table will have the following rows:
id | name | age | salary |
---|---|---|---|
1 | John | 25 | 30000 |
2 | Jane | 30 | 40000 |
3 | Jim | 35 | 50000 |
4 | Janet | 40 | 60000 |
Explanation
The INSERT INTO statement adds one or more records to a database table. In the example above, we are inserting multiple rows into the "employees" table using the VALUES clause. Each row in the VALUES clause contains a set of values for the columns in the table.
Use
The SQL INSERT statement is used to insert new rows to a database table. When you need to insert multiple rows at once, you can use the syntax demonstrated above. This is more efficient than executing a separate INSERT statement for each row.
Important Points
- Column names and values must be enclosed in single quotes (') or double quotes (").
- The number of values in each row must match the number of columns in the table.
- Column values must be listed in the same order as the corresponding columns in the table.
- The SQL INSERT statement can also be used with a SELECT statement to insert data from one table into another.
Summary
In this article, we learned how to insert multiple rows into a SQL database table using the INSERT INTO statement. We also discussed the syntax and important points to keep in mind while inserting multiple rows. The ability to insert multiple rows at once can help increase efficiency when working with large datasets.