0
0
PostgreSQLquery~5 mins

String aggregation with STRING_AGG in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ATo combine multiple string values into one string with a separator
BTo count the number of rows in a table
CTo find the maximum value in a column
DTo split a string into multiple rows
How do you specify the separator in STRING_AGG?
AAs the second argument
BAs the first argument
CUsing a separate function
DIt uses a default separator and cannot be changed
Which clause can be used inside STRING_AGG to order the concatenated strings?
AGROUP BY
BHAVING
CORDER BY
DWHERE
What does STRING_AGG return if no rows are found?
AAn error
BAn empty string
CZero
DNULL
Which of the following is a valid STRING_AGG usage?
ASTRING_AGG(name)
BSTRING_AGG(name, ', ' ORDER BY name)
CSTRING_AGG(name, ', ', GROUP BY name)
DSTRING_AGG(name, ', ', WHERE age > 20)
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.