0
0
SQLquery~10 mins

IF-ELSE in procedures in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - IF-ELSE in procedures
Start Procedure
Evaluate IF condition
Execute IF block
End Procedure
The procedure starts, checks the IF condition, executes the IF block if true, otherwise executes the ELSE block, then ends.
Execution Sample
SQL
CREATE PROCEDURE CheckAge(IN age INT)
BEGIN
  IF age >= 18 THEN
    SELECT 'Adult';
  ELSE
    SELECT 'Minor';
  END IF;
END;
This procedure checks if the input age is 18 or older and returns 'Adult' or 'Minor' accordingly.
Execution Table
StepInput ageCondition (age >= 18)Branch TakenOutput
120TrueIF block'Adult'
217FalseELSE block'Minor'
318TrueIF block'Adult'
40FalseELSE block'Minor'
5Exit--Procedure ends
💡 Procedure ends after executing IF or ELSE block based on condition.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
ageundefined2017180-
condition (age >= 18)undefinedTrueFalseTrueFalse-
outputundefined'Adult''Minor''Adult''Minor'-
Key Moments - 2 Insights
Why does the ELSE block run when age is 17?
Because the condition age >= 18 is False at step 2 in the execution_table, so the ELSE block executes.
What happens if age is exactly 18?
At step 3, the condition age >= 18 is True, so the IF block runs and outputs 'Adult'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what output is produced when age is 20?
ANo output
B'Minor'
C'Adult'
DError
💡 Hint
Check row 1 in execution_table where age=20 and output is shown.
At which step does the condition become False?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look at the 'Condition' column in execution_table for False values.
If age is changed to 15, which branch will execute?
AIF block
BELSE block
CBoth IF and ELSE blocks
DNeither block
💡 Hint
Refer to variable_tracker and execution_table where condition is False for ages less than 18.
Concept Snapshot
IF-ELSE in SQL procedures:
- Use IF condition THEN ... ELSE ... END IF;
- Condition is evaluated once per call
- IF block runs if condition True
- ELSE block runs if condition False
- Helps control flow inside procedures
Full Transcript
This visual execution trace shows how IF-ELSE works inside SQL procedures. The procedure starts and checks the condition age >= 18. If true, it runs the IF block and outputs 'Adult'. If false, it runs the ELSE block and outputs 'Minor'. The execution table shows different input ages and which branch runs. The variable tracker follows the age input, condition result, and output at each step. Key moments clarify why ELSE runs for age 17 and IF runs for age 18. The quiz tests understanding of outputs and condition results. This helps beginners see step-by-step how IF-ELSE controls procedure flow.