Recall & Review
beginner
What does the STRING_AGG function do in PostgreSQL?
STRING_AGG combines multiple string values from rows into a single string, separated by a specified delimiter.
Click to reveal answer
beginner
How do you specify the separator in STRING_AGG?
You provide the separator as the second argument, for example: STRING_AGG(column_name, ', ') combines values separated by a comma and space.
Click to reveal answer
intermediate
Can STRING_AGG be used with an ORDER BY clause? If yes, why?
Yes, STRING_AGG supports ORDER BY inside its syntax to control the order of concatenated strings, ensuring predictable output.
Click to reveal answer
beginner
Write a simple example of STRING_AGG usage to combine names separated by a semicolon.
SELECT STRING_AGG(name, '; ') FROM users;
Click to reveal answer
intermediate
What happens if STRING_AGG is used on an empty set of rows?
STRING_AGG returns NULL if there are no rows to aggregate.
Click to reveal answer
What is the purpose of STRING_AGG in PostgreSQL?
✗ Incorrect
STRING_AGG combines multiple strings into one, separated by a specified delimiter.
How do you specify the separator in STRING_AGG?
✗ Incorrect
The separator is the second argument in STRING_AGG, for example: STRING_AGG(column, ', ').
Which clause can be used inside STRING_AGG to order the concatenated strings?
✗ Incorrect
ORDER BY inside STRING_AGG controls the order of concatenated strings.
What does STRING_AGG return if no rows are found?
✗ Incorrect
STRING_AGG returns NULL when there are no rows to aggregate.
Which of the following is a valid STRING_AGG usage?
✗ Incorrect
STRING_AGG can include ORDER BY inside parentheses to order the concatenated strings.
Explain how STRING_AGG works and how you can control the order of concatenated strings.
Think about how to join many names into one string with a chosen separator and order.
You got /4 concepts.
Write a query using STRING_AGG to list all product names separated by a dash (-), ordered alphabetically.
Use STRING_AGG(product_name, ' - ' ORDER BY product_name) from products.
You got /4 concepts.