0
0
SQLquery~10 mins

REPLACE function in SQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A'dog'
B'bird'
C'fish'
D'lion'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong replacement string.
Forgetting to put quotes around the replacement string.
2fill in blank
medium

Complete 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'
A'-'
B'_'
C' '
D','
Attempts:
3 left
💡 Hint
Common Mistakes
Using underscore '_' as the string to replace instead of space.
Forgetting to use quotes around the space character.
3fill in blank
hard

Fix 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'
Anew
Bnew'
C"new"
D'new'
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.
4fill in blank
hard

Fill 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'
Acolor
B'blue'
C'red'
Dshade
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the column name and string to replace.
Forgetting quotes around the string 'blue'.
5fill in blank
hard

Fill 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'
Aevent_year
B'2023'
C'2024'
D'2025'
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers without quotes.
Mixing up the strings to replace and replacement.