Challenge - 5 Problems
DO Blocks Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate1:30remaining
Output of a simple DO block with RAISE NOTICE
What will be the output message when this DO block runs in PostgreSQL?
PostgreSQL
DO $$ BEGIN RAISE NOTICE 'Hello from DO block!'; END $$;
Attempts:
2 left
💡 Hint
RAISE NOTICE prints a message to the client.
✗ Incorrect
The RAISE NOTICE command outputs a notice message to the client. Here it prints 'Hello from DO block!'.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in this DO block
Which option correctly identifies the syntax error in this DO block?
PostgreSQL
DO $$ BEGIN PERFORM 1/0; END $$;
Attempts:
2 left
💡 Hint
Check if the code syntax is valid before runtime errors.
✗ Incorrect
The code syntax is valid. The error occurs at runtime due to division by zero, not a syntax error.
❓ optimization
advanced2:00remaining
Best way to loop 5 times in a DO block
Which DO block code efficiently loops exactly 5 times and raises a notice each iteration?
Attempts:
2 left
💡 Hint
PostgreSQL supports FOR loops with range syntax 1..5.
✗ Incorrect
Option A uses the correct FOR loop syntax with 1..5 range. Option A uses a WHILE loop but is more verbose. Option A loops over an array which works but is less efficient. Option A uses invalid FOR loop syntax '1 TO 5'.
🔧 Debug
advanced1:30remaining
Why does this DO block fail to compile?
This DO block fails with a syntax error. What is the cause?
PostgreSQL
DO $$ BEGIN RAISE NOTICE 'Value: %', my_var; END $$;
Attempts:
2 left
💡 Hint
Variables must be declared in DECLARE section before use.
✗ Incorrect
The variable my_var is used without declaration, causing a compilation error.
🧠 Conceptual
expert2:00remaining
Effect of transaction control commands inside DO blocks
Which statement about transaction control commands inside DO blocks is true?
Attempts:
2 left
💡 Hint
Consider how PostgreSQL treats anonymous code blocks and transactions.
✗ Incorrect
DO blocks run inside a single transaction context. You cannot use COMMIT or ROLLBACK inside them; attempting to do so causes an error.