w3resource

Insert 3 rows by a single MySQL insert statement

MySQL insert into Statement: Exercise-5 with Solution

5. Write a MySQL query to insert 3 rows by a single insert statement.

Sample Solution:

-- Inserting multiple records into the 'countries' table
INSERT INTO countries VALUES('C0001', 'India', 1001),
('C0002', 'USA', 1007), ('C0003', 'UK', 1003);

Let execute the above code in MySQL command prompt.

Here is the structure of the table:

mysql> SELECT * FROM COUNTRIES;
+------------+--------------+-----------+
| COUNTRY_ID | COUNTRY_NAME | REGION_ID |
+------------+--------------+-----------+
| C0001      | India        |      1001 |
| C0002      | USA          |      1007 |
| C0003      | UK           |      1003 |
+------------+--------------+-----------+
3 rows in set (0.00 sec)

Explanation:

The above MySQL code inserts multiple records into the 'countries' table with distinct values for 'country_id', 'country_name', and 'region_id'. Three records are inserted, representing countries with IDs 'C0001', 'C0002', and 'C0003', along with their respective names and region IDs.

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous:Write a MySQL query to insert NULL values against region_id column for a row of countries table.
Next:Write a MySQL query insert rows from country_new table to countries table.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql-exercises/insert-into-statement/insert-into-statement-exercise-5.php