What is query parameterization in database queries?
Query parameterization means using placeholders in SQL queries and passing actual values separately to prevent direct insertion of user input into the query string.
Click to reveal answer
beginner
Why is query parameterization important for safety?
It protects against SQL injection attacks by ensuring user input is treated as data, not executable code in the database query.
Click to reveal answer
intermediate
How do you use query parameterization with Node.js and the 'mysql2' library?
Use '?' as placeholders in the SQL string and pass an array of values as the second argument to the query function, e.g., connection.query('SELECT * FROM users WHERE id = ?', [userId]).
Click to reveal answer
beginner
What can happen if you do NOT use query parameterization?
Your app can be vulnerable to SQL injection, allowing attackers to run harmful SQL commands, steal data, or damage your database.
Click to reveal answer
intermediate
Give an example of a safe query using parameterization in Node.js with 'pg' (PostgreSQL) library.
const text = 'SELECT * FROM users WHERE email = $1'; const values = [userEmail]; client.query(text, values);
Click to reveal answer
What symbol is commonly used as a placeholder in parameterized queries with 'mysql2' in Node.js?
A@
B$1
C?
D#
✗ Incorrect
The '?' symbol is used as a placeholder for parameters in 'mysql2' queries.
What is the main security risk avoided by using query parameterization?
ACross-site scripting (XSS)
BDenial of Service (DoS)
CMan-in-the-middle attack
DSQL injection
✗ Incorrect
Query parameterization prevents SQL injection by separating code from data.
In PostgreSQL queries with Node.js 'pg' library, how are parameters referenced?
AUsing numbered placeholders like $1, $2
BUsing '?' placeholders
CUsing '@param' syntax
DUsing '#' symbols
✗ Incorrect
PostgreSQL uses numbered placeholders like $1, $2 for parameterized queries.
Which of these is NOT a benefit of query parameterization?
AMakes queries run faster by caching
BPrevents SQL injection
CAutomatically escapes user input
DAllows dynamic query building without risk
✗ Incorrect
Parameterization mainly improves security, not necessarily query speed.
What happens if user input is directly concatenated into SQL queries without parameterization?
AThe query runs faster
BThe app becomes vulnerable to SQL injection
CThe database automatically sanitizes input
DThe query fails to execute
✗ Incorrect
Direct concatenation allows attackers to inject malicious SQL code.
Explain in your own words why query parameterization is important for database safety in Node.js applications.
Think about how attackers might try to trick your database.
You got /4 concepts.
Describe how you would write a safe SQL query using parameterization with a Node.js database library of your choice.
Remember the syntax differs slightly between libraries like mysql2 and pg.
You got /4 concepts.
Practice
(1/5)
1. Why is query parameterization important in Node.js database queries?
easy
A. It prevents SQL injection by separating user input from the query structure.
B. It makes queries run faster by caching results automatically.
C. It allows queries to be written without any syntax rules.
D. It automatically encrypts the database connection.
Solution
Step 1: Understand SQL injection risk
When user input is directly included in query strings, attackers can inject harmful SQL code.
Step 2: Role of parameterization
Parameterization sends user input separately, so the database treats it only as data, not code.
Final Answer:
It prevents SQL injection by separating user input from the query structure. -> Option A
Assuming injection will run despite parameterization
Expecting syntax errors from malicious input
Thinking all rows will be returned without a WHERE clause
4. Identify the error in this Node.js code using 'sqlite3' for parameterized queries:
db.run('INSERT INTO users (name, age) VALUES (?, ?)', name, age, (err) => {
if (err) throw err;
console.log('User added');
});
medium
A. The 'db.run' method cannot accept parameters.
B. Parameters should be passed as an array, not separate arguments.
C. The query string should use named parameters like ':name' instead of '?'.
D. The callback function is missing an error parameter.
Solution
Step 1: Check parameter passing syntax
sqlite3 expects parameters as an array or object, not separate arguments.
Step 2: Correct usage
Parameters should be passed as an array: [name, age].
Final Answer:
Parameters should be passed as an array, not separate arguments. -> Option B
Quick Check:
sqlite3 parameters = array [OK]
Hint: Pass parameters as array in sqlite3 queries [OK]
Common Mistakes:
Passing parameters as separate arguments
Confusing named and positional parameters
Ignoring callback error handling
5. You want to safely insert multiple user records in a batch using Node.js and 'pg' library. Which approach correctly uses query parameterization to avoid SQL injection?
hard
A. Run separate queries for each user without parameterization.
B. Build a single query string by concatenating all user values directly.
C. Use a parameterized query with multiple placeholders and pass all values in an array.
D. Use template literals to insert user data inside the query string.
Solution
Step 1: Understand batch insertion with parameterization
To insert multiple records safely, use placeholders for each value and pass all values as an array.
Step 2: Avoid unsafe concatenation or unparameterized queries
Concatenating strings or using template literals risks injection; separate queries without parameters are inefficient and unsafe.
Final Answer:
Use a parameterized query with multiple placeholders and pass all values in an array. -> Option C