16.6 String Aggregation: string_agg() and array_agg()
Right, let’s talk about making lists. Because sometimes you don’t want a summary statistic like SUM() or AVG(); you want to see the actual stuff that got grouped together. That’s where string_agg() and array_agg() come in. They’re your go-to functions for when you need to roll up a bunch of row values into a single, consolidated value. Think of them as the “and company” in a list of names. string_agg() is the more commonly used of the two. It takes a column of values, mashes them together into a single string, and plops a delimiter of your choice between each one. It’s shockingly useful for generating comma-separated lists, tags, or any scenario where you need a human-readable summary.