Bird
0
0

Consider this PL/pgSQL function:

medium📝 query result Q5 of 15
PostgreSQL - Advanced PL/pgSQL
Consider this PL/pgSQL function:
CREATE OR REPLACE FUNCTION test_return() RETURNS INTEGER AS $$
DECLARE
  val INTEGER := 5;
BEGIN
  IF val > 3 THEN
    RETURN val * 2;
  ELSE
    RETURN val / 2;
  END IF;
END;
$$ LANGUAGE plpgsql;

What will SELECT test_return(); return?
A10
B2
C5
DNULL
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the IF condition

    val is 5, which is greater than 3, so the IF branch executes.
  2. Step 2: Calculate the RETURN value

    RETURN val * 2 = 5 * 2 = 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Function returns doubled val when val > 3 [OK]
Quick Trick: Check IF conditions carefully to predict return values [OK]
Common Mistakes:
  • Returning val instead of val * 2
  • Confusing ELSE branch output
  • Assuming NULL return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes