SELECT query used with an
INSERT can be as complex as you need it to be. For example, we could add a
WHERE clause to filter records. Check out the following query:
INSERT INTO product_history (id, name, price)
SELECT id, name, price
WHERE delivered_timestamp >= '2017-01-01' and delivered_timestamp < '2018-01-01'
This chooses the ID, name, and price of products that were delivered in 2017. All these rows are inserted into the report table
SELECT query in an
INSERT can be complicated. For example, it can contain an aggregate function and a
GROUP BY to compute statistics that will be used in later reports.