Bird
Raised Fist0
MySQLquery~20 mins

REPLACE function in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
REPLACE Function 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 REPLACE function query?
Consider the following SQL query:

SELECT REPLACE('apple banana apple', 'apple', 'orange') AS result;

What will be the value of result?
MySQL
SELECT REPLACE('apple banana apple', 'apple', 'orange') AS result;
A'orange banana apple'
B'apple banana orange'
C'apple banana apple'
D'orange banana orange'
Attempts:
2 left
💡 Hint
REPLACE substitutes all occurrences of the search string with the replacement string.
❓ query_result
intermediate
2:00remaining
What does this REPLACE query return?
Given the query:

SELECT REPLACE('123-456-7890', '-', '') AS phone;

What is the value of phone?
MySQL
SELECT REPLACE('123-456-7890', '-', '') AS phone;
A'123 456 7890'
B'123-456-7890'
C'1234567890'
D'123--456--7890'
Attempts:
2 left
💡 Hint
REPLACE removes all dashes by replacing them with an empty string.
📝 Syntax
advanced
2:00remaining
Which REPLACE function usage is syntactically correct?
Identify the correct syntax for the REPLACE function in MySQL:
AREPLACE('hello world', 'world')
BREPLACE('hello world', 'world', 'everyone')
CREPLACE('hello world', 'world', 'everyone', 'extra')
DREPLACE('hello world' 'world' 'everyone')
Attempts:
2 left
💡 Hint
REPLACE requires exactly three arguments: the original string, the substring to find, and the substring to replace it with.
❓ optimization
advanced
2:00remaining
Optimizing multiple REPLACE calls in a query
You want to replace all commas and semicolons in a string with spaces. Which query is more efficient?
ASELECT REPLACE(REPLACE(column_name, ',', ' '), ';', ' ') FROM table_name;
BSELECT REPLACE(column_name, ',;', ' ') FROM table_name;
CSELECT REPLACE(column_name, ',', ' ') FROM table_name WHERE column_name LIKE '%;';
DSELECT REPLACE(column_name, ';', ' ') FROM table_name WHERE column_name LIKE '%,%';
Attempts:
2 left
💡 Hint
REPLACE only replaces exact substrings, so multiple nested calls may be needed for different characters.
🧠 Conceptual
expert
2:00remaining
What happens if the search string is empty in REPLACE?
Consider this query:

SELECT REPLACE('database', '', 'X') AS result;

What will be the output?
MySQL
SELECT REPLACE('database', '', 'X') AS result;
A'XdXaXtXaXbXaXsXeX'
BNULL
CSyntax error
D'database'
Attempts:
2 left
💡 Hint
Replacing an empty string inserts the replacement string between every character and at the start and end.

Practice

(1/5)
1. What does the REPLACE function do in MySQL?
easy
A. It deletes the entire string if a substring is found.
B. It splits the string into multiple parts.
C. It adds a substring at the end of the string.
D. It replaces all occurrences of a specified substring within a string.

Solution

  1. Step 1: Understand the purpose of REPLACE

    The REPLACE function searches for all occurrences of a substring inside a string and replaces them with another substring.
  2. Step 2: Compare options with the function behavior

    Only It replaces all occurrences of a specified substring within a string. correctly describes this behavior. Options A, C, and D describe different string operations.
  3. Final Answer:

    It replaces all occurrences of a specified substring within a string. -> Option D
  4. Quick Check:

    REPLACE changes parts of text inside strings = B [OK]
Hint: REPLACE swaps all found text parts inside a string [OK]
Common Mistakes:
  • Thinking REPLACE deletes the whole string
  • Confusing REPLACE with string splitting
  • Assuming REPLACE adds text instead of replacing
2. Which of the following is the correct syntax to replace 'cat' with 'dog' in the string 'catapult' using MySQL?
easy
A. REPLACE('catapult', 'cat', 'dog')
B. REPLACE('catapult', 'dog', 'cat')
C. REPLACE('catapult', 'catapult', 'dog')
D. REPLACE('catapult', 'dog', 'dog')

