0
0
SQLquery~20 mins

IF-ELSE in procedures in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
IF-ELSE Procedure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of IF-ELSE in a stored procedure
Consider the following stored procedure in SQL. What will be the output when the procedure is called with input value 15?
SQL
CREATE PROCEDURE CheckValue(IN input_val INT)
BEGIN
  IF input_val < 10 THEN
    SELECT 'Less than 10' AS result;
  ELSEIF input_val BETWEEN 10 AND 20 THEN
    SELECT 'Between 10 and 20' AS result;
  ELSE
    SELECT 'Greater than 20' AS result;
  END IF;
END;
ABetween 10 and 20
BNULL
CLess than 10
DGreater than 20
Attempts:
2 left
💡 Hint
Check the condition ranges in the IF-ELSE statement carefully.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in IF-ELSE block
Which option contains a syntax error in the IF-ELSE block of a SQL procedure?
SQL
CREATE PROCEDURE SampleProc(IN val INT)
BEGIN
  IF val > 0 THEN
    SELECT 'Positive';
  ELSE
    SELECT 'Non-positive';
  END IF;
END;
AMissing END keyword for procedure
BMissing THEN keyword after IF condition
CMissing semicolon after END IF
DMissing BEGIN keyword after ELSE
Attempts:
2 left
💡 Hint
Check punctuation after control flow statements.
optimization
advanced
3:00remaining
Optimizing nested IF-ELSE in procedures
Given a procedure with nested IF-ELSE statements checking multiple ranges, which option optimizes the logic to reduce unnecessary checks?
SQL
CREATE PROCEDURE RangeCheck(IN num INT)
BEGIN
  IF num < 0 THEN
    SELECT 'Negative';
  ELSE
    IF num = 0 THEN
      SELECT 'Zero';
    ELSE
      IF num > 0 AND num < 10 THEN
        SELECT 'Small positive';
      ELSE
        SELECT 'Large positive';
      END IF;
    END IF;
  END IF;
END;
AUse ELSEIF to combine conditions instead of nested IFs
BReplace IF with CASE statement for clarity
CRemove ELSE blocks to simplify code
DUse multiple separate procedures for each condition
Attempts:
2 left
💡 Hint
Think about how ELSEIF can reduce nesting.
🔧 Debug
advanced
2:30remaining
Debugging unexpected output in IF-ELSE procedure
A procedure is expected to return 'Adult' for age >= 18 and 'Minor' otherwise. However, it always returns 'Minor'. Which option explains the bug?
SQL
CREATE PROCEDURE CheckAge(IN age INT)
BEGIN
  IF age >= 18 THEN
    SELECT 'Adult';
  ELSE
    SELECT 'Minor';
  END IF;
END;
AThe ELSE block is missing a SELECT statement
BThe condition should be age >= 18, not age > 18
CThe procedure lacks a RETURN statement
DThe input parameter is not declared correctly
Attempts:
2 left
💡 Hint
Check the boundary condition in the IF statement.
🧠 Conceptual
expert
3:00remaining
Understanding IF-ELSE execution flow in procedures
In a SQL procedure, what happens if multiple IF conditions are true but only IF and ELSEIF branches are used without ELSE? Which option best describes the behavior?
AThe ELSE block executes regardless of IF conditions
BAll true conditions' blocks execute sequentially
CThe procedure throws a runtime error due to multiple true conditions
DOnly the first true condition's block executes, others are skipped
Attempts:
2 left
💡 Hint
Think about how IF-ELSEIF chains work in SQL procedures.