Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to replace all occurrences of 'cat' with 'dog' in the column 'pet' from the table 'animals'.
MySQL
SELECT REPLACE(pet, [1], 'dog') FROM animals;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong string to replace, like 'dog' instead of 'cat'.
Confusing the order of arguments in REPLACE.
✗ Incorrect
The REPLACE function replaces all occurrences of the first string ('cat') with the second string ('dog') in the given column.
2fill in blank
mediumComplete the code to replace all spaces with underscores in the 'username' column of the 'users' table.
MySQL
SELECT REPLACE(username, [1], '_') FROM users;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '_' as the string to find instead of the replacement.
Using the word 'space' instead of the space character.
✗ Incorrect
To replace spaces, you specify a space character (' ') as the string to find in REPLACE.
3fill in blank
hardFix the error in the code to replace 'old' with 'new' in the 'description' column from 'products'.
MySQL
SELECT REPLACE(description, [1], 'new') FROM products;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around string literals.
Using double quotes which may not work in MySQL for strings.
✗ Incorrect
String literals in SQL must be enclosed in single quotes, so 'old' is correct.
4fill in blank
hardFill both blanks to replace 'blue' with 'red' in the 'color' column from 'items' table.
MySQL
SELECT REPLACE(color, [1], [2]) FROM items;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of the strings.
Using wrong strings that don't match the task.
✗ Incorrect
The first argument is the string to find ('blue'), the second is the replacement ('red').
5fill in blank
hardFill all three blanks to replace 'cat' with 'dog' in the 'pet' column and rename the output column as 'new_pet' in the 'animals' table.
MySQL
SELECT REPLACE([1], [2], [3]) AS new_pet FROM animals;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using table name instead of column name.
Not quoting the strings to find and replace.
✗ Incorrect
The first blank is the column name 'pet', the second is the string to find 'cat', the third is the replacement 'dog'.