0
0
MySQLquery~20 mins

Variables and control flow in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Variables and Control Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
ASmaller
BSyntax error
CGreater
DNULL
Attempts:
2 left
💡 Hint
Control flow statements like IF require stored routine context.
query_result
intermediate
2: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;
ASyntax error
B3
C0
D6
Attempts:
2 left
💡 Hint
Control flow statements like WHILE require stored routine context.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in variable declaration
Which option contains the correct syntax to declare and set a variable in MySQL stored procedure?
ADECLARE x INT = 10;
BSET x = 10;
CDECLARE x INT DEFAULT 10;
DDECLARE x = INT 10;
Attempts:
2 left
💡 Hint
Look at the correct order of keywords in DECLARE.
optimization
advanced
2: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;
AInitialize @sum to 0 before the loop and keep the rest same.
BRemove the loop and directly set @sum = 15;
CInitialize @sum inside the loop to 0 each time.
DUse a FOR loop instead of WHILE.
Attempts:
2 left
💡 Hint
Uninitialized variables can cause unexpected results.
🧠 Conceptual
expert
2: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?
AThe variable is accessible throughout the entire stored procedure regardless of where declared.
BThe variable is accessible only after the block ends.
CThe variable is global and accessible outside the stored procedure.
DThe variable is accessible only within the BEGIN...END block where it is declared.
Attempts:
2 left
💡 Hint
Think about local vs global variable scope.