Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to convert a comma-separated string into an array.
PostgreSQL
SELECT string_to_array('apple,banana,cherry', [1]) AS fruits_array;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong delimiter like ';' or '.' which won't split the string correctly.
✗ Incorrect
The string_to_array function splits the string by the delimiter. Here, the delimiter is a comma ',' to separate the fruits.
2fill in blank
mediumComplete the code to join an array of strings into a single string separated by semicolons.
PostgreSQL
SELECT array_to_string(ARRAY['red', 'green', 'blue'], [1]) AS colors_string;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a comma ',' instead of a semicolon ';' which changes the output format.
✗ Incorrect
The array_to_string function joins array elements using the specified delimiter. Here, ';' is used to separate colors.
3fill in blank
hardFix the error in the code to correctly convert a string to an array using a space as delimiter.
PostgreSQL
SELECT string_to_array('one two three', [1]) AS words_array;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an empty string '' as delimiter which won't split the string correctly.
Using a comma ',' which does not split the string correctly.
✗ Incorrect
To split the string by spaces, the delimiter must be a space character ' '.
4fill in blank
hardFill both blanks to convert a string to an array and then back to a string with a different delimiter.
PostgreSQL
SELECT array_to_string(string_to_array('cat|dog|bird', [1]), [2]) AS new_string;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong delimiters that do not match the original string or desired output.
✗ Incorrect
First, split the string by '|' to get the array. Then join the array elements with ';' to form the new string.
5fill in blank
hardFill all three blanks to create an array from a string, filter elements containing 'a', and join them with commas.
PostgreSQL
SELECT array_to_string(ARRAY(SELECT * FROM unnest(string_to_array('apple,banana,cherry,date', [1])) WHERE [2] LIKE '%a%'), [3]) AS filtered_string;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong delimiters or missing unnest function causing errors.
✗ Incorrect
Split the string by ',' to get the array. Use unnest to expand array elements. Filter elements containing 'a'. Join filtered elements with ','.