Solution

  1. Step 1: Recall REPLACE syntax

    The syntax is REPLACE(original_string, string_to_find, string_to_replace_with).
  2. Step 2: Match parameters to the question

    We want to replace 'cat' with 'dog' in 'catapult', so the correct call is REPLACE('catapult', 'cat', 'dog').
  3. Final Answer:

    REPLACE('catapult', 'cat', 'dog') -> Option A
  4. Quick Check:

    REPLACE(original, find, replace) = A [OK]
Hint: Syntax is REPLACE(text, find, replace) always [OK]
Common Mistakes:
  • Swapping find and replace arguments
  • Replacing the whole string instead of substring
  • Using wrong order of parameters
3. What is the result of this query?
SELECT REPLACE('hello world', 'l', 'x');
medium
A. 'hello world'
B. 'hexxo worlx'
C. 'hexxo worxd'
D. 'hexxo worll'

Solution

  1. Step 1: Identify what is replaced

    All occurrences of 'l' in 'hello world' are replaced with 'x'.
  2. Step 2: Perform the replacement

    'hello world' becomes 'hexxo worlx' because every 'l' changes to 'x'.
  3. Final Answer:

    'hexxo worlx' -> Option B
  4. Quick Check:

    Replace all 'l' with 'x' = 'hexxo worlx' [OK]
Hint: Replace every found letter, not just first [OK]
Common Mistakes:
  • Replacing only first occurrence
  • Not replacing at all
  • Replacing wrong characters
4. Identify the error in this query:
SELECT REPLACE('database', 'a');
medium
A. Missing third argument for replacement string.
B. The first argument should be a column name, not a string.
C. REPLACE cannot be used in SELECT statements.
D. The function name should be REPLACESTR.

Solution

  1. Step 1: Check REPLACE function arguments

    REPLACE requires three arguments: original string, string to find, and string to replace with.
  2. Step 2: Identify missing argument

    The query only has two arguments, missing the replacement string.
  3. Final Answer:

    Missing third argument for replacement string. -> Option A
  4. Quick Check:

    REPLACE needs 3 arguments = A [OK]
Hint: REPLACE needs 3 arguments: original, find, replace [OK]
Common Mistakes:
  • Forgetting the replacement string
  • Using wrong function name
  • Thinking REPLACE can't be in SELECT
5. Given a table products with a column code containing values like 'AB-123', 'AB-456', and 'CD-789', which query replaces the prefix 'AB-' with 'XY-' only in the code column?
hard
A. SELECT REPLACE(code, 'AB', 'XY') AS new_code FROM products;
B. SELECT REPLACE('code', 'AB-', 'XY-') AS new_code FROM products;
C. SELECT REPLACE(code, 'AB-', 'XY-') AS new_code FROM products;
D. SELECT REPLACE(code, 'XY-', 'AB-') AS new_code FROM products;

Solution

  1. Step 1: Understand the goal

    We want to replace the prefix 'AB-' with 'XY-' in the column code values.
  2. Step 2: Analyze each option

    SELECT REPLACE(code, 'AB-', 'XY-') AS new_code FROM products; correctly uses REPLACE on the column code replacing 'AB-' with 'XY-'. SELECT REPLACE('code', 'AB-', 'XY-') AS new_code FROM products; treats 'code' as a string literal, not a column. SELECT REPLACE(code, 'XY-', 'AB-') AS new_code FROM products; reverses replacement. SELECT REPLACE(code, 'AB', 'XY') AS new_code FROM products; replaces 'AB' without the dash, which may affect unintended parts.
  3. Final Answer:

    SELECT REPLACE(code, 'AB-', 'XY-') AS new_code FROM products; -> Option C
  4. Quick Check:

    Replace prefix 'AB-' with 'XY-' in column = D [OK]
Hint: Use column name as first argument, exact substring to replace [OK]
Common Mistakes:
  • Using quotes around column name
  • Replacing wrong substring
  • Swapping find and replace strings
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