What if you could copy thousands of records with one simple command instead of typing each one?
Why INSERT with SELECT in MySQL? - Purpose & Use Cases
Imagine you have two big lists of contacts in separate spreadsheets. You want to add all contacts from one list into the other without typing each one manually.
Copying and pasting each contact one by one is slow and mistakes happen easily. You might miss some entries or add duplicates by accident.
Using INSERT with SELECT lets you copy many rows from one table to another in one simple command, saving time and avoiding errors.
INSERT INTO contacts (name, email) VALUES ('Alice', 'alice@example.com'); INSERT INTO contacts (name, email) VALUES ('Bob', 'bob@example.com');
INSERT INTO contacts (name, email) SELECT name, email FROM new_contacts;
This lets you quickly merge data from one table into another, making database updates fast and reliable.
A company merges customer info from a marketing list into their main customer database without retyping or errors.
Manual entry is slow and error-prone.
INSERT with SELECT copies many rows at once.
It makes data merging easy and safe.
Practice
INSERT INTO ... SELECT statement do in MySQL?Solution
Step 1: Understand the purpose of INSERT INTO ... SELECT
This statement copies data from one table to another by selecting rows from the source table.Step 2: Recognize the matching column requirement
The columns selected must match the target table's columns in number and type to insert correctly.Final Answer:
Copies rows from one table to another matching column structure -> Option AQuick Check:
INSERT with SELECT copies rows [OK]
- Confusing INSERT with SELECT as a delete or update operation
- Thinking it creates a new table
- Assuming it modifies existing rows instead of inserting
table1 into table2 using INSERT INTO ... SELECT?Solution
Step 1: Identify correct INSERT INTO ... SELECT syntax
The correct syntax isINSERT INTO target_table SELECT ... FROM source_table;.Step 2: Match the option to syntax
INSERT INTO table2 SELECT * FROM table1; matches this syntax exactly, inserting rows fromtable1intotable2.Final Answer:
INSERT INTO table2 SELECT * FROM table1; -> Option CQuick Check:
Correct INSERT INTO ... SELECT syntax = INSERT INTO table2 SELECT * FROM table1; [OK]
- Swapping table names in INSERT and SELECT
- Using SELECT INTO which is not standard in MySQL
- Incorrect keyword order like INSERT table INTO
employees(id, name, department)managers(id, name, department)What will be the result of this query?
INSERT INTO managers (id, name, department) SELECT id, name, department FROM employees WHERE department = 'Sales';
Solution
Step 1: Analyze the SELECT part with WHERE clause
The SELECT picks only employees where department equals 'Sales'.Step 2: Understand INSERT INTO effect
These selected rows are inserted into the managers table, adding only Sales department employees.Final Answer:
Only employees from Sales department are copied to managers -> Option AQuick Check:
WHERE filters rows inserted [OK]
- Assuming all employees are copied ignoring WHERE
- Thinking it updates existing rows instead of inserting
- Confusing syntax causing no rows inserted
INSERT INTO products (id, name) SELECT id, name, price FROM items;
Solution
Step 1: Compare columns in INSERT and SELECT
INSERT specifies 2 columns (id, name), but SELECT returns 3 columns (id, name, price).Step 2: Understand column count requirement
Number of columns must match exactly; mismatch causes error.Final Answer:
Number of columns in INSERT and SELECT do not match -> Option BQuick Check:
Column count mismatch causes error [OK]
- Ignoring extra columns in SELECT
- Thinking WHERE is mandatory
- Assuming reversed table names cause error
orders to newsletter_subscribers without duplicates. Which query achieves this?Solution
Step 1: Identify how to select unique emails
UsingSELECT DISTINCT emailreturns unique emails from orders.Step 2: Insert unique emails into newsletter_subscribers
INSERT INTO with SELECT DISTINCT copies only unique emails, avoiding duplicates.Final Answer:
INSERT INTO newsletter_subscribers (email) SELECT DISTINCT email FROM orders; -> Option DQuick Check:
Use DISTINCT to avoid duplicates [OK]
- Using WHERE NOT IN with incorrect syntax
- Grouping but selecting only duplicates
- Inserting NULL emails unintentionally
Ready to prove it?
Pass the test and this pattern moves unknown
