0
0
PostgreSQLquery~20 mins

Variable declaration and assignment in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Variable Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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 $$;
ASyntax error due to variable initialization
BResult is 15
CResult is 10
DResult is 5
Attempts:
2 left
💡 Hint
Remember that variables declared in PL/pgSQL can be initialized and then reassigned.
📝 Syntax
intermediate
2: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.
ADECLARE count integer = 100;
BDECLARE count := integer 100;
CDECLARE count integer; count = 100;
Dcount integer := 100;
Attempts:
2 left
💡 Hint
PL/pgSQL uses := for assignment during declaration.
🔧 Debug
advanced
2: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 $$;
AType mismatch error because 'ten' is a string, not an integer
BSyntax error due to missing semicolon after declaration
CRuntime error because variable total is not initialized
DNo error, the code runs and prints 'Total is ten'
Attempts:
2 left
💡 Hint
Check the data type of the assigned value compared to the variable type.
🧠 Conceptual
advanced
2: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?
AIt contains random garbage value
BIt causes a syntax error
CIt is NULL by default
DIt is zero by default
Attempts:
2 left
💡 Hint
Think about how SQL handles uninitialized variables or columns.
optimization
expert
2: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?
ADECLARE counter integer := 0;
BDECLARE counter integer;
CDECLARE counter bigint := 0;
DDECLARE counter text := '0';
Attempts:
2 left
💡 Hint
Consider type size and initialization for numeric counters.