Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to replace all occurrences of 'cat' with 'dog' in the column animal_name.
SQL
SELECT REPLACE(animal_name, 'cat', [1]) AS new_name FROM animals;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong replacement string.
Forgetting to put quotes around the replacement string.
✗ Incorrect
The REPLACE function replaces all occurrences of the second argument with the third argument. Here, 'cat' is replaced with 'dog'.
2fill in blank
mediumComplete the code to replace all spaces with underscores in the column product_name.
SQL
SELECT REPLACE(product_name, [1], '_') AS new_product FROM products;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using underscore '_' as the string to replace instead of space.
Forgetting to use quotes around the space character.
✗ Incorrect
To replace spaces, the second argument must be a space character ' '.
3fill in blank
hardFix the error in the code to replace 'old' with 'new' in the description column.
SQL
SELECT REPLACE(description, 'old', [1]) FROM items;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the replacement string.
Using double quotes which may not be valid in all SQL dialects.
✗ Incorrect
String literals in SQL must be enclosed in single quotes. So 'new' is correct.
4fill in blank
hardFill both blanks to replace all occurrences of 'blue' with 'red' in the color column and rename the output as new_color.
SQL
SELECT REPLACE([1], [2], 'red') AS new_color FROM colors;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the column name and string to replace.
Forgetting quotes around the string 'blue'.
✗ Incorrect
The first blank is the column name 'color'. The second blank is the string to replace, which is 'blue'.
5fill in blank
hardFill all three blanks to replace '2023' with '2024' in the event_year column, rename the output as updated_year, and select from the events table.
SQL
SELECT REPLACE([1], [2], [3]) AS updated_year FROM events;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers without quotes.
Mixing up the strings to replace and replacement.
✗ Incorrect
The first blank is the column name 'event_year'. The second blank is the string to replace '2023'. The third blank is the replacement string '2024'.