w3resource

PostgreSQL Insert Records - Exercises, Practice, Solution

PostgreSQL Insert Rows into the Table [13 exercises with solution]

1. Write a SQL statement to insert a record with your own value into the table countries against each column.

Here in the following is the structure of the table countries.

    Column    |         Type          | Modifiers
--------------+-----------------------+-----------
 country_id   | character varying(2)  |
 country_name | character varying(40) |
 region_id    | numeric(10,0)         |

Click me to see the solution

2. Write a SQL statement to insert one row into the table countries against the column country_id and country_name.

Here in the following is the structure of the table countries.

    Column    |         Type          | Modifiers
--------------+-----------------------+-----------
 country_id   | character varying(2)  |
 country_name | character varying(40) |
 region_id    | numeric(10,0)         |

Click me to see the solution

3. Write a SQL statement to create duplicates of countries table named country_new with all structure and data.

Here in the following is the structure of the table countries.

    Column    |         Type          | Modifiers
--------------+-----------------------+-----------
 country_id   | character varying(2)  |
 country_name | character varying(40) |
 region_id    | numeric(10,0)         |

Click me to see the solution

4. Write a SQL statement to insert NULL values into region_id column for a row of countries table.

Click me to see the solution

5. Write a SQL statement to insert 3 rows by a single insert statement.

Click me to see the solution

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)

Click me to see the solution

7. Write a SQL statement to insert one row in the jobs table to ensure that no duplicate values will be entered into the job_id column.

Click me to see the solution

8. Write a SQL statement to insert a record into the table countries to ensure that, at country_id and the region_id combination will be entered once in the table.

Click me to see the solution

9. Write a SQL statement to insert rows into the table countries in which the value of country_id column will be unique and auto incremented.

Click me to see the solution

10. Write a SQL statement to insert records into the table countries to ensure that the country_id column will not contain any duplicate data and this will be automatically incremented and the column country_name will be filled up by 'N/A' if no value assigned to that column.

Click me to see the solution

11. Write a SQL statement to insert rows into the job_history table in which one column job_id is containing those values which exist in job_id column of jobs table.

Click me to see the solution

12. Write a SQL statement to insert rows into the table employees in which a set of columns department_id and manager_id contains a unique value and that combined value must have existed into the table departments.

Click me to see the solution

13. Write a SQL statement to insert rows into the table employees in which a set of columns department_id and job_id contains the values which must have existed into the table departments and jobs.

Click me to see the solution



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://www.w3resource.com/postgresql-exercises/insert-record/