SQL
Concatenating Grouped Strings (GROUP_CONCAT / STRING_AGG)
Efficiently combine multiple related string values from a grouped set of rows into a single comma-separated string within your SQL query, useful for tag lists.
SELECT
p.id AS product_id,
p.name AS product_name,
STRING_AGG(t.tag_name, ', ') AS associated_tags
FROM
products p
LEFT JOIN
product_tags pt ON p.id = pt.product_id
LEFT JOIN
tags t ON pt.tag_id = t.id
GROUP BY
p.id, p.name
ORDER BY
p.id;
How it works: This query demonstrates how to aggregate multiple string values (e.g., tags) into a single concatenated string per group (e.g., per product). PostgreSQL uses STRING_AGG(expression, delimiter). MySQL's equivalent is GROUP_CONCAT(expression SEPARATOR delimiter). This is highly useful for displaying lists of associated items directly in your query results.