Challenge - 5 Problems
Query Parameterization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Why use query parameterization?
Why is query parameterization important when working with databases in Node.js?
Attempts:
2 left
💡 Hint
Think about how user input can affect database commands.
✗ Incorrect
Query parameterization helps keep user data separate from SQL commands, stopping attackers from injecting harmful code.
❓ component_behavior
intermediate2:00remaining
Output of parameterized query execution
Given the following Node.js code using a parameterized query, what will be the output if the user input is "O'Reilly"?
Node.js
const userInput = "O'Reilly"; const query = 'SELECT * FROM authors WHERE last_name = ?'; db.execute(query, [userInput], (err, results) => { if (err) throw err; console.log(results); });
Attempts:
2 left
💡 Hint
How does parameterization handle special characters like apostrophes?
✗ Incorrect
Parameterization safely escapes special characters, so the query runs without syntax errors and returns matching results.
📝 Syntax
advanced2:00remaining
Identify the correct parameterized query syntax
Which of the following Node.js code snippets correctly uses parameterized queries with the mysql2 library?
Attempts:
2 left
💡 Hint
Check how parameters are passed as an array in mysql2.
✗ Incorrect
mysql2 uses '?' placeholders and an array of values for parameters in the execute method.
🔧 Debug
advanced2:00remaining
Find the error in this parameterized query code
What error will occur when running this Node.js code snippet using mysql2?
Node.js
const userId = 5; db.execute('SELECT * FROM users WHERE id = ?', userId, (err, results) => { if (err) throw err; console.log(results); });
Attempts:
2 left
💡 Hint
Check the type of the second argument to execute().
✗ Incorrect
mysql2 expects parameters as an array. Passing a single value causes a TypeError.
❓ state_output
expert2:30remaining
Result of multiple parameterized queries with async/await
Consider this async function using mysql2's promise API. What will be logged to the console?
Node.js
async function getUserData(userId) { const [rows1] = await db.execute('SELECT * FROM users WHERE id = ?', [userId]); const [rows2] = await db.execute('SELECT * FROM orders WHERE user_id = ?', [userId]); console.log(rows1.length, rows2.length); } getUserData(10);
Attempts:
2 left
💡 Hint
Think about how async/await works with promise-based query execution.
✗ Incorrect
Each await pauses until the query completes, returning arrays of rows. The console logs the counts of results.