Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Referring to other tables
INSERT, UPDATE, and DELETE with subqueries
13. Summary


Amazing! Let's summarize what we have learned. You know how to ...

  • ... use INSERT INTO with SELECT to insert data from one table into another:
    INSERT INTO history_purchase
    SELECT * FROM purchase;
  • ... delete information from one table based on data from another table, as in DELETE FROM ... USING:
    DELETE FROM purchase
    USING product
    WHERE = purchase.product_id
      AND product.price IS NULL;
  • ... update data in one table based on data from another table by using UPDATE ... FROM:
    UPDATE purchase
      purchase.total_price = product.price * purchase.quantity
    FROM product
    WHERE purchase.product_id =;
  • ... write INSERT, UPDATE, and DELETE statements that include subqueries:
    DELETE FROM product
    WHERE id NOT IN (SELECT product_id FROM purchase)
      AND delivered_timestamp < CURRENT_TIMESTAMP - INTERVAL '3 years';

Note: INSERT, UPDATE, and DELETE operations can also include common table expressions (CTEs). However, an explanation of that is beyond the scope of this course. If you would like to learn more about it, check out our Recursive Queries in PostgreSQL course.


Congratulations! That's all we wanted to teach you about inserting, updating, and deleting data in PostgreSQL. In the next part, you'll test your knowledge with some cumulative practice exercises.

Click Next exercise to proceed to the final quiz.