INSERT with SELECT in MySQL - Time & Space Complexity
When we use INSERT with SELECT in MySQL, we copy rows from one table to another. Understanding how long this takes helps us plan for bigger data.
We want to know how the time to insert grows as the number of rows selected grows.
Analyze the time complexity of the following code snippet.
INSERT INTO archive_orders (order_id, customer_id, order_date)
SELECT order_id, customer_id, order_date
FROM orders
WHERE order_date < '2023-01-01';
This code copies all orders before 2023 from the orders table into archive_orders.
- Primary operation: Reading each row from the orders table that matches the date condition.
- How many times: Once for each matching row, then inserting that row into archive_orders.
As the number of rows before 2023 grows, the work grows too because each row is read and inserted.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 reads and 10 inserts |
| 100 | About 100 reads and 100 inserts |
| 1000 | About 1000 reads and 1000 inserts |
Pattern observation: The total work grows directly with the number of rows selected.
Time Complexity: O(n)
This means the time to complete the insert grows in a straight line with the number of rows copied.
[X] Wrong: "The insert happens all at once, so time does not depend on number of rows."
[OK] Correct: Each row must be read and inserted, so more rows mean more work and more time.
Knowing how insert with select scales helps you explain data migration or backup tasks clearly and confidently.
"What if the SELECT query had a JOIN with another large table? How would the time complexity change?"
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
