Recall & Review
beginner
What is the purpose of the
format() function in PostgreSQL?The
format() function safely creates formatted strings by inserting values into a template, helping avoid SQL injection and syntax errors.Click to reveal answer
intermediate
How does
format() help prevent SQL injection?It automatically escapes values and inserts them safely into the string, so user input cannot break the query or run harmful commands.
Click to reveal answer
beginner
What placeholder would you use in
format() to insert a string safely?Use
%L to insert a string as a literal, which adds quotes and escapes special characters.Click to reveal answer
intermediate
Write a simple example of
format() inserting a table name and a value safely.Example:
format('SELECT * FROM %I WHERE id = %L', 'users', 123) returns SELECT * FROM "users" WHERE id = 123.Click to reveal answer
intermediate
What is the difference between
%I and %L in format()?%I is for identifiers like table or column names (adds quotes if needed). %L is for literal values like strings or numbers (adds quotes and escapes).Click to reveal answer
Which
format() placeholder is used to safely insert a table name?✗ Incorrect
%I is used for identifiers like table or column names.What does
format('SELECT * FROM %I WHERE name = %L', 'users', 'Alice') return?✗ Incorrect
The table name is quoted as an identifier and the string value is quoted and escaped as a literal.
Why is using
format() safer than simple string concatenation in SQL queries?✗ Incorrect
It escapes values properly, preventing malicious input from breaking the query.
Which placeholder should you use to insert a number safely in
format()?✗ Incorrect
%L safely inserts literal values including numbers by quoting and escaping if needed.What will happen if you use
%s in format()?✗ Incorrect
%s inserts the value directly without escaping, which can be unsafe.Explain how the
format() function in PostgreSQL helps write safe SQL queries.Think about how user input can be dangerous and how format() protects against that.
You got /4 concepts.
Describe the difference between the %I and %L placeholders in the
format() function.One is for names, the other for values.
You got /3 concepts.