2. Which of the following is the correct syntax to create a simple utility function in PostgreSQL that returns the square of an integer?
easy
A. CREATE FUNCTION square(x INT) RETURNS INT AS 'RETURN x * x;' LANGUAGE sql;
B. CREATE FUNCTION square(x INT) RETURNS INT BEGIN RETURN x * x; END;
C. CREATE FUNCTION square(x INT) RETURNS INT AS $$ BEGIN RETURN x * x; END; $$ LANGUAGE plpgsql;
D. CREATE FUNCTION square(x INT) RETURNS INT AS $$ RETURN x * x; $$ LANGUAGE plpgsql;
Solution
Step 1: Identify correct function creation syntax
PostgreSQL requires the function body inside $$ with BEGIN and END for plpgsql language.
Step 2: Check for proper RETURNS and LANGUAGE clauses
CREATE FUNCTION square(x INT) RETURNS INT AS $$ BEGIN RETURN x * x; END; $$ LANGUAGE plpgsql; correctly uses RETURNS INT, BEGIN...END block, and LANGUAGE plpgsql.
Final Answer:
CREATE FUNCTION square(x INT) RETURNS INT AS $$ BEGIN RETURN x * x; END; $$ LANGUAGE plpgsql; -> Option C
Quick Check:
Correct plpgsql function syntax = CREATE FUNCTION square(x INT) RETURNS INT AS $$ BEGIN RETURN x * x; END; $$ LANGUAGE plpgsql; [OK]
Hint: Use $$ with BEGIN...END and specify LANGUAGE plpgsql [OK]
Common Mistakes:
Omitting BEGIN and END in plpgsql functions
Missing LANGUAGE clause
Incorrect function body syntax
3. Given the function below, what will be the output of SELECT multiply_by_two(5);?
CREATE FUNCTION multiply_by_two(n INT) RETURNS INT AS $$ BEGIN RETURN n * 2; END; $$ LANGUAGE plpgsql;
medium
A. 5
B. NULL
C. Error: function not found
D. 10
Solution
Step 1: Understand the function logic
The function takes an integer n and returns n multiplied by 2.
Step 2: Apply the input value 5
Calling multiply_by_two(5) returns 5 * 2 = 10.
Final Answer:
10 -> Option D
Quick Check:
5 * 2 = 10 [OK]
Hint: Multiply input by 2 as function defines [OK]
Common Mistakes:
Confusing input and output values
Assuming function returns input unchanged
Expecting NULL or error without reason
4. You wrote this utility function but get an error when calling it:
CREATE FUNCTION add_five(n INT) RETURNS INT AS $$ BEGIN RETURN n + 5 END; $$ LANGUAGE plpgsql;
What is the likely cause of the error?
medium
A. Wrong data type for parameter
B. Missing semicolon after RETURN statement
C. Incorrect function name
D. LANGUAGE clause is missing
Solution
Step 1: Check function syntax carefully
In plpgsql, each statement must end with a semicolon; the RETURN line lacks one.
Adding a semicolon after RETURN n + 5 fixes the error.
Final Answer:
Missing semicolon after RETURN statement -> Option B
Quick Check:
Semicolon needed after RETURN [OK]
Hint: Always end statements with semicolon in plpgsql [OK]
Common Mistakes:
Forgetting semicolon after RETURN
Assuming function name causes error
Ignoring LANGUAGE clause presence
5. You want to create a utility function that returns the length of a text string but returns 0 if the input is NULL. Which function definition correctly implements this behavior?
hard
A. CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ BEGIN IF s IS NULL THEN RETURN 0; ELSE RETURN LENGTH(s); END IF; END; $$ LANGUAGE plpgsql;
B. CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ BEGIN RETURN COALESCE(s, 0); END; $$ LANGUAGE plpgsql;
C. CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ RETURN LENGTH(s) OR 0; $$ LANGUAGE sql;
D. CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ BEGIN RETURN LENGTH(s); END; $$ LANGUAGE plpgsql;
Solution
Step 1: Understand requirement for NULL input handling
The function must return 0 if input is NULL, else return string length.
Step 2: Analyze each option for NULL safety
CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ BEGIN IF s IS NULL THEN RETURN 0; ELSE RETURN LENGTH(s); END IF; END; $$ LANGUAGE plpgsql; explicitly checks for NULL and returns 0, fulfilling the requirement correctly.
Final Answer:
CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ BEGIN IF s IS NULL THEN RETURN 0; ELSE RETURN LENGTH(s); END IF; END; $$ LANGUAGE plpgsql; -> Option A
Quick Check:
Explicit NULL check returns 0 [OK]
Hint: Use IF NULL THEN return 0 ELSE length END IF [OK]
Common Mistakes:
Not handling NULL input explicitly
Using COALESCE incorrectly with LENGTH
Assuming LENGTH returns 0 for NULL
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.