0
0
MySQLquery~10 mins

INSERT with SELECT in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - INSERT with SELECT
Start
Run SELECT query
Fetch rows from SELECT
Insert each row into target table
All rows inserted
End
This flow shows how an INSERT statement uses a SELECT query to get rows, then inserts those rows into another table.
Execution Sample
MySQL
INSERT INTO employees_backup (id, name, salary)
SELECT id, name, salary FROM employees
WHERE salary > 50000;
This query copies employees with salary over 50000 from employees table into employees_backup table.
Execution Table
StepActionSELECT Result RowINSERT ActionRows Inserted So Far
1Run SELECT queryid=2, name='Alice', salary=60000Insert row into employees_backup1
2Run SELECT queryid=4, name='Bob', salary=70000Insert row into employees_backup2
3Run SELECT queryid=5, name='Carol', salary=80000Insert row into employees_backup3
4Run SELECT queryNo more rowsStop inserting3
💡 No more rows returned by SELECT, insertion complete.
Variable Tracker
VariableStartAfter 1After 2After 3Final
Rows Inserted01233
Key Moments - 2 Insights
Why does the INSERT statement not have VALUES but uses SELECT instead?
Because the INSERT is using a SELECT query to get multiple rows at once, so VALUES is replaced by the SELECT result rows as shown in execution_table steps 1-3.
What happens if the SELECT query returns no rows?
No rows are inserted. The execution_table row 4 shows 'No more rows' and insertion stops immediately.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many rows have been inserted after step 2?
A3
B1
C2
D0
💡 Hint
Check the 'Rows Inserted So Far' column at step 2 in the execution_table.
At which step does the SELECT query return no more rows?
AStep 4
BStep 3
CStep 1
DStep 2
💡 Hint
Look at the 'SELECT Result Row' column in execution_table for the step showing 'No more rows'.
If the WHERE condition changed to salary > 80000, how would the 'Rows Inserted So Far' change at step 3?
AIt would be 3
BIt would be 0
CIt would be 1
DIt would be 2
💡 Hint
Refer to variable_tracker and consider which rows meet the new condition.
Concept Snapshot
INSERT with SELECT syntax:
INSERT INTO target_table (columns)
SELECT columns FROM source_table WHERE condition;

- SELECT fetches rows
- INSERT adds those rows to target
- Useful for copying or transforming data
- No VALUES keyword needed
- Stops when SELECT returns no rows
Full Transcript
This visual execution shows how an INSERT statement can use a SELECT query to insert multiple rows into a table. First, the SELECT query runs and fetches rows matching the condition. Each row from the SELECT is inserted into the target table one by one. The process continues until no more rows are returned by the SELECT. The variable tracker shows how the count of inserted rows increases step by step. Key moments clarify why VALUES is not used and what happens if no rows match. The quiz tests understanding of the insertion count and flow based on the execution table.