0
0
PostgreSQLquery~20 mins

String to array and array to string in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Convert a comma-separated string to an array
What is the result of this PostgreSQL query that converts a string to an array?
PostgreSQL
SELECT string_to_array('apple,banana,cherry', ',');
A{apple,banana,cherry}
B['apple','banana','cherry']
Capple,banana,cherry
DARRAY['apple','banana','cherry']
Attempts:
2 left
💡 Hint
Use the string_to_array function with the delimiter as a comma.
query_result
intermediate
2:00remaining
Convert an array to a string with a delimiter
What does this PostgreSQL query return when converting an array to a string separated by semicolons?
PostgreSQL
SELECT array_to_string(ARRAY['red','green','blue'], ';');
Ared;green;blue
B{red;green;blue}
CARRAY['red;green;blue']
Dred,green,blue
Attempts:
2 left
💡 Hint
array_to_string joins array elements using the given delimiter.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in string to array conversion
Which option contains a syntax error when trying to convert a string to an array in PostgreSQL?
PostgreSQL
SELECT string_to_array('dog|cat|mouse', '|');
ASELECT string_to_array('dog|cat|mouse', '|');
B;)'|' ,'esuom|tac|god'(yarra_ot_gnirts TCELES
CSELECT string_to_array('dog|cat|mouse' '|');
DELECT string_to_array('dog|cat|mouse', '|');
Attempts:
2 left
💡 Hint
Check for missing commas between function arguments.
query_result
advanced
2:00remaining
Using array_to_string with NULL elements
What is the output of this query that converts an array with NULL elements to a string?
PostgreSQL
SELECT array_to_string(ARRAY['a', NULL, 'c'], ',');
Aa,c
Ba,,c
Ca,NULL,c
Da,,NULL,c
Attempts:
2 left
💡 Hint
NULL elements become empty strings in the output.
🧠 Conceptual
expert
3:00remaining
Choosing the correct function for string to array conversion with multiple delimiters
You have a string 'one;two,three|four' and want to split it into an array using any of the delimiters ';', ',', or '|'. Which PostgreSQL approach correctly achieves this?
AUse array_to_string('one;two,three|four', '[;,|]')
BUse string_to_array('one;two,three|four', ';,|')
CUse split_part('one;two,three|four', '[;,|]', 1)
DUse regexp_split_to_array('one;two,three|four', '[;,|]')
Attempts:
2 left
💡 Hint
string_to_array only accepts a single delimiter character, not multiple.