Challenge - 5 Problems
Variable Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this variable assignment in a PostgreSQL function?
Consider the following PL/pgSQL block. What will be the value of
result after execution?PostgreSQL
DO $$ DECLARE result integer := 5; BEGIN result := result + 10; RAISE NOTICE 'Result is %', result; END $$;
Attempts:
2 left
💡 Hint
Remember that variables declared in PL/pgSQL can be initialized and then reassigned.
✗ Incorrect
The variable
result is initialized to 5, then 10 is added, making it 15. The RAISE NOTICE prints this value.📝 Syntax
intermediate2:00remaining
Which option correctly declares and assigns a variable in PL/pgSQL?
Choose the correct syntax to declare an integer variable
count and assign it the value 100.Attempts:
2 left
💡 Hint
PL/pgSQL uses := for assignment during declaration.
✗ Incorrect
In PL/pgSQL, variables are declared with their type and assigned using :=. Option D follows this syntax.
🔧 Debug
advanced2:00remaining
Why does this PL/pgSQL block raise an error?
Examine the code below and identify the cause of the error.
PostgreSQL
DO $$ DECLARE total integer; BEGIN total := 'ten'; RAISE NOTICE 'Total is %', total; END $$;
Attempts:
2 left
💡 Hint
Check the data type of the assigned value compared to the variable type.
✗ Incorrect
The variable
total is declared as integer but assigned a string 'ten', causing a type mismatch error.🧠 Conceptual
advanced2:00remaining
What happens if you declare a variable without initializing it in PL/pgSQL?
Consider the variable declaration
DECLARE count integer; without assignment. What is the initial value of count?Attempts:
2 left
💡 Hint
Think about how SQL handles uninitialized variables or columns.
✗ Incorrect
In PL/pgSQL, uninitialized variables default to NULL, meaning no value is assigned yet.
❓ optimization
expert2:00remaining
Which variable declaration is most efficient for repeated integer assignment in PL/pgSQL?
You need a variable
counter to increment many times inside a loop. Which declaration is best for performance and clarity?Attempts:
2 left
💡 Hint
Consider type size and initialization for numeric counters.
✗ Incorrect
Declaring
counter as integer and initializing to 0 is efficient and clear for counting loops.