Challenge - 5 Problems
Variables and Control Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of variable assignment and IF statement
What is the output of the following MySQL code snippet?
MySQL
SET @x = 5; IF @x > 3 THEN SELECT 'Greater'; ELSE SELECT 'Smaller'; END IF;
Attempts:
2 left
💡 Hint
Control flow statements like IF require stored routine context.
✗ Incorrect
IF statements can only be used within stored routines (procedures, functions, etc.). This standalone code causes a syntax error.
❓ query_result
intermediate2:00remaining
Output of WHILE loop with variable increment
What will be the output of this MySQL code?
MySQL
SET @count = 1; SET @result = 0; WHILE @count <= 3 DO SET @result = @result + @count; SET @count = @count + 1; END WHILE; SELECT @result;
Attempts:
2 left
💡 Hint
Control flow statements like WHILE require stored routine context.
✗ Incorrect
WHILE loops can only be used within stored programs; this standalone code results in a syntax error.
📝 Syntax
advanced2:00remaining
Identify the syntax error in variable declaration
Which option contains the correct syntax to declare and set a variable in MySQL stored procedure?
Attempts:
2 left
💡 Hint
Look at the correct order of keywords in DECLARE.
✗ Incorrect
In MySQL stored procedures, variables are declared with 'DECLARE var_name type DEFAULT value;'. Options C and D have wrong syntax. Option C sets a variable but does not declare it.
❓ optimization
advanced2:00remaining
Optimize variable usage in a loop
Which option optimizes the following code to avoid unnecessary variable assignments?
Original code:
SET @i = 1;
WHILE @i <= 5 DO
SET @sum = @sum + @i;
SET @i = @i + 1;
END WHILE;
SELECT @sum;
Attempts:
2 left
💡 Hint
Uninitialized variables can cause unexpected results.
✗ Incorrect
Without initializing @sum to 0, the addition may produce NULL or wrong results. Initializing @sum before the loop is necessary.
🧠 Conceptual
expert2:00remaining
Understanding variable scope in MySQL stored procedures
Consider a stored procedure where a variable is declared inside a BEGIN...END block. Which statement is true about the variable's scope?
Attempts:
2 left
💡 Hint
Think about local vs global variable scope.
✗ Incorrect
Variables declared inside a BEGIN...END block are local to that block and cannot be accessed outside it.