You want to assign a category based on age using CASE in PL/pgSQL. Which code correctly assigns 'Child' if age < 13, 'Teen' if age between 13 and 19, and 'Adult' otherwise?
hard📝 Application Q8 of 15
PostgreSQL - PL/pgSQL Fundamentals
You want to assign a category based on age using CASE in PL/pgSQL. Which code correctly assigns 'Child' if age < 13, 'Teen' if age between 13 and 19, and 'Adult' otherwise?
ACASE age
WHEN age < 13 THEN category := 'Child';
WHEN age >= 13 AND age <= 19 THEN category := 'Teen';
ELSE category := 'Adult';
END CASE;
BCASE
WHEN age < 13 THEN category := 'Child';
WHEN age BETWEEN 13 AND 19 THEN category := 'Teen';
ELSE category := 'Adult';
END CASE;
CCASE
WHEN age < 13 THEN category = 'Child';
WHEN age >= 13 AND age <= 19 THEN category = 'Teen';
ELSE category = 'Adult';
END CASE;
DCASE age
WHEN 0 TO 12 THEN category := 'Child';
WHEN 13 TO 19 THEN category := 'Teen';
ELSE category := 'Adult';
END CASE;
Step-by-Step Solution
Solution:
Step 1: Check syntax for CASE with conditions
Using CASE without expression and WHEN with conditions is correct.
Step 2: Validate assignments and conditions
CASE
WHEN age < 13 THEN category := 'Child';
WHEN age BETWEEN 13 AND 19 THEN category := 'Teen';
ELSE category := 'Adult';
END CASE; uses WHEN with conditions and assigns category correctly with := and semicolons.
Final Answer:
CASE without expression and WHEN conditions -> Option B
Quick Check:
CASE with WHEN conditions and := assignments [OK]
Quick Trick:Use CASE without expression for condition ranges [OK]
Common Mistakes:
Using CASE with expression but conditions in WHEN
Using = instead of := for assignment
Invalid range syntax like '0 TO 12'
Master "PL/pgSQL Fundamentals" in PostgreSQL
9 interactive learning modes - each teaches the same concept differently