Bird
Raised Fist0
SQLquery~10 mins

How string concatenation creates vulnerabilities in SQL - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to safely select a user by ID using string concatenation.

SQL
SELECT * FROM users WHERE id = '[1]';
Drag options to blanks, or click blank then click option'
Auser_input
BDROP TABLE users
C42
D1 OR 1=1
Attempts:
3 left
💡 Hint
Common Mistakes
Using raw user input directly in the query.
Including SQL commands in the input.
2fill in blank
medium

Complete the code to demonstrate a vulnerable query using string concatenation.

SQL
query = "SELECT * FROM users WHERE username = '" + [1] + "';"
Drag options to blanks, or click blank then click option'
Auser_input
Badmin
Csafe_username
Dconstant
Attempts:
3 left
💡 Hint
Common Mistakes
Using safe or constant strings instead of user input.
Not understanding where the input comes from.
3fill in blank
hard

Fix the error in the vulnerable query by replacing the blank with the correct safe method.

SQL
cursor.execute("SELECT * FROM users WHERE username = %s", ([1],))
Drag options to blanks, or click blank then click option'
Auser_input
B"user_input"
Cuser_input + "' OR '1'='1"
Duser_input; DROP TABLE users
Attempts:
3 left
💡 Hint
Common Mistakes
Passing raw strings with SQL code inside.
Not using parameter placeholders.
4fill in blank
hard

Fill both blanks to create a safe query using parameterized inputs.

SQL
query = "SELECT * FROM users WHERE username = [1] AND password = [2]"
Drag options to blanks, or click blank then click option'
A%s
B"user_input"
C'%s'
Duser_input
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around placeholders incorrectly.
Embedding user input directly.
5fill in blank
hard

Fill all three blanks to safely execute a query with parameters.

SQL
cursor.execute("SELECT * FROM users WHERE username = [1] AND password = [2]", ([3], user_password))
Drag options to blanks, or click blank then click option'
A%s
Buser_name
D'%s'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around %s placeholders.
Passing raw strings instead of variables.

Practice

(1/5)
1. What is the main security risk when using string concatenation to build SQL queries?
easy
A. It can allow attackers to inject malicious SQL code.
B. It makes queries run faster than parameterized queries.
C. It automatically encrypts user input.
D. It prevents any syntax errors in the query.

Solution

  1. Step 1: Understand string concatenation in SQL queries

    Concatenating user input directly into SQL commands can mix code and data.
  2. Step 2: Recognize the risk of SQL injection

    This mixing allows attackers to add harmful SQL commands, called SQL injection.
  3. Final Answer:

    It can allow attackers to inject malicious SQL code. -> Option A
  4. Quick Check:

    SQL injection risk = A [OK]
Hint: Concatenating user input risks injection attacks [OK]
Common Mistakes:
  • Thinking concatenation speeds up queries
  • Believing concatenation encrypts input
  • Assuming concatenation prevents errors
2. Which of the following SQL query constructions using string concatenation is syntactically correct?
easy
A. SELECT * FROM users WHERE name = '\'' + userName + '\'';
B. SELECT * FROM users WHERE name = '\'' + userName + '\'
C. SELECT * FROM users WHERE name = ' + userName + "';
D. SELECT * FROM users WHERE name = '\'' + userName + "';

Solution

  1. Step 1: Check string delimiters and concatenation syntax

    In SQL, string literals are enclosed in single quotes. Concatenation in code requires proper quotes and plus signs.
  2. Step 2: Identify correct syntax for concatenation in code

    SELECT * FROM users WHERE name = '\'' + userName + '\''; correctly escapes single quotes for SQL string literals and concatenates userName with plus signs.
  3. Final Answer:

    "SELECT * FROM users WHERE name = '\'' + userName + '\'';" -> Option A
  4. Quick Check:

    Proper quotes and concatenation = A [OK]
Hint: Look for matching quotes and plus signs [OK]
Common Mistakes:
  • Missing semicolon at end
  • Mismatched or missing quotes
  • Using wrong concatenation operators
3. Given the query built by concatenation: "SELECT * FROM users WHERE username = '" + input + "';" and input = admin' OR '1'='1, what will be the effect when executed?
medium
A. Returns only the user with username 'admin'.
B. Returns all users because the WHERE clause is always true.
C. Causes a syntax error and query fails.
D. Returns no rows because input is invalid.

Solution

  1. Step 1: Substitute input into the query string

    The query becomes: SELECT * FROM users WHERE username = 'admin' OR '1'='1';
  2. Step 2: Analyze the WHERE clause logic

    The condition '1'='1' is always true, so the WHERE clause matches all rows.
  3. Final Answer:

    Returns all users because the WHERE clause is always true. -> Option B
  4. Quick Check:

    Injected OR '1'='1' returns all rows = D [OK]
Hint: Injected OR '1'='1' makes WHERE always true [OK]
Common Mistakes:
  • Thinking it returns only 'admin'
  • Assuming syntax error occurs
  • Believing no rows are returned
4. Identify the error in this SQL query construction using string concatenation: query = "SELECT * FROM users WHERE id = " + userId; where userId is a string input.
medium
A. Using plus sign for concatenation is invalid in SQL.
B. No error; query is correct.
C. Query is safe from SQL injection.
D. Missing quotes around userId value causing syntax error.

Solution

  1. Step 1: Check how userId is inserted in query

    The query concatenates userId without quotes, so if userId is a string, SQL syntax breaks.
  2. Step 2: Understand SQL string literal requirements

    String values must be enclosed in single quotes in SQL to be valid.
  3. Final Answer:

    Missing quotes around userId value causing syntax error. -> Option D
  4. Quick Check:

    String values need quotes = B [OK]
Hint: String inputs need quotes in SQL queries [OK]
Common Mistakes:
  • Thinking plus sign is invalid in code
  • Assuming query is safe from injection
  • Ignoring missing quotes causes errors
5. You want to prevent SQL injection when building queries with user input. Which approach best avoids vulnerabilities caused by string concatenation?
hard
A. Remove all spaces from user input before concatenation.
B. Escape all single quotes in user input manually before concatenation.
C. Use parameterized queries or prepared statements instead of concatenation.
D. Convert user input to uppercase before concatenation.

Solution

  1. Step 1: Understand the problem with string concatenation

    Concatenation mixes code and data, allowing injection if input is malicious.
  2. Step 2: Identify secure alternatives

    Parameterized queries separate code from data, preventing injection regardless of input.
  3. Final Answer:

    Use parameterized queries or prepared statements instead of concatenation. -> Option C
  4. Quick Check:

    Parameterized queries prevent injection = C [OK]
Hint: Always prefer parameterized queries over concatenation [OK]
Common Mistakes:
  • Relying on manual escaping which is error-prone
  • Thinking removing spaces prevents injection
  • Assuming uppercase conversion secures input
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard