0
0
MySQLquery~20 mins

REPLACE function in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 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.