Challenge - 5 Problems
Format Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate1:30remaining
What is the output of this PostgreSQL format function?
Consider the following SQL query using the
What is the output of this query?
format function:SELECT format('Hello, %s! Today is %s.', 'Alice', 'Monday');What is the output of this query?
PostgreSQL
SELECT format('Hello, %s! Today is %s.', 'Alice', 'Monday');
Attempts:
2 left
💡 Hint
The format function replaces placeholders like %s with the provided arguments in order.
✗ Incorrect
The
format function replaces each %s placeholder with the corresponding argument. Here, 'Alice' replaces the first %s and 'Monday' replaces the second %s.❓ query_result
intermediate1:30remaining
What does this format call return with integer and string?
Given this SQL statement:
What is the output?
SELECT format('ID: %s, Name: %s', 123, 'Bob');What is the output?
PostgreSQL
SELECT format('ID: %s, Name: %s', 123, 'Bob');
Attempts:
2 left
💡 Hint
The %s placeholder can accept any data type and converts it to text.
✗ Incorrect
The
format function converts all arguments to text when using %s, so 123 becomes '123' and 'Bob' stays 'Bob'.📝 Syntax
advanced1:30remaining
Which option causes a syntax error in this format usage?
Identify which SQL statement will cause a syntax error when using the
format function:Attempts:
2 left
💡 Hint
Check if all placeholders have matching arguments.
✗ Incorrect
Option A has a missing argument after the comma, causing a syntax error.
❓ optimization
advanced2:00remaining
Which format usage is safest to avoid SQL injection?
You want to safely insert a user input string into a query using
format. Which option correctly uses format to avoid SQL injection?Attempts:
2 left
💡 Hint
Use the correct format specifier to safely quote literals.
✗ Incorrect
The %L specifier safely quotes and escapes string literals, preventing SQL injection. %s does not quote, %I is for identifiers.
🧠 Conceptual
expert2:30remaining
Why use format() with %I instead of string concatenation for identifiers?
In PostgreSQL, when dynamically building SQL queries, why is it better to use
format() with the %I specifier for table or column names instead of simple string concatenation?Attempts:
2 left
💡 Hint
Think about how identifiers with spaces or special characters are handled.
✗ Incorrect
The %I specifier quotes identifiers properly, so names with spaces or reserved keywords are handled safely. String concatenation risks syntax errors or injection.