w3resource

PostgreSQL Insert Record: Insert rows from one table to another table


6. Write a SQL statement insert rows from the country_new table to countries table.

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

 country_id | country_name | region_id
------------+--------------+-----------
 C1         | India        |      1002
 C2         | USA          |
 C3         | UK           |
 C4         | India        |      1001
 C5         | USA          |      1007
 C6         | UK           |      1003
(6 rows)

Sample Solution:

Code:

INSERT INTO countries
SELECT * FROM country_new;

Here is the command to see the list of inserting rows :

postgres=# SELECT * FROM countries;


 country_id | country_name | region_id
------------+--------------+-----------
 C1         | India        |      1002
 C2         | USA          |
 C3         | UK           |
 C4         | India        |      1001
 C5         | USA          |      1007
 C6         | UK           |      1003
(6 rows)

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

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming