0
0
SQLquery~20 mins

How SQL injection exploits queries - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SQL Injection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this vulnerable SQL query?

Consider a web application that constructs this SQL query by directly inserting user input username without sanitization:

SELECT * FROM users WHERE username = 'user_input';

If the user inputs ' OR '1'='1, what will the query return?

SQL
SELECT * FROM users WHERE username = '' OR '1'='1';
AReturns only rows where username is exactly '' OR '1'='1'
BReturns no rows
CRaises a syntax error
DReturns all rows from the users table
Attempts:
2 left
💡 Hint

Think about how the OR condition affects the WHERE clause.

🧠 Conceptual
intermediate
1:30remaining
Which part of this query is vulnerable to SQL injection?

Given this query construction in code:

query = "SELECT * FROM products WHERE category = '" + user_input + "';"

Which part allows SQL injection?

AUsing single quotes around <code>user_input</code>
BDirectly concatenating <code>user_input</code> into the query string
CSelecting from the products table
DUsing the WHERE clause
Attempts:
2 left
💡 Hint

Consider how user input is included in the query.

📝 Syntax
advanced
2:00remaining
Which option correctly prevents SQL injection using parameterized queries?

Choose the correct SQL query syntax using parameters to safely query a user by username.

ASELECT * FROM users WHERE username = ?;
BSELECT * FROM users WHERE username = '" + username + "';
CSELECT * FROM users WHERE username = username;
DSELECT * FROM users WHERE username = :username OR 1=1;
Attempts:
2 left
💡 Hint

Look for the syntax that uses placeholders for parameters.

🔧 Debug
advanced
2:00remaining
Why does this query allow SQL injection?

Examine this code snippet:

query = f"SELECT * FROM orders WHERE order_id = {order_id};"

Assuming order_id comes from user input, why is this vulnerable?

ABecause order_id is an integer and cannot be injected
BBecause the query uses double quotes instead of single quotes
CBecause <code>order_id</code> is inserted directly without validation or parameterization
DBecause the query is missing a semicolon
Attempts:
2 left
💡 Hint

Think about how user input is included in the query string.

optimization
expert
2:30remaining
Which approach best prevents SQL injection and improves query performance?

Given a web app querying user data by email, which option both prevents SQL injection and optimizes query execution?

AUse parameterized queries with an index on the email column
BEscape single quotes in user input and use string concatenation
CUse dynamic SQL with user input directly inserted
DUse a LIKE clause with wildcards around user input
Attempts:
2 left
💡 Hint

Consider both security and performance aspects.