Introduction
You use INSERT with SELECT to copy data from one table to another quickly without typing each value.
INSERT INTO target_table (column1, column2, ...) SELECT column1, column2, ... FROM source_table WHERE condition;
INSERT INTO employees_backup (id, name, salary) SELECT id, name, salary FROM employees WHERE salary > 50000;
INSERT INTO archive_orders (order_id, order_date) SELECT order_id, order_date FROM orders WHERE order_date < '2023-01-01';
INSERT INTO new_customers (customer_id, customer_name) SELECT customer_id, customer_name FROM customers WHERE region = 'West';
CREATE TABLE products ( product_id INT, product_name VARCHAR(50), price DECIMAL(10,2) ); CREATE TABLE expensive_products ( product_id INT, product_name VARCHAR(50), price DECIMAL(10,2) ); INSERT INTO products VALUES (1, 'Pen', 1.20), (2, 'Notebook', 3.50), (3, 'Backpack', 45.00), (4, 'Calculator', 55.00); INSERT INTO expensive_products (product_id, product_name, price) SELECT product_id, product_name, price FROM products WHERE price > 40.00; SELECT * FROM expensive_products;
INSERT INTO ... SELECT statement do in MySQL?table1 into table2 using INSERT INTO ... SELECT?INSERT INTO target_table SELECT ... FROM source_table;.table1 into table2.employees(id, name, department)managers(id, name, department)INSERT INTO managers (id, name, department) SELECT id, name, department FROM employees WHERE department = 'Sales';
INSERT INTO products (id, name) SELECT id, name, price FROM items;
orders to newsletter_subscribers without duplicates. Which query achieves this?SELECT DISTINCT email returns unique emails from orders.Pass the test and this pattern moves unknown