The REPLACE function searches for all occurrences of a substring in a string and replaces them with another substring.
Click to reveal answer
beginner
Syntax of the REPLACE function in MySQL?
REPLACE(original_string, from_substring, to_substring) - It returns a new string where all occurrences of from_substring are replaced by to_substring.
Click to reveal answer
beginner
What happens if the substring to replace is not found in the original string?
The REPLACE function returns the original string unchanged if the substring to replace is not found.
Click to reveal answer
beginner
Example: What is the result of REPLACE('apple pie', 'pie', 'tart')?
The result is 'apple tart' because 'pie' is replaced by 'tart'.
Click to reveal answer
intermediate
Can REPLACE function replace multiple occurrences of the substring?
Yes, REPLACE replaces all occurrences of the substring in the original string, not just the first one.
Click to reveal answer
What does REPLACE('hello world', 'world', 'there') return?
A'there world'
B'hello world'
C'hello there'
D'hello'
✗ Incorrect
REPLACE changes 'world' to 'there', so the result is 'hello there'.
If the substring to replace is not found, what does REPLACE return?
AOriginal string unchanged
BEmpty string
CNULL
DAn error
✗ Incorrect
REPLACE returns the original string unchanged if the substring is not found.
Which of these is the correct syntax for REPLACE?
AREPLACE(string, old_substring, new_substring)
BREPLACE(old_substring, new_substring, string)
CREPLACE(string, new_substring, old_substring)
DREPLACE(new_substring, string, old_substring)
✗ Incorrect
The correct syntax is REPLACE(original_string, from_substring, to_substring).
What will REPLACE('banana', 'a', 'o') return?
Abanana
Bbano
Cbnn
Dbonono
✗ Incorrect
All 'a's are replaced by 'o's, so 'banana' becomes 'bonono'.
Can REPLACE be used to remove a substring by replacing it with an empty string?
AOnly if substring is at the start
BYes
CNo
DOnly if substring is at the end
✗ Incorrect
Replacing a substring with an empty string effectively removes it.
Explain how the REPLACE function works in MySQL and give a simple example.
Think about changing parts of a word to another word.
You got /4 concepts.
Describe what happens if the substring to replace does not exist in the original string when using REPLACE.
What if you try to replace something that isn't there?
You got /3 concepts.
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
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.
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.
Final Answer:
It replaces all occurrences of a specified substring within a string. -> Option D
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
Step 1: Recall REPLACE syntax
The syntax is REPLACE(original_string, string_to_find, string_to_replace_with).
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').
Final Answer:
REPLACE('catapult', 'cat', 'dog') -> Option A
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
Step 1: Identify what is replaced
All occurrences of 'l' in 'hello world' are replaced with 'x'.
Step 2: Perform the replacement
'hello world' becomes 'hexxo worlx' because every 'l' changes to 'x'.
Final Answer:
'hexxo worlx' -> Option B
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
Step 1: Check REPLACE function arguments
REPLACE requires three arguments: original string, string to find, and string to replace with.
Step 2: Identify missing argument
The query only has two arguments, missing the replacement string.
Final Answer:
Missing third argument for replacement string. -> Option A
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
Step 1: Understand the goal
We want to replace the prefix 'AB-' with 'XY-' in the column code values.
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.
Final Answer:
SELECT REPLACE(code, 'AB-', 'XY-') AS new_code FROM products; -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.