Challenge - 5 Problems
STRING_AGG Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this STRING_AGG query?
Consider a table
fruits with a column name containing values: 'apple', 'banana', 'cherry'. What is the result of this query?SELECT STRING_AGG(name, ', ') AS fruit_list FROM fruits;
PostgreSQL
SELECT STRING_AGG(name, ', ') AS fruit_list FROM fruits;
Attempts:
2 left
💡 Hint
Look at the separator used in STRING_AGG function.
✗ Incorrect
STRING_AGG concatenates strings from multiple rows using the specified separator. Here, ', ' is used, so the fruits are joined with comma and space.
❓ query_result
intermediate2:00remaining
How does STRING_AGG handle NULL values?
Given a table
colors with values: 'red', NULL, 'blue', what is the output of:SELECT STRING_AGG(color, ', ') AS color_list FROM colors;
PostgreSQL
SELECT STRING_AGG(color, ', ') AS color_list FROM colors;
Attempts:
2 left
💡 Hint
STRING_AGG ignores NULL values by default.
✗ Incorrect
STRING_AGG skips NULL values and concatenates only non-null strings with the separator.
📝 Syntax
advanced2:00remaining
Which query correctly orders values in STRING_AGG?
You want to concatenate
city names from table locations ordered alphabetically. Which query is correct?Attempts:
2 left
💡 Hint
STRING_AGG supports ORDER BY inside its parentheses.
✗ Incorrect
The ORDER BY clause must be inside STRING_AGG parentheses to order the concatenated values.
❓ optimization
advanced2:00remaining
Optimizing STRING_AGG with DISTINCT
You want to concatenate unique
tags from a table posts. Which query efficiently returns distinct tags concatenated with ', '?Attempts:
2 left
💡 Hint
STRING_AGG supports DISTINCT inside its first argument.
✗ Incorrect
Using DISTINCT inside STRING_AGG removes duplicates efficiently without subqueries.
🧠 Conceptual
expert2:00remaining
Why use STRING_AGG over array_agg + array_to_string?
Which is the main advantage of using STRING_AGG over combining ARRAY_AGG and ARRAY_TO_STRING for string concatenation in PostgreSQL?
Attempts:
2 left
💡 Hint
Think about performance and intermediate data structures.
✗ Incorrect
STRING_AGG concatenates strings directly, avoiding overhead of building arrays first, making it more efficient.