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

Instruction

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 product.id = 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
    SET
      purchase.total_price = product.price * purchase.quantity
    FROM product
    WHERE purchase.product_id = 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.

Exercise

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.