Challenge - 5 Problems
REPLACE Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this REPLACE function query?
Consider the following SQL query:
What will be the value of
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;
Attempts:
2 left
💡 Hint
REPLACE substitutes all occurrences of the search string with the replacement string.
✗ Incorrect
The REPLACE function replaces every occurrence of 'apple' with 'orange' in the string, so both 'apple's become 'orange'.
❓ query_result
intermediate2:00remaining
What does this REPLACE query return?
Given the query:
What is the value of
SELECT REPLACE('123-456-7890', '-', '') AS phone;What is the value of
phone?MySQL
SELECT REPLACE('123-456-7890', '-', '') AS phone;
Attempts:
2 left
💡 Hint
REPLACE removes all dashes by replacing them with an empty string.
✗ Incorrect
Replacing '-' with '' removes all dashes, so the phone number becomes a continuous string of digits.
📝 Syntax
advanced2:00remaining
Which REPLACE function usage is syntactically correct?
Identify the correct syntax for the REPLACE function in MySQL:
Attempts:
2 left
💡 Hint
REPLACE requires exactly three arguments: the original string, the substring to find, and the substring to replace it with.
✗ Incorrect
Option B correctly uses three arguments. Option B misses the replacement string, B has too many arguments, and D lacks commas.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
REPLACE only replaces exact substrings, so multiple nested calls may be needed for different characters.
✗ Incorrect
Option A correctly replaces commas and semicolons by nesting REPLACE calls. Option A tries to replace ',;' as a single substring which won't work. Options A and C only replace one character conditionally.
🧠 Conceptual
expert2:00remaining
What happens if the search string is empty in REPLACE?
Consider this query:
What will be the output?
SELECT REPLACE('database', '', 'X') AS result;What will be the output?
MySQL
SELECT REPLACE('database', '', 'X') AS result;
Attempts:
2 left
💡 Hint
Replacing an empty string inserts the replacement string between every character and at the start and end.
✗ Incorrect
When the search string is empty, REPLACE inserts the replacement string between every character and at the start and end of the original string.