0
0
PostgreSQLquery~5 mins

Format function for safe formatting in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A%s
B%I
C%L
D%d
What does format('SELECT * FROM %I WHERE name = %L', 'users', 'Alice') return?
ASELECT * FROM "users" WHERE name = 'Alice'
BSELECT * FROM users WHERE name = Alice
CSELECT * FROM 'users' WHERE name = "Alice"
DSELECT * FROM users WHERE name = 'Alice'
Why is using format() safer than simple string concatenation in SQL queries?
AIt formats numbers with commas
BIt runs queries faster
CIt automatically escapes values to prevent SQL injection
DIt converts all text to uppercase
Which placeholder should you use to insert a number safely in format()?
A%L
B%I
C%s
D%d
What will happen if you use %s in format()?
AIt throws an error
BIt escapes the value as a literal
CIt quotes the value as an identifier
DIt inserts the value as is, without escaping
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.