w3resource

MySQL insert into Statement Exercises: Insert rows from country_new table to countries table

MySQL insert into Statement: Exercise-6 with Solution

6. Write a MySQL query insert rows from country_new table to countries table.

Here is the rows for country_new table. Assume that, the countries table is empty.

+------------+--------------+-----------+
| COUNTRY_ID | COUNTRY_NAME | REGION_ID |
+------------+--------------+-----------+
| C0001      | India        |      1001 |
| C0002      | USA          |      1007 |
| C0003      | UK           |      1003 |
+------------+--------------+-----------+

Sample Solution:

-- Inserting records into the 'countries' table from the 'country_new' table
INSERT INTO countries
-- Selecting all records from the 'country_new' table
SELECT * FROM country_new;

Let execute the above code in MySQL command prompt.

Here is the structure of the table:

mysql> SELECT * FROM country_new;
+------------+--------------+-----------+
| 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 records into the 'countries' table by selecting all records from the existing 'country_new' table. It essentially copies the data from one table to another.

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

Previous:Write a MySQL query to insert 3 rows by a single insert statement.
Next:Write a MySQL query to insert one row in jobs table to ensure that no duplicate value will be entered in the job_id column.

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-6.php