0
0
MySQLquery~10 mins

Variables and control flow in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Variables and control flow
Start
Declare Variables
Assign Initial Values
Evaluate Condition
|Yes
Execute Statements
Update Variables
Loop Back to Condition
No
End
This flow shows how variables are declared, assigned, checked in conditions, updated, and how control loops until the condition is false.
Execution Sample
MySQL
SET @counter = 1;
WHILE @counter <= 3 DO
  SELECT @counter;
  SET @counter = @counter + 1;
END WHILE;
This code uses a variable to count from 1 to 3, selecting the value each time.
Execution Table
StepVariable @counterCondition (@counter <= 3)ActionOutput
111 <= 3 is TRUESELECT 1; SET @counter = 21
222 <= 3 is TRUESELECT 2; SET @counter = 32
333 <= 3 is TRUESELECT 3; SET @counter = 43
444 <= 3 is FALSEExit loopNo output
💡 Loop ends because @counter is 4 and condition 4 <= 3 is FALSE
Variable Tracker
VariableStartAfter 1After 2After 3Final
@counterundefined1234
Key Moments - 2 Insights
Why does the loop stop when @counter is 4 even though we set it to 4 inside the loop?
Because the condition is checked before each loop iteration (see step 4 in execution_table). When @counter becomes 4, the condition @counter <= 3 is FALSE, so the loop exits before running the body again.
What happens if we forget to update @counter inside the loop?
The condition will always be TRUE (e.g., @counter stays 1), causing an infinite loop. The execution_table shows how @counter changes each step to avoid this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of @counter at Step 3?
A2
B3
C4
D1
💡 Hint
Check the 'Variable @counter' column at Step 3 in the execution_table.
At which step does the loop condition become false?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Look at the 'Condition' column in the execution_table to find when it is FALSE.
If we change the initial value of @counter to 4, what will happen?
AThe loop will run once
BThe loop will run three times
CThe loop will not run at all
DThe loop will run infinitely
💡 Hint
Refer to the condition check in the execution_table and consider if 4 <= 3 is TRUE or FALSE at the start.
Concept Snapshot
Variables and control flow in MySQL:
- Declare variables with SET @var = value;
- Use WHILE loops to repeat while a condition is TRUE
- Condition checked before each loop iteration
- Update variables inside loop to avoid infinite loops
- Loop ends when condition becomes FALSE
Full Transcript
This lesson shows how to use variables and control flow in MySQL. We start by declaring a variable @counter and setting it to 1. Then we use a WHILE loop that runs as long as @counter is less than or equal to 3. Inside the loop, we select the current value of @counter and then increase it by 1. The loop repeats until @counter becomes 4, at which point the condition is false and the loop stops. We track the variable changes and see how the condition controls the loop. This helps avoid infinite loops by updating variables properly.