Bird
Raised Fist0
MySQLquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the purpose of a variable in MySQL stored procedures?
easy
A. To create new tables dynamically
B. To permanently save data in the database
C. To store temporary data during procedure execution
D. To define user permissions

Solution

  1. Step 1: Understand variable role in procedures

    Variables hold temporary data only while the procedure runs.
  2. Step 2: Differentiate from permanent storage

    Permanent data is stored in tables, not variables.
  3. Final Answer:

    To store temporary data during procedure execution -> Option C
  4. Quick Check:

    Variables = Temporary data [OK]
Hint: Variables hold temporary data inside procedures [OK]
Common Mistakes:
  • Confusing variables with table data storage
  • Thinking variables create database objects
  • Assuming variables control user access
2. Which of the following is the correct way to declare and set a variable in MySQL stored procedures?
easy
A. SET x INT; DECLARE x = 10;
B. DECLARE x INT; SET x = 10;
C. DECLARE x = 10 INT;
D. SET x = 10; DECLARE x INT;

Solution

  1. Step 1: Recall syntax for variable declaration

    Use DECLARE variable_name data_type; to declare.
  2. Step 2: Assign value using SET

    Use SET variable_name = value; after declaration.
  3. Final Answer:

    DECLARE x INT; SET x = 10; -> Option B
  4. Quick Check:

    DECLARE then SET = correct syntax [OK]
Hint: Declare first, then set value with SET [OK]
Common Mistakes:
  • Using SET before DECLARE
  • Combining declaration and assignment incorrectly
  • Wrong order of keywords
3. Consider the following MySQL procedure snippet:
DECLARE counter INT DEFAULT 1;
WHILE counter <= 3 DO
SET counter = counter + 1;
END WHILE;
SELECT counter;

What will be the output of the SELECT counter; statement?
medium
A. 3
B. Error due to missing BEGIN...END
C. 1
D. 4

Solution

  1. Step 1: Trace the WHILE loop increments

    Counter starts at 1, increments by 1 each loop until counter > 3.
  2. Step 2: Count loop iterations and final value

    Loop runs while counter=1,2,3; increments to 4 and stops.
  3. Final Answer:

    4 -> Option D
  4. Quick Check:

    Counter ends at 4 after loop [OK]
Hint: WHILE increments counter until condition false [OK]
Common Mistakes:
  • Assuming counter stops at 3
  • Forgetting counter increments before condition check
  • Thinking syntax error without BEGIN...END in snippet
4. Identify the error in this MySQL stored procedure snippet:
DECLARE total INT;
SET total = 0
WHILE total < 5 DO
SET total = total + 1;
END WHILE;
medium
A. Missing semicolon after SET total = 0
B. WHILE loop condition syntax is wrong
C. DECLARE must be inside BEGIN...END block
D. Variable total not declared

Solution

  1. Step 1: Check statement endings

    SET total = 0 is missing a semicolon at the end.
  2. Step 2: Validate syntax for DECLARE and WHILE

    DECLARE is valid before BEGIN, and WHILE syntax is correct.
  3. Final Answer:

    Missing semicolon after SET total = 0 -> Option A
  4. Quick Check:

    Every statement needs semicolon [OK]
Hint: Check semicolons after each statement [OK]
Common Mistakes:
  • Ignoring missing semicolon errors
  • Thinking DECLARE must be inside BEGIN...END always
  • Misreading WHILE syntax
5. You want to create a MySQL stored procedure that counts how many numbers between 1 and 10 are even, using variables and control flow. Which of the following code snippets correctly implements this?
hard
A. DECLARE count_even INT DEFAULT 0; DECLARE i INT DEFAULT 1; WHILE i <= 10 DO IF i % 2 = 0 THEN SET count_even = count_even + 1; END IF; SET i = i + 1; END WHILE; SELECT count_even;
B. DECLARE count_even INT; SET count_even = 0; FOR i IN 1..10 DO IF i % 2 = 0 THEN SET count_even = count_even + 1; END IF; END FOR; SELECT count_even;
C. DECLARE count_even INT DEFAULT 0; SET i = 1; WHILE i < 10 DO IF i % 2 = 1 THEN SET count_even = count_even + 1; END IF; SET i = i + 1; END WHILE; SELECT count_even;
D. DECLARE count_even INT DEFAULT 0; DECLARE i INT DEFAULT 1; WHILE i <= 10 DO IF i / 2 = 0 THEN SET count_even = count_even + 1; END IF; SET i = i + 1; END WHILE; SELECT count_even;

Solution

  1. Step 1: Check loop and variable initialization

    DECLARE count_even INT DEFAULT 0; DECLARE i INT DEFAULT 1; WHILE i <= 10 DO IF i % 2 = 0 THEN SET count_even = count_even + 1; END IF; SET i = i + 1; END WHILE; SELECT count_even; correctly declares variables and initializes them.
  2. Step 2: Verify condition and increment logic

    DECLARE count_even INT DEFAULT 0; DECLARE i INT DEFAULT 1; WHILE i <= 10 DO IF i % 2 = 0 THEN SET count_even = count_even + 1; END IF; SET i = i + 1; END WHILE; SELECT count_even; uses WHILE loop from 1 to 10, checks even with modulo, increments count and i properly.
  3. Step 3: Identify errors in other options

    DECLARE count_even INT; SET count_even = 0; FOR i IN 1..10 DO IF i % 2 = 0 THEN SET count_even = count_even + 1; END IF; END FOR; SELECT count_even; uses invalid FOR syntax; C counts odd numbers; D uses division instead of modulo.
  4. Final Answer:

    Option A code correctly counts even numbers between 1 and 10 -> Option A
  5. Quick Check:

    Use WHILE + modulo for even check [OK]
Hint: Use WHILE loop and modulo (%) to check even numbers [OK]
Common Mistakes:
  • Using invalid FOR loop syntax in MySQL
  • Confusing modulo (%) with division (/)
  • Counting odd numbers instead of even
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard