0
0
PostgreSQLquery~20 mins

IF-ELSIF-ELSE control flow in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
IF-ELSIF-ELSE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of IF-ELSIF-ELSE in a PL/pgSQL function
Consider the following PostgreSQL function. What will be the output when calling check_number(15)?
PostgreSQL
CREATE OR REPLACE FUNCTION check_number(num INTEGER) RETURNS TEXT AS $$
BEGIN
  IF num < 10 THEN
    RETURN 'Less than 10';
  ELSIF num < 20 THEN
    RETURN 'Between 10 and 19';
  ELSE
    RETURN '20 or more';
  END IF;
END;
$$ LANGUAGE plpgsql;
A'Between 10 and 19'
B'Less than 10'
C'20 or more'
DNULL
Attempts:
2 left
💡 Hint
Think about which condition matches the input number 15.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in IF-ELSIF-ELSE block
Which option contains a syntax error in the IF-ELSIF-ELSE block in PostgreSQL PL/pgSQL?
PostgreSQL
BEGIN
  IF x = 1 THEN
    RAISE NOTICE 'One';
  ELSEIF x = 2 THEN
    RAISE NOTICE 'Two';
  ELSE
    RAISE NOTICE 'Other';
  END IF;
END;
ARemove ELSE block
BReplace END IF with END
CAdd semicolon after THEN
DReplace ELSEIF with ELSIF
Attempts:
2 left
💡 Hint
Check the correct keyword spelling for else-if in PL/pgSQL.
query_result
advanced
2:00remaining
Result of nested IF-ELSIF-ELSE in a function
What will be the output of evaluate_score(75) given this function?
PostgreSQL
CREATE OR REPLACE FUNCTION evaluate_score(score INTEGER) RETURNS TEXT AS $$
BEGIN
  IF score >= 90 THEN
    RETURN 'Excellent';
  ELSIF score >= 70 THEN
    IF score >= 80 THEN
      RETURN 'Very Good';
    ELSE
      RETURN 'Good';
    END IF;
  ELSE
    RETURN 'Needs Improvement';
  END IF;
END;
$$ LANGUAGE plpgsql;
A'Good'
B'Very Good'
C'Excellent'
D'Needs Improvement'
Attempts:
2 left
💡 Hint
Check the nested IF conditions carefully for score 75.
🔧 Debug
advanced
2:00remaining
Find the logical error in IF-ELSIF-ELSE
This function is intended to classify ages but returns incorrect results for age 18. What is the logical error?
PostgreSQL
CREATE OR REPLACE FUNCTION classify_age(age INTEGER) RETURNS TEXT AS $$
BEGIN
  IF age < 13 THEN
    RETURN 'Child';
  ELSIF age > 13 AND age < 18 THEN
    RETURN 'Teenager';
  ELSE
    RETURN 'Adult';
  END IF;
END;
$$ LANGUAGE plpgsql;
AShould use OR instead of AND in ELSIF
BMissing ELSEIF for age = 18
CThe condition excludes age 13 and 18 from 'Teenager' category
DRETURN statements are misplaced
Attempts:
2 left
💡 Hint
Check the conditions carefully for boundary ages 13 and 18.
optimization
expert
2:00remaining
Optimize IF-ELSIF-ELSE for performance
Which option optimizes this IF-ELSIF-ELSE block for better performance when checking multiple conditions on a variable val?
PostgreSQL
IF val = 1 THEN
  RETURN 'One';
ELSIF val = 2 THEN
  RETURN 'Two';
ELSIF val = 3 THEN
  RETURN 'Three';
ELSE
  RETURN 'Other';
END IF;
AUse nested IF statements for each condition
BUse CASE statement instead of IF-ELSIF-ELSE
CReplace ELSE with another ELSIF for val = 4
DUse multiple IF statements without ELSIF
Attempts:
2 left
💡 Hint
Consider which control structure is more efficient and readable for multiple discrete values